Class: CheckMobi::Client
- Inherits:
-
Object
- Object
- CheckMobi::Client
- Extended by:
- Forwardable
- Defined in:
- lib/check_mobi/client.rb
Constant Summary collapse
- ALLOWED_METHODS =
%i[get post delete].freeze
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #body ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #is_successful ⇒ Object
- #perform ⇒ Object
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( = {}) @endpoint = URI(Configuration::DEFAULT_ENDPOINT + .fetch(:rel_path, '/')) @request = Net::HTTP.const_get(.fetch(:http_method, :get) .to_s.capitalize).new(@endpoint) set_headers set_body .fetch(:form_data, {}) end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
16 17 18 |
# File 'lib/check_mobi/client.rb', line 16 def endpoint @endpoint end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
16 17 18 |
# File 'lib/check_mobi/client.rb', line 16 def headers @headers end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
16 17 18 |
# File 'lib/check_mobi/client.rb', line 16 def request @request end |
#response ⇒ Object (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
#body ⇒ Object
40 41 42 |
# File 'lib/check_mobi/client.rb', line 40 def body OpenStruct.new(JSON.parse(response.body)) end |
#is_successful ⇒ Object
44 45 46 |
# File 'lib/check_mobi/client.rb', line 44 def is_successful code === /^"20\d"$/ end |
#perform ⇒ Object
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 |