class CheckMobi::Client

Constants

ALLOWED_METHODS

Attributes

endpoint[R]
headers[R]
request[R]
response[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/check_mobi/client.rb, line 20
def initialize(options = {})
  @endpoint = URI(Configuration::DEFAULT_ENDPOINT +
                      options.fetch(:rel_path, '/'))

  @request = Net::HTTP.const_get(options.fetch(:http_method, :get)
                                     .to_s.capitalize).new(@endpoint)
  set_headers
  set_body options.fetch(:form_data, {})
end

Public Instance Methods

body() click to toggle source
# File lib/check_mobi/client.rb, line 40
def body
  OpenStruct.new(JSON.parse(response.body))
end
is_successful() click to toggle source
# File lib/check_mobi/client.rb, line 44
def is_successful
  code === /^"20\d"$/
end
perform() click to toggle source
# File lib/check_mobi/client.rb, line 30
def perform
  @request.initialize_http_header(@headers)

  @response = Net::HTTP.start(@endpoint.hostname, @endpoint.port, use_ssl: true) do |http|
    http.request(@request)
  end

  handle_response
end

Private Instance Methods

handle_response() click to toggle source
# File lib/check_mobi/client.rb, line 61
def handle_response
  CheckMobi::Response.new self
end
set_body(form_data) click to toggle source
# File lib/check_mobi/client.rb, line 57
def set_body(form_data)
  @request.body = form_data.to_json if @request.request_body_permitted?
end
set_headers() click to toggle source
# File lib/check_mobi/client.rb, line 50
def set_headers
  @headers = {}
  @headers['Content-Type'] = CheckMobi.content_type
  @headers['Accept'] = CheckMobi.accept_type
  @headers['Authorization'] = CheckMobi.api_key
end