Skip to content

Instantly share code, notes, and snippets.

@imajes
Created August 9, 2014 21:10
Show Gist options
  • Save imajes/2c8d981edc04165c2c55 to your computer and use it in GitHub Desktop.
Save imajes/2c8d981edc04165c2c55 to your computer and use it in GitHub Desktop.
module Quill
class Client
module Transport
class InvalidKeys < Exception ; end
attr_accessor :token, :client, :config
delegate :get, :post, :put, to: :token
include EndpointDefinitions
def initialize options = {}
@config = Configuration.new(options, api_url: 'http://api.quill.org/')
# raise ArgumentError, 'Missing access_token' if config.access_token.blank?
end
def token
@token ||= OAuth2::AccessToken.new client, config.access_token
end
def client
@client ||= OAuth2::Client.new(config.client_id, config.client_secret, site: config.api_url, raise_errors: false) do |conn|
conn.request :json
conn.response :json
conn.adapter Faraday.default_adapter
end
end
def get path, params = {}
request :get, path, params
end
def post path, params = {}
request :post, path, params
end
def put path, params = {}
request :put, path, params
end
def request verb, path, params = {}
key = if verb == :get then :params else :body end
response = token.request verb, path, key => params
raise response.error if response.status == 401
response
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment