class SmsAreaApi::Client

Attributes

api_key[RW]
service[RW]

Public Class Methods

new(api_key, service = 'sms_area') click to toggle source
# File lib/sms_area_api/api.rb, line 10
def initialize(api_key, service = 'sms_area')
  self.api_key = api_key
  self.service = service
end

Public Instance Methods

method_missing(method, **args) click to toggle source
# File lib/sms_area_api/api.rb, line 55
def method_missing(method, **args)
  args[:action] = method.to_s.camelize
  request(args)
end
request(params) click to toggle source
# File lib/sms_area_api/api.rb, line 19
def request(params)
  params[:api_key] = api_key
  response = RestClient.post self.service_endpoint, params

  if params[:action] == 'getNumbersStatus'
    return JSON.parse(response)

  else
    data = response.split(':')
    state = data.first

    case state
    when 'ACCESS_NUMBER'
      return {
        state: state,
        id: data[1],
        access_number: data[2]
      }
    when 'STATUS_OK', 'STATUS_ACCESS', 'STATUS_ACCESS_SCREEN'
      return {
        state: state,
        code: data[1]
      }
    when 'ACCESS_BALANCE'
      return {
        state: state,
        balance: data[1]
      }
    else
      return {
        state: state
      }
    end
  end
end
service_endpoint() click to toggle source
# File lib/sms_area_api/api.rb, line 15
def service_endpoint
  @@service_endpoints[service]
end