Skip to content

Commit

Permalink
Say
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwynn committed Apr 18, 2013
1 parent 6421b5e commit f891e55
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 5 deletions.
20 changes: 19 additions & 1 deletion lib/octokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
require 'octokit/authentication'
require 'octokit/rate_limit'
require 'octokit/client/rate_limit'
require 'octokit/client/say'


module Octokit
class Client
include Octokit::Authentication
include Octokit::Configurable
include Octokit::Client::RateLimit
include Octokit::Client::Say

attr_reader :last_response

Expand Down Expand Up @@ -63,6 +65,7 @@ def head(url, options = {})

def agent
@agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
http.headers[:accept] = default_media_type
if basic_authenticated?
http.basic_auth(@login, @password)
elsif token_authenticated?
Expand All @@ -76,7 +79,22 @@ def request(method, url, options)
options[:query] ||= {}
options[:query].merge! application_authentication
end
@last_response = agent.call(method, url, options)
if accept = options.delete(:accept)
options[:headers] ||= {}
options[:headers][:accept] = accept
end

@last_response = response = agent.call(method, url, options)
response.data
end

# Executes the request, checking if it was successful
#
# @return [Boolean] True on success, false otherwise
def boolean_from_response(method, path, options={})
request(method, path, options).status == 204
rescue Octokit::NotFound
false
end


Expand Down
2 changes: 1 addition & 1 deletion lib/octokit/client/say.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Say
def say(text=nil)
options = {}
options[:s] = text if text
get "/octocat", options
get "/octocat", :query => options
end
alias :octocat :say

Expand Down
3 changes: 2 additions & 1 deletion lib/octokit/configurable.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Octokit
module Configurable
attr_accessor :api_endpoint, :auto_paginate, :client_id, :login, :per_page, :proxy, :user_agent, :web_endpoint
attr_accessor :api_endpoint, :auto_paginate, :client_id, :default_media_type, :login, :per_page, :proxy, :user_agent, :web_endpoint
attr_writer :access_token, :client_secret, :password

class << self
Expand All @@ -12,6 +12,7 @@ def keys
:auto_paginate,
:client_id,
:client_secret,
:default_media_type,
:login,
:per_page,
:password,
Expand Down
7 changes: 6 additions & 1 deletion lib/octokit/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module Octokit
module Default

API_ENDPOINT = "https://api.github.com".freeze
WEB_ENDPOINT = "https://github.com".freeze
USER_AGENT = "Octokit Ruby Gem #{Octokit::VERSION}".freeze
MEDIA_TYPE = "application/vnd.github.beta+json"
WEB_ENDPOINT = "https://github.com".freeze

class << self

Expand Down Expand Up @@ -31,6 +32,10 @@ def client_secret
ENV['OCTOKIT_SECRET']
end

def default_media_type
ENV['OCTOKIT_DEFAULT_MEDIA_TYPE'] || MEDIA_TYPE
end

def login
ENV['OCTOKIT_LOGIN']
end
Expand Down
2 changes: 1 addition & 1 deletion octokit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'octokit/version'

Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.0'
spec.add_dependency 'sawyer', '~> 0.0.8'
spec.add_dependency 'sawyer', '~> 0.0.9'
spec.authors = ["Wynn Netherland", "Erik Michaels-Ober", "Clint Shryock"]
spec.description = %q{Simple wrapper for the GitHub v3 API}
spec.email = ['[email protected]', '[email protected]', '[email protected]']
Expand Down
109 changes: 109 additions & 0 deletions spec/cassettes/say.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions spec/octokit/client/say_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require File.expand_path('../../../spec_helper.rb', __FILE__)

describe Octokit::Client::Say do
before do
@client = Octokit::Client.new
end

describe ".say" do
it "returns an ASCII octocat" do
VCR.use_cassette('say', :match_requests_on => [:uri, :query]) do
text = @client.say
puts text.inspect
text.must_match /logically awesome/
end
end

it "returns an ASCII octocat with custom text" do
VCR.use_cassette('say', :match_requests_on => [:uri, :query]) do
text = @client.say "There is no need to be upset"
text.must_match /upset/
end
end
end

end
25 changes: 25 additions & 0 deletions spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,29 @@
end
end
end

describe "when making requests" do
before do
Octokit.reset!
@client = Octokit.client
end
it "Accepts application/vnd.github.beta+json by default" do
VCR.use_cassette 'root' do
root_request = stub_get("/").
with(:headers => {:accept => "application/vnd.github.beta+json"})
@client.get "/"
assert_requested root_request
@client.last_response.status.must_equal 200
end
end
it "allows Accept'ing another media type" do
VCR.use_cassette 'root' do
root_request = stub_get("/").
with(:headers => {:accept => "application/vnd.github.beta.diff+json"})
@client.get "/", :accept => "application/vnd.github.beta.diff+json"
assert_requested root_request
@client.last_response.status.must_equal 200
end
end
end
end

0 comments on commit f891e55

Please sign in to comment.