Class: SmsActivate::Client
- Inherits:
-
Object
- Object
- SmsActivate::Client
- 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
-
#get_activation_status(id) ⇒ Object
Returns the activation status.
-
#get_available_services(full = true) ⇒ Object
Returns an amount of available numbers for each service.
-
#get_balance ⇒ Object
Returns user’s balance in RUB.
-
#initialize(api_key) ⇒ Client
constructor
Initializes a new SmsActivate Client.
-
#obtain_number(service) ⇒ Object
Obtains a number for the given service.
-
#set_activation_status(id, status) ⇒ Object
TODO: forward.
Constructor Details
#initialize(api_key) ⇒ Client
Initializes a new SmsActivate Client
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
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
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_balance ⇒ Object
Returns user’s balance in RUB
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
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
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 |