class Moyasar::HTTPClient
Constants
- DEFAULT_HEADERS
- METHOD_CLASS
Public Class Methods
new(endpoint)
click to toggle source
# File lib/moyasar/http_client.rb, line 15 def initialize(endpoint) uri = URI.parse(endpoint) @http = Net::HTTP.new(uri.host, uri.port) @http.use_ssl = true end
Public Instance Methods
request(method, path, key = nil, params = {}, headers = {})
click to toggle source
# File lib/moyasar/http_client.rb, line 21 def request(method, path, key = nil, params = {}, headers = {}) case method when :get full_path = encode_path_params(path, params) request = METHOD_CLASS[method.to_sym].new(full_path, DEFAULT_HEADERS) else request = METHOD_CLASS[method.to_sym].new(path, DEFAULT_HEADERS) # post_data = URI.encode_www_form(params) request.body = params.to_json end request.basic_auth(key, '') unless key.nil? @http.request(request) end
request_json(method, path, key = nil, params = {}, headers = {})
click to toggle source
# File lib/moyasar/http_client.rb, line 37 def request_json(method, path, key = nil, params = {}, headers = {}) response = request(method, path, key, params, headers) body = JSON.parse(response.body) OpenStruct.new(code: response.code.to_i, body: body) rescue JSON::ParserError response end
Private Instance Methods
encode_path_params(path, params)
click to toggle source
# File lib/moyasar/http_client.rb, line 48 def encode_path_params(path, params) encoded = URI.encode_www_form(params) [path, encoded].join('?') end