Created
January 5, 2013 07:45
-
-
Save sent-hil/4460391 to your computer and use it in GitHub Desktop.
Refactoring of Saran's `Endpoint` class based on Sandi Metz's talk/book.
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
module Saran | |
# Makes http requests. | |
class Endpoint | |
attr_reader :verb, :path, :provider, :endpoint, :access_token | |
# TODO: remove config dependency, | |
# can combine path, endpoint, access_token | |
# depends on if OpenAuth can deal with full_url | |
# how to deal with provider? | |
# use 'default' for all requests? | |
# user will've to build post queries themselves then | |
# | |
# how close to couple saran to open_auth2? | |
# will i ever remove open_auth2? possibly | |
# | |
# how to deal with body encoded params? | |
# | |
# Examples: | |
# Endpoint.new(:get, 'http://graph.facebook.com/me/feed') | |
# Endpoint.new(:post, 'http://graph.facebook.com/me/feed?message=hi') | |
# | |
# Endpoint.new(:post, 'http://google.com', JSON.parse('hi')) | |
# | |
def initialize(verb, path, config) | |
@verb = verb | |
@path = path | |
@provider = config.provider || :default | |
@endpoint = config.endpoint | |
@access_token = config.access_token | |
end | |
# TODO: dependency to client's get/post methods, | |
# knows about :path argument | |
# | |
# Examples: | |
# ep = Endpoint.new(:get, 'http://graph.facebook.com/me/feed') | |
# ep.fetch(:access_token => '1') | |
# | |
# ep = Endpoint.new(:post, 'http://google.com') | |
# ep.fetch(:body => JSON.parse('hi'), :header => 'application/json') | |
# | |
# TODO: hash key name dependency | |
# varies by method: | |
# get encodes anything, | |
# post accepts only body and header | |
# | |
# problem with open_auth2 api? | |
# how many diff. args does open_auth2 accept? | |
# params, header, body, what else? | |
# | |
def fetch | |
client.send(verb, :path => path) | |
end | |
private | |
# TODO: should there OpenAuth2 dependency here? | |
# yes, only in one place | |
# | |
def client | |
config = OpenAuth2::Config.new do |c| | |
c.provider = provider | |
c.endpoint = endpoint | |
c.access_token = access_token | |
end | |
OpenAuth2::Client.new do |c| | |
c.config = config | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment