AbstractApiWrapper
An abstract REST API wrapper based on missing_method
magic.
Installation
Add this line to your application's Gemfile:
gem 'abstract_api_wrapper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install abstract_api_wrapper
Usage
Define a new class and extend it with AbstractApiWrapper
, then Modify the Request#endpoint
and/or Request#headers
methods to fit your requirements.
# Basecamp example
class BasecampApi < AbstractApiWrapper
class Client < BasecampApi::Client
def method_missing(name, *params, &block)
BasecampApi::Request.new(name.to_s, self)
end
end
class Request < BasecampApi::Request
def headers
# Use Bearer instead of token
{
'Authorization' => "Bearer #{@client.access_token}",
'Content-Type' => 'application/json'
}
end
def endpoint
# Adds the .json at the end of each request
query_string = filters.map { |k, v| "#{k}=#{v}" }.join('&')
"#{@client.base_url}/#{path.join('/')}.json?#{query_string}"
end
end
end
ACCESS_TOKEN = "YOUR ACCESS TOKEN"
client = BasecampApi::Client.new(
access_token: ACCESS_TOKEN,
base_url: 'https://launchpad.37signals.com'
)
authorizations = client.authorization.all.run
# => <BasecampApi::Response::Resource accounts=#<Hashie::Array [#<BasecampApi::Response::Resource app_href="https://basecamp.com/xxxxxxx" href="https://basecamp.com/xxxxxxx/api/v1" id=xxxxxxx name="Your Company" product="bcx">, #<BasecampApi::Response::Resource app_href="https://basecamp.com/xxxxxxx" href="https://basecamp.com/xxxxxxx/api/v1" id=xxxxxxx name="Another company" product="bcx">]> expires_at="2016-12-19T18:13:52.000Z" identity=#<BasecampApi::Response::Resource email_address="email@yourcompany.com" first_name="Juan" id=xxxxxx last_name="Puelpan">>
account = authorizations.accounts.first
client = BasecampApi::Client.new(access_token: ACCESS_TOKEN, base_url: account.href)
projects = client.projects.all.run
puts projects
puts projects.pagination
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/octopull/abstract_api_wrapper.