Class: Fbox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fbox/client.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :site               => 'https://faucetbox.com',
  :context_path       => '',
  :rest_base_path     => "/api/v1/",
  :ssl_verify_mode    => OpenSSL::SSL::VERIFY_PEER,
  :use_ssl            => true,
  :api_key            => '',
}
FBOX_API =
{
  :balance            => 'balance',
  :currencies         => 'currencies',
  :payment            => 'send',
  :payouts            => 'payouts',
}
@@addres_validator =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



51
52
53
54
55
56
57
58
59
60
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/fbox/client.rb', line 31

def options
  @options
end

Instance Method Details

#balance(currency = '') ⇒ Object



62
63
64
65
66
67
68
69
70
# 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

#currenciesObject



90
91
92
93
94
# 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) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/fbox/client.rb', line 117

def is_address_valid?(address)
  @@addres_validator.is_address_valid?(address)
end

#is_response_ok?(body) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# 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 = '') ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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 = '') ⇒ Object

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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