11 месяцев назад История
README.md

unigo-ruby

Unisender Go (https://go.unisender.ru) integration gem for Ruby

Examples of usage

Installation

gem install unigo-ruby

Working with API responses

This library using faraday gem for making HTTP queries along with mashify middleware for post-processing response body. So, you can use extended syntax for accessing response fields.

Example of work with responses:

require 'unigo-ruby'

unigo = UniGo::Client.new(
  hostname: 'go1.unisender.ru',
  lang: 'ru',
  api_key: ENV['UNIGO_API_KEY']
)

response = unigo.get_dns_records("example.com")

puts response.status
puts response.body.to_h
puts response.headers

# Access fields as hash keys or as object attributes:
puts response.body['status']
puts response.body.status

Passing API key in parameters

By default authentication sent through X-API-KEY header.

For passing API key in parameters, use:

unigo = UniGo::Client.new(
  hostname: 'go1.unisender.ru',
  lang: 'ru',
  api_key: ENV['UNIGO_API_KEY'],
  api_key_in_params: true
)

Verify webhook callback message

Will raise UniGo::Client::InvalidCallbackAuth error unless `auth’ field is correct.

unigo.verify_callback_auth!(params)

Webhook callback helper

Use it to substitute webhook handler verification and parsing to directly get list of events.

Will raise UniGo::Client::InvalidCallbackAuth error unless `auth’ field is correct.

unigo.callback_helper(params) do |events|
  # [
  #   {
  #     'event_name' => 'transactional_email_status',
  #     'event_data' =>
  #      {
  #        'email' => 'recipient.email@example.com',
  #        'status' => 'sent',
  #        'event_time' => '2015-11-30 15:09:42',
  #       ...
  #      }
  #   },
  #   ...
  # ]
end

Handling API errors

Library using `raise-error’ middleware for handling error responses.

Example of work with errors:

require 'unigo-ruby'

begin

  unigo = UniGo::Client.new(
    hostname: 'go1.unisender.ru',
    lang: 'ru',
    api_key: ENV['UNIGO_API_KEY']
  )

  response = unigo.get_dns_records("example.com")

  puts response.status
  puts response.body.to_h
  puts response.headers

rescue Faraday::Error => e

  # Note that exception response has internal format which is a little
  # bit different and body field is not post-processed

  puts e.response[:status]
  puts e.response[:body]
  puts e.response[:headers]

end

HTTP timeout configuration

Pass timeout: <seconds> parameter on creating client, default is 5.

HTTP logging

Pass enable_logging: true parameter on creating client to enable logging, default false.

Testing

You can run tests through included test task and passing methods for test and data inside YAML config file:

rake test DOMAIN APIKEY WEBHOOK_URL FROM_EMAIL ./test/CONFIGFILE.yml
Описание

Unisender Go Ruby Library

Конвейеры
0 успешных
0 с ошибкой