Overview

Build status Gem Version

Synopsis

This gem provides a wrapper around HasOffers API in version 3, HasOffers APIv3 Documentation.

Compatibility

Supported ruby versions:

  • 2.2
  • 2.3
  • 2.4

Installation

gem install hasoffersv3

Usage

First, check if the method you want to call is already defined in hasoffersv3/lib/hasoffersv3/*. If not, you will need to add the method yourself (either just use your fork or submit a PR with your changes).

Next, create an initializer in your project in config/intializers:

HasOffersV3.configure do |config|
  config.api_key      = 'Your HasOffers API Key'
  config.network_id   = 'Your HasOffers Network ID'
  config.read_timeout = 10
  config.raise_errors = true # add this if you want the hasoffersv3 to raise errors upon detected API error messages in responses; defaults to `false`

  # Optionally configure a proxy:
  config.proxy_host   = 'yourproxy.com'
  config.proxy_port   = 8080
end

You can now use the defined methods, following this pattern:

HasOffersV3::ControllerName.snake_case_method_name

If HasOffers method does not take any parameters, then API also doesn't take them, otherwise it should be always a hash.

Naming is the same as in HasOffers documentation, also if it requires attributes then API will raise an exception if it's missing.

Examples:

HasOffersV3::Affiliate.update_payment_method_wire({ affiliate_id: '877', data: {} })

or when working with forms:

HasOffersV3::Advertiser.({
  account: {
    company: params[:company_name],
    country: params[:company_country],
    address1: 'Change me 123',
    zipcode: '123123'
  },
  user: {
    first_name: params[:user_name],
    email: params[:user_email],
    password: params[:user_password],
    password_confirmation: params[:user_password_confirmation]
  },
  return_object: 1
})

Error handling

If config.raise_errors was set to true, the hasoffersv3 will raise internal exceptions when error occurs at protocol or business logic level. See the HasOffersV3::Error and its descendants' definitions for more comments and details.

Logging

To enable log you can set a logger in configuration. All HTTP requests and responses will be logged.

To disable it, just set the logger configuration to nil value. Default is disabled.

An example setting Rails logger:

HasOffersV3.configure do |config|
  config.logger = Rails.logger
end

Testing

If RAILS_ENV or RACK_ENV is set to test, or there's a TEST environment variable, it will require the HasOffersV3::Testing module and enable testing mode. In testing mode all requests will return stubbed successful response with empty data set.

When you need to disable testing mode:

HasOffersV3::Testing.disable!

When you want to provide custom stub:

HasOffersV3::Testing.stub_request status_code, body