Class: CheckMobi::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/check_mobi/client.rb

Constant Summary collapse

ALLOWED_METHODS =
%i[get post delete].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



16
17
18
# File 'lib/check_mobi/client.rb', line 16

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



16
17
18
# File 'lib/check_mobi/client.rb', line 16

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



16
17
18
# File 'lib/check_mobi/client.rb', line 16

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



16
17
18
# File 'lib/check_mobi/client.rb', line 16

def response
  @response
end

Instance Method Details

#bodyObject



40
41
42
# File 'lib/check_mobi/client.rb', line 40

def body
  OpenStruct.new(JSON.parse(response.body))
end

#is_successfulObject



44
45
46
# File 'lib/check_mobi/client.rb', line 44

def is_successful
  code === /^"20\d"$/
end

#performObject



30
31
32
33
34
35
36
37
38
# 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