Skip to content

ApiClient for simple api calls integrated with api-auth gem

Notifications You must be signed in to change notification settings

Mifiel/api-auth-client

Repository files navigation

ApiAuth::Client

Gem Version Build Status Coverage Status security

Use this gem to create simple API Client classes.

Installation

Add this line to your application's Gemfile:

gem 'api_auth-client'

And then execute:

$ bundle

Or install it yourself as:

$ gem install api_auth-client

Usage

class VolabitClient < ApiAuth::Client::Base
  connect url: 'https://www.volabit.com/api/v1',
          app_id: 'some-app-id',
          secret_key: 'some-app-secret'

  def tickers
    connection.get('/tickers')
  end

  # bang method, it will raise an error if it fails
  def tickers!
    connection.get!('/tickers')
  end

  # reqires auth
  def me
    connection.get('/users/me')
  end
end

Access to the response

client = VolabitClient.new
response = client.tickers
#<ApiAuth::Client::Response btc_mxn_buy="123255.81", btc_mxn_sell="127187.57", ltc_mxn_buy="999.96", ltc_mxn_sell="1033.07", bch_mxn_buy="10522.97", bch_mxn_sell="10873.06", xrp_mxn_buy="9.58", xrp_mxn_sell="9.9">
response[:btc_mxn_buy] == response['btc_mxn_buy'] == btc_mxn_buy.btc_mxn_buy

response[:unknown] == response['unknown'] == nil
response.unknown # raises NoMethodError

Bang methods, RoR style

You can use bang ! methods if you want the method to raise an error if the requests fails due to server error, bad requests or a bad connection.

client = VolabitClient.new
begin
  response = client.tickers!
rescue ApiAuth::Client::ConnectionError =>
  e # => #<ApiAuth::Client::ConnectionError: Connection Error>
  e.response
  # { message: 'Failed to open TCP connection' }
  e.code # nil
rescue ApiAuth::Client::ApiEndpointError => e
  e # => #<ApiAuth::Client::ApiEndpointError: 500 Internal Server Error>
  e.response
  # <ApiAuth::Client::Response error="Bad JSON", body="500 Internal Server Error...">
  e.response.body
  e.code # 400
end

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_auth-client.

About

ApiClient for simple api calls integrated with api-auth gem

Resources

Stars

Watchers

Forks

Packages

No packages published