class Fbox::Client

Constants

DEFAULT_OPTIONS
FBOX_API

Attributes

options[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/fbox/client.rb, line 51
def initialize(options={})
  @@addres_validator = CoinsAddressValidator::Validator.new
  options = DEFAULT_OPTIONS.merge(options)
  @options = options
  @options[:rest_base_path] = @options[:context_path] + @options[:rest_base_path]

  @request_client = HttpClient.new(@options)

  @options.freeze
end

Public Instance Methods

balance(currency = '') click to toggle source
# File lib/fbox/client.rb, line 62
def balance(currency = '')
  path = prepare_api_request(FBOX_API[:balance])
  form_params = {
    'api_key' => @options[:api_key],
    'currency' => !currency.empty? ? currency : 'BTC'
  }
  response = request(path, form_params)
  process_response(response)
end
currencies() click to toggle source
# File lib/fbox/client.rb, line 90
def currencies()
  path = prepare_api_request(FBOX_API[:currencies])
  response = request(path)
  process_response(response)
end
is_address_valid?(address) click to toggle source
# File lib/fbox/client.rb, line 117
def is_address_valid?(address)
  @@addres_validator.is_address_valid?(address)
end
is_response_ok?(body) click to toggle source
# File lib/fbox/client.rb, line 113
def is_response_ok?(body)
  body['status'].present? && body['status'] == 200
end
payment(to, amount, referral = false, currency = '') click to toggle source
# File lib/fbox/client.rb, line 96
def payment(to, amount, referral = false, currency = '')
  if !to.empty? && self.is_address_valid?(to) && amount > 0
    path = prepare_api_request(FBOX_API[:payment])
    form_params = {
      'api_key' => @options[:api_key],
      'to' => to,
      'amount' => amount,
      'currency' => !currency.empty? ? currency : 'BTC',
      'referral' => referral.to_s
    }
    response = request(path, form_params)
    process_response(response)
  else
    nil
  end
end
payouts(count = 1, currency = '') click to toggle source

Use with care it gives timeouts, probably this API call is disabled on FaucetBox

# File lib/fbox/client.rb, line 75
def payouts(count = 1, currency = '')
  if count.to_i > 0 && count <= 10
    path = prepare_api_request(FBOX_API[:payouts])
    form_params = {
      'api_key' => @options[:api_key],
      'count' => count,
      'currency' => !currency.empty? ? currency : 'BTC'
    }
    response = request(path, form_params)
    process_response(response)
  else
    invalid_parameter("'count' parameter has to be in 1..10 range")
  end
end

Private Instance Methods

default_headers() click to toggle source
# File lib/fbox/client.rb, line 142
def default_headers()
  { 'Content-type' => 'application/x-www-form-urlencoded' }
end
invalid_parameter(message) click to toggle source
# File lib/fbox/client.rb, line 122
def invalid_parameter(message)
  { :error => message }.to_json
end
prepare_api_request(api) click to toggle source
# File lib/fbox/client.rb, line 134
def prepare_api_request(api)
  @options[:site] + @options[:rest_base_path] + api
end
process_response(response) click to toggle source
# File lib/fbox/client.rb, line 126
def process_response(response)
  if response.code == '200'
    JSON.parse(response.body)
  else
    { :status => response.code.to_i, :message => response.msg.to_s }.to_json
  end
end
request(path, form_params = {}) click to toggle source
# File lib/fbox/client.rb, line 138
def request(path, form_params = {})
  @request_client.request(:post, path, '', default_headers(), form_params)
end