Class: SmsActivate::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/sms_activate/client.rb

Constant Summary collapse

STATUSES =

Sets the status of the activation

{
    cancelled:   -1, # Cancels the activation
    sms_sent:     1, # Tells that SMS message has been sent to the number
    on_retry:    3,  # Requests one more code (for free)
    finished:    6,  # Finishes the activation
    number_used: 8   # Complains that number is already used and cancels the activation
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Initializes a new SmsActivate Client

Parameters:



13
14
15
# File 'lib/sms_activate/client.rb', line 13

def initialize(api_key)
  @options = { query: { api_key: api_key } }
end

Instance Method Details

#get_activation_status(id) ⇒ Object

Returns the activation status

Examples:

SmsActivate::Client.new('key').get_activation_status(42)
=> OpenStruct status=:success code="12345"


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sms_activate/client.rb', line 105

def get_activation_status(id)
  response = self.class.get('/', query: { action: 'getStatus', id: id }.merge(@options[:query]))

  check_errors! response
  case response.parsed_response
    when 'STATUS_WAIT_CODE'
      OpenStruct.new(status: :waiting)
    when 'STATUS_WAIT_RESEND'
      OpenStruct.new(status: :waiting_for_resend)
    when 'STATUS_CANCEL'
      OpenStruct.new(status: :cancelled)
    when /STATUS_WAIT_RETRY/
      OpenStruct.new(status: :waiting_for_code_confirmation, lastcode: response.parsed_response.split(':')[1])
    when /STATUS_OK/
      OpenStruct.new(status: :success, code: response.parsed_response.split(':')[1])
    else
      raise ServerError('Bad activation response')
  end
end

#get_available_services(full = true) ⇒ Object

Returns an amount of available numbers for each service

Examples:

SmsActivate::Client.new('key').get_available_services
=> {"vk_0"=>{"quant"=>"0", "cost"=>10}, ...}

SmsActivate::Client.new('key').get_available_services(false)
=> {"vk_0"=>"0", "ok_0"=>"42", ...}

Parameters:

  • full (Boolean) (defaults to: true)

    Whether to include “cost” and “quant” keys



28
29
30
31
32
33
34
# File 'lib/sms_activate/client.rb', line 28

def get_available_services(full = true)
  action = full ? 'getNumbersStatus1' : 'getNumbersStatus'
  response = self.class.get('/', query: { action: action }.merge(@options[:query]))

  check_errors! response
  JSON.parse(response.parsed_response)
end

#get_balanceObject

Returns user’s balance in RUB

Examples:

SmsActivate::Client.new('key').get_balance
=> 0.0


41
42
43
44
45
46
# File 'lib/sms_activate/client.rb', line 41

def get_balance
  response = self.class.get('/', query: { action: 'getBalance' }.merge(@options[:query]))

  check_errors! response
  response.parsed_response.split(':')[1].to_f
end

#obtain_number(service) ⇒ Object

Obtains a number for the given service

TODO: forward, operator

Examples:

SmsActivate::Client.new('key').obtain_number('ya')
=> OpenStruct id=42 number="+1234567890"


56
57
58
59
60
61
62
# File 'lib/sms_activate/client.rb', line 56

def obtain_number(service)
  response = self.class.get('/', query: { action: 'getNumber', service: service }.merge(@options[:query]))
  check_errors! response

  splitted = response.parsed_response.split(':')
  OpenStruct.new(id: splitted[1].to_i, number: splitted[2])
end

#set_activation_status(id, status) ⇒ Object

TODO: forward

Examples:

SmsActivate::Client.new('key').set_activation_status(42, :sms_sent)
=> OpenStruct status=:confirmed

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sms_activate/client.rb', line 80

def set_activation_status(id, status)
  raise BadStatusError unless (status = STATUSES[status])
  response = self.class.get('/', query: { action: 'setStatus', id: id, status: status }.merge(@options[:query]))

  check_errors! response
  case response.parsed_response
    when 'ACCESS_READY'
      OpenStruct.new(status: :confirmed)
    when 'ACCESS_RETRY_GET'
      OpenStruct.new(status: :retrying)
    when 'ACCESS_ACTIVATION'
      OpenStruct.new(status: :activated)
    when 'ACCESS_CANCEL'
      OpenStruct.new(status: :cancelled)
    else
      raise ServerError('Bad activation response')
  end
end