No commit activity in last 3 years
No release in over 3 years
An abstract REST API wrapper based on missing_method magic
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.5
~> 1.24

Runtime

~> 0.9
~> 3.4
~> 1.8
 Project Readme

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.