0.0
No release in over a year
A basic adaptor to send HTTP requests and parse the responses. Intended to bootstrap the quick writing of Adaptors for specific APIs, without having to write the same old JSON request and processing time and time again.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 13.0
~> 3.0
~> 1.21
~> 0.9
~> 3.18

Runtime

 Project Readme

ApiAdaptor

A basic adaptor to send HTTP requests and parse the responses. Intended to bootstrap the quick writing of Adaptors for specific APIs, without having to write the same old JSON request and processing time and time again.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add api_adaptor

If bundler is not being used to manage dependencies, install the gem by executing:

gem install api_adaptor

Usage

Use the ApiAdaptor as a base class for your API wrapper, for example:

  class MyApi < ApiAdaptor::Base; end

Use your new class to create a client that can make HTTP requests to JSON APIs for:

client = MyApi.new
client.get_json("http://some.endpoint/json")
client.post_json("http://some.endpoint/json", { "foo": "bar" })
client.put_json("http://some.endpoint/json", { "foo": "bar" })
client.patch_json("http://some.endpoint/json", { "foo": "bar" })
client.delete_json("http://some.endpoint/json", { "foo": "bar" })

You can also get a raw response from the API

client.get_raw("http://some.endpoint/json")

Conventional usage

An example of how to use this repository to bootstrap an API can be found in the WikiData REST adaptor it was built for.

A REST API module can be created with:

module MyApiAdaptor
  # Wikidata REST API class
  class RestApi < ApiAdaptor::Base
    def get_foo(foo_id)
      get_json("#{endpoint}/foo/#{CGI.escape(foo_id)}")
    end
  end
end

and can be wrapped in a top level module:

module MyApiAdaptor
  class Error < StandardError; end

  def self.rest_endpoint
    ENV["MYAPI_REST_ENDPOINT"] || "https://example.com"
  end

  def self.rest_api
    MyApiAdaptor::RestApi.new(rest_endpoint)
  end
end

The intended convention is to have test helpers ship alongside the actual Adaptor code. See WikiData examples here. This allows other applications that integrate the API Adaptor to easily mock out calls and receive representative data back.

Environment variables

User Agent is populated with a default string. See .env.example.

For instance if you provide:

APP_NAME=test_app
APP_VERSION=1.0.0
APP_CONTACT=contact@example.com

User agent would read

test_app/1.0.0 (contact@example.com)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/huwd/api_adaptor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ApiAdaptor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.