Permalink
Browse files

Add ability to pass headers, clean up and organize tests, fix logging…

… text
  • Loading branch information...
kyledrake committed Aug 30, 2011
1 parent c50d897 commit e71c5d1e43b81467e3f0a22691888d3d22970e72
Showing with 385 additions and 413 deletions.
  1. +0 −2 .travis.yml
  2. +6 −6 lib/geoloqi.rb
  3. +10 −10 lib/geoloqi/session.rb
  4. +1 −1 lib/geoloqi/version.rb
  5. +23 −0 spec/env.rb
  6. +11 −0 spec/geoloqi_api_error_spec.rb
  7. +46 −0 spec/geoloqi_config.rb
  8. +279 −0 spec/geoloqi_session_spec.rb
  9. +9 −394 spec/geoloqi_spec.rb
View
@@ -1,5 +1,3 @@
rvm:
- 1.9.2
- - rbx
- - rbx-2.0
- jruby
View
@@ -32,16 +32,16 @@ def config(opts=nil)
@@config = Config.new opts
end
- def get(access_token, path, args={})
- run :get, access_token, path, args
+ def get(access_token, path, args={}, headers={})
+ run :get, access_token, path, args, headers
end
- def post(access_token, path, args={})
- run :post, access_token, path, args
+ def post(access_token, path, args={}, headers={})
+ run :post, access_token, path, args, headers
end
- def run(meth, access_token, path, args={})
- Session.new(:access_token => access_token).run meth, path, args
+ def run(meth, access_token, path, args={}, headers={})
+ Session.new(:access_token => access_token).run meth, path, args, headers
end
def authorize_url(client_id=nil, redirect_uri=@@config.redirect_uri, opts={})
View
@@ -30,20 +30,20 @@ def authorize_url(redirect_uri=@config.redirect_uri, opts={})
Geoloqi.authorize_url @config.client_id, redirect_uri, opts
end
- def get(path, query=nil)
- run :get, path, query
+ def get(path, query=nil, headers={})
+ run :get, path, query, headers
end
- def post(path, query=nil)
- run :post, path, query
+ def post(path, query=nil, headers={})
+ run :post, path, query, headers
end
- def run(meth, path, query=nil)
+ def run(meth, path, query=nil, headers={})
renew_access_token! if auth[:expires_at] && Time.rfc2822(auth[:expires_at]) <= Time.now && !(path =~ /^\/?oauth\/token$/)
retry_attempt = 0
begin
- response = execute meth, path, query
+ response = execute meth, path, query, headers
hash = JSON.parse response.body
raise ApiError.new(response.status, hash['error'], hash['error_description']) if hash.is_a?(Hash) && hash['error'] && @config.throw_exceptions
rescue Geoloqi::ApiError
@@ -61,11 +61,11 @@ def run(meth, path, query=nil)
@config.use_hashie_mash ? Hashie::Mash.new(hash) : hash
end
- def execute(meth, path, query=nil)
+ def execute(meth, path, query=nil, headers={})
query = Rack::Utils.parse_query query if query.is_a?(String)
raw = @connection.send(meth) do |req|
req.url "/#{Geoloqi.api_version.to_s}/#{path.gsub(/^\//, '')}"
- req.headers = headers
+ req.headers = headers.merge! default_headers
if query
meth == :get ? req.params = query : req.body = query.to_json
@@ -75,7 +75,7 @@ def execute(meth, path, query=nil)
if @config.logger
@config.logger.print "### Geoloqi::Session - #{meth.to_s.upcase} #{path}"
@config.logger.print "?#{Rack::Utils.build_query query}" unless query.nil?
- @config.logger.puts "\nStatus: #{raw.status}\nHeaders: #{raw.headers.inspect}\nBody: #{raw.body}"
+ @config.logger.puts "\n### Status: #{raw.status}\n### Headers: #{raw.headers.inspect}\n### Body: #{raw.body}"
end
Response.new raw.status, raw.headers, raw.body
@@ -109,7 +109,7 @@ def auth_expires_at(expires_in=nil)
expires_in.to_i.zero? ? nil : ((Time.now + expires_in.to_i)-5).rfc2822
end
- def headers
+ def default_headers
headers = {'Content-Type' => 'application/json', 'User-Agent' => "geoloqi-ruby #{Geoloqi.version}", 'Accept' => 'application/json'}
headers['Authorization'] = "OAuth #{access_token}" if access_token
headers
View
@@ -1,5 +1,5 @@
module Geoloqi
def self.version
- '0.9.26'
+ '0.9.27'
end
end
View
@@ -0,0 +1,23 @@
+# Bundler.setup
+require 'rubygems'
+require './lib/geoloqi.rb'
+require 'minitest/autorun'
+require 'wrong'
+require 'wrong/adapters/minitest'
+require 'webmock'
+
+CLIENT_ID = 'client_id1234'
+CLIENT_SECRET = 'client_secret1234'
+ACCESS_TOKEN = 'access_token1234'
+
+def auth_headers(access_token='access_token1234')
+ {'Content-Type' => 'application/json',
+ 'User-Agent' => "geoloqi-ruby #{Geoloqi.version}",
+ 'Accept' => 'application/json',
+ 'Authorization' => "OAuth #{access_token}"}
+end
+
+def api_url(path); "#{Geoloqi.api_url}/#{Geoloqi.api_version}/#{path}" end
+
+Wrong.config.alias_assert :expect
+include WebMock::API
@@ -0,0 +1,11 @@
+require File.join File.dirname(__FILE__), 'env.rb'
+
+describe Geoloqi::ApiError do
+ it 'throws exception properly and allows drill-down of message' do
+ error = Geoloqi::ApiError.new 405, 'not_enough_cats', 'not enough cats to complete this request'
+ expect { error.status == 405 }
+ expect { error.type == 'not_enough_cats' }
+ expect { error.reason == 'not enough cats to complete this request' }
+ expect { error.message == "#{error.type} - #{error.reason} (405)" }
+ end
+end
View
@@ -0,0 +1,46 @@
+require File.join File.dirname(__FILE__), 'env.rb'
+
+describe Geoloqi::Config do
+ describe 'with redirect_uri' do
+ it 'returns authorize url' do
+ Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :redirect_uri => 'http://blah.blah/test'
+ authorize_url = Geoloqi.authorize_url 'test'
+ expect { authorize_url == "#{Geoloqi.oauth_url}?"+
+ 'response_type=code&'+
+ "client_id=#{Rack::Utils.escape 'test'}&"+
+ "redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}" }
+ end
+ end
+
+ it 'displays log information if logger is provided' do
+ stub_request(:get, api_url('account/username?cats=lol')).
+ with(:headers => {'Authorization'=>'OAuth access_token1234'}).
+ to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
+
+ io = StringIO.new
+ Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :logger => io
+
+ Geoloqi.get ACCESS_TOKEN, 'account/username', :cats => 'lol'
+ expect { io.string =~ /Geoloqi::Session/ }
+ end
+
+ it 'displays log information if logger is provided and query is nil' do
+ stub_request(:get, api_url('account/username')).
+ with(:headers => {'Authorization'=>'OAuth access_token1234'}).
+ to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
+
+ io = StringIO.new
+ Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :logger => io
+
+ Geoloqi.get ACCESS_TOKEN, 'account/username'
+ expect { io.string =~ /Geoloqi::Session/ }
+ end
+
+ it 'correctly checks booleans for client_id and client_secret' do
+ [:client_id, :client_secret].each do |k|
+ expect { Geoloqi.config(k => '').send("#{k}?") == false }
+ expect { Geoloqi.config(k => nil).send("#{k}?") == false }
+ expect { Geoloqi.config(k => 'lol').send("#{k}?") == true }
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit e71c5d1

Please sign in to comment.