Last active
August 29, 2015 14:07
-
-
Save kerinin/059469b4bd5cde4b1636 to your computer and use it in GitHub Desktop.
CIO Ruby Lite
This file contains hidden or 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 'faraday' | |
require 'faraday_middleware' | |
class ContextIO | |
attr_reader :key, :secret | |
def initialize(key, secret) | |
@key = key | |
@secret = secret | |
end | |
def request(method, path, params) | |
normalized_params = params.inject({}) do |normalized_params, (key, value)| | |
normalized_params[key.to_sym] = value | |
normalized_params | |
end | |
connection.send(method, path, normalized_params) | |
end | |
private | |
def connection | |
@connection ||= Faraday::Connection.new('https://api.context.io') do |faraday| | |
faraday.headers['Accept'] = 'application/json' | |
faraday.request :oauth, consumer_key: key, consumer_secret: secret | |
faraday.request :url_encoded | |
faraday.adapter Faraday.default_adapter | |
faraday.use FaradayMiddleware::ParseJson | |
end | |
end | |
end | |
client = ContextIO.new('DEVELOPER_KEY', 'DEVELOPER_SECRET') | |
response = client.request(:get, 'https://api.context.io/lite/users/', {}) | |
puts response.body # => should look identical to data structure in context.io documentation sample response JSON |
This file contains hidden or 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
gem 'faraday' | |
gem 'faraday_middleware' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment