Created
December 31, 2012 01:57
-
-
Save anonymous/4416782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'hashie' | |
require 'oauth' | |
Hash.send :include, Hashie::HashExtensions | |
module Taste | |
class Test | |
def hi | |
puts "this method works" | |
end | |
end | |
class OAuth | |
def initialize(token, secret, options={}) | |
@consumer_token, @consumer_secret = token, secret | |
end | |
def consumer | |
return @consumer if @consumer | |
@consumer = ::OAuth::Consumer.new(@consumer_token, @consumer_secret, { | |
:site => "http://foodspotting.com", | |
:scheme => :header, | |
:http_method => :post, | |
:request_token_path => "/oauth/request_token", | |
:access_token_path => "/oauth/access_token", | |
:authorize_path => "/oauth/authorize", | |
:proxy => (ENV['HTTP_PROXY'] || ENV['http_proxy']) | |
}) | |
end | |
def set_callback_url(url) | |
clear_request_token | |
request_token(:oauth_callback => url) | |
end | |
def request_token(options={}) | |
@request_token ||= consumer.get_request_token(options) | |
end | |
def authorize_from_request(request_token, request_secret, verifier) | |
request_token = ::OAuth::RequestToken.new(consumer, request_token, request_secret) | |
access_token = request_token.get_access_token(:oauth_verifier => verifier) | |
@atoken, @asecret = access_token.token, access_token.secret | |
end | |
def access_token | |
@access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret) | |
end | |
def authorize_from_access(atoken, asecret) | |
@atoken, @asecret = atoken, asecret | |
end | |
private | |
def clear_request_token | |
@request_token = nil | |
end | |
end | |
class Base | |
BASE_URL = 'http://www.foodspotting.com/api/v1' | |
FORMAT = 'json' | |
attr_accessor :oauth | |
def initialize(oauth) | |
@oauth = oauth | |
end | |
def method_missing(method_symbol, params = {}) | |
method_name = method_symbol.to_s.split(/\.|_/).join('/') | |
if (method_name[-1,1]) == '=' | |
method = method_name[0..-2] | |
result = post(api_url(method), params) | |
params.replace(result[method] || result) | |
else | |
result = get(api_url(method_name, params)) | |
result[method_name] || result | |
end | |
end | |
def api(method_symbol, params = {}) | |
Hashie::Mash.new(method_missing(method_symbol, params)) | |
end | |
def api_url(method_name, options = nil) | |
params = options.is_a?(Hash) ? to_query_params(options) : options | |
params = nil if params and params.blank? | |
url = BASE_URL + '/' + method_name.split('.').join('/') | |
url += ".#{FORMAT}" | |
url += "?#{params}" if params | |
url = URI.escape(url) | |
url | |
end | |
def parse_response(response) | |
raise_errors(response) | |
Crack::JSON.parse(response.body) | |
end | |
def to_query_params(options) | |
options.collect { |key, value| "#{key}=#{value}" }.join('&') | |
end | |
def get(url) | |
parse_response(@oauth.access_token.get(url)) | |
end | |
def post(url, body) | |
parse_response(@oauth.access_token.post(url, body)) | |
end | |
#API-SPECIFIC CALLS GO HERE DAWG | |
private | |
def raise_errors(response) | |
message = "(#{response.code}): #{response.message} - #{response.inspect} - #{response.body}" | |
case response.code.to_i | |
when 400 | |
raise BadRequest, message | |
when 401 | |
raise Unauthorized, message | |
when 403 | |
raise General, message | |
when 404 | |
raise NotFound, message | |
when 500 | |
raise InternalError, "Foodspotting broke something. Try again later or double check the API documentation." | |
when 502..503 | |
raise Unavailable, message | |
end | |
end | |
end | |
class BadRequest < StandardError; end | |
class Unauthorized < StandardError; end | |
class General < StandardError; end | |
class Unavailable < StandardError; end | |
class InternalError < StandardError; end | |
class NotFound < StandardError; end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment