Skip to content

Instantly share code, notes, and snippets.

@kerinin
Last active August 29, 2015 14:07
Show Gist options
  • Save kerinin/059469b4bd5cde4b1636 to your computer and use it in GitHub Desktop.
Save kerinin/059469b4bd5cde4b1636 to your computer and use it in GitHub Desktop.
CIO Ruby Lite
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
gem 'faraday'
gem 'faraday_middleware'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment