Permalink
Browse files

Further test refactoring

  • Loading branch information...
1 parent 920d71d commit 6f8d3fbc38c84a9cc80aae657d73a4d3fcf4c561 @kyledrake kyledrake committed Apr 26, 2011
Showing with 17 additions and 33 deletions.
  1. +17 −33 models/geoloqi.rb
View
@@ -17,47 +17,31 @@ def self.headers(oauth_token)
def self.run(meth, oauth_token, url, body=nil)
args = {:head => headers(oauth_token)}
args[:body] = body.to_json if body
- response_json = JSON.parse EM::Synchrony.sync(EventMachine::HttpRequest.new(API_URL+url).send(meth.to_sym, args)).response
- raise Error.new(response_json['error'], response_json['error_description']) if response_json['error']
- response_json
- end
-
- def self.post(oauth_token, url, body)
- obj = run :post, oauth_token, url, body
- case obj
+ response = JSON.parse EM::Synchrony.sync(EventMachine::HttpRequest.new(API_URL+url).send(meth.to_sym, args)).response
+ raise Error.new(response['error'], response['error_description']) if response['error']
+
+ case response
when Array
- ret = []
- obj.each do |el|
- ret << SymbolTable.new(el)
- end
+ response.map! {|e| SymbolTable.new e}
when Hash
- ret = SymbolTable.new obj
+ SymbolTable.new response
end
- ret
+ end
+
+ def self.post(oauth_token, url, body)
+ run :post, oauth_token, url, body
end
def self.get(oauth_token, url)
- obj = run :get, oauth_token, url
- case obj
- when Array
- ret = []
- obj.each do |el|
- ret << SymbolTable.new(el)
- end
- when Hash
- ret = SymbolTable.new obj
- end
- ret
+ run :get, oauth_token, url
end
def self.get_token(auth_code, redirect_uri)
- args = {} # {:head => {'Authorization' => "Basic " + Base64.encode64(Geoloqi::CLIENT_ID + ":" + Geoloqi::CLIENT_SECRET)}}
- args[:body] = Rack::Utils.escape :client_id => Geoloqi::CLIENT_ID,
- :client_secret => Geoloqi::CLIENT_SECRET,
- :code => auth_code,
- :grant_type => "authorization_code",
- :redirect_uri => redirect_uri
- response = EM::Synchrony.sync(EventMachine::HttpRequest.new(API_URL+"oauth/token").post(args)).response
- response_json = JSON.parse response
+ args = {:body => Rack::Utils.escape(:client_id => Geoloqi::CLIENT_ID,
+ :client_secret => Geoloqi::CLIENT_SECRET,
+ :code => auth_code,
+ :grant_type => "authorization_code",
+ :redirect_uri => redirect_uri)}
+ JSON.parse EM::Synchrony.sync(EventMachine::HttpRequest.new(API_URL+"oauth/token").post(args)).response
end
end

0 comments on commit 6f8d3fb

Please sign in to comment.