Class: Cryptomarket::HttpManager
- Inherits:
-
Object
- Object
- Cryptomarket::HttpManager
- Defined in:
- lib/cryptomarket/http_manager.rb
Overview
Manager of http requests to the cryptomarket server
Constant Summary collapse
- @@API_URL =
rubocop:disable Naming/VariableName,Style/ClassVars
'https://api.exchange.cryptomkt.com'
- @@API_VERSION =
rubocop:disable Naming/VariableName,Style/ClassVars
'/api/3/'
Instance Method Summary collapse
- #build_headers(method, endpoint, params, public) ⇒ Object
- #build_payload(params) ⇒ Object
- #build_post_headers(endpoint, params) ⇒ Object
- #do_request(method, uri, payload, headers) ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize(api_key:, api_secret:, window: nil) ⇒ HttpManager
constructor
A new instance of HttpManager.
- #make_post_request(method:, endpoint:, params: nil) ⇒ Object
- #make_request(method:, endpoint:, params: nil, public: false) ⇒ Object
Constructor Details
#initialize(api_key:, api_secret:, window: nil) ⇒ HttpManager
Returns a new instance of HttpManager.
34 35 36 37 38 |
# File 'lib/cryptomarket/http_manager.rb', line 34 def initialize(api_key:, api_secret:, window: nil) @credential_factory = Cryptomarket::CredentialsFactory.new( api_version: @@API_VERSION, api_key: api_key, api_secret: api_secret, window: window ) end |
Instance Method Details
#build_headers(method, endpoint, params, public) ⇒ Object
57 58 59 60 61 |
# File 'lib/cryptomarket/http_manager.rb', line 57 def build_headers(method, endpoint, params, public) return {} if public { 'Authorization' => @credential_factory.get_credential(method.upcase, endpoint, params) } end |
#build_payload(params) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/cryptomarket/http_manager.rb', line 63 def build_payload(params) return nil if params.nil? payload = params.compact payload = Hash[payload.sort_by { |key, _val| key.to_s }] if payload.is_a?(Hash) payload end |
#build_post_headers(endpoint, params) ⇒ Object
82 83 84 85 |
# File 'lib/cryptomarket/http_manager.rb', line 82 def build_post_headers(endpoint, params) { 'Content-Type' => 'application/json', 'Authorization' => @credential_factory.get_credential('POST'.upcase, endpoint, params.to_json) } end |
#do_request(method, uri, payload, headers) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cryptomarket/http_manager.rb', line 71 def do_request(method, uri, payload, headers) args = { method: method.downcase.to_sym, url: uri.to_s, headers: headers } if post?(method) || patch?(method) args[:payload] = post?(method) ? payload.to_json : payload end response = RestClient::Request.execute(**args) handle_response(response) rescue RestClient::ExceptionWithResponse => e handle_response(e.response) end |
#handle_response(response) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cryptomarket/http_manager.rb', line 87 def handle_response(response) result = response.body parsed_result = JSON.parse result if (response.code != 200) && !parsed_result['error'].nil? error = parsed_result['error'] exception = Cryptomarket::APIException.new error raise exception end parsed_result end |
#make_post_request(method:, endpoint:, params: nil) ⇒ Object
51 52 53 54 55 |
# File 'lib/cryptomarket/http_manager.rb', line 51 def make_post_request(method:, endpoint:, params: nil) uri = URI(@@API_URL + @@API_VERSION + endpoint) payload = build_payload(params) do_request(method, uri, payload, build_post_headers(endpoint, payload)) end |
#make_request(method:, endpoint:, params: nil, public: false) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cryptomarket/http_manager.rb', line 40 def make_request(method:, endpoint:, params: nil, public: false) uri = URI(@@API_URL + @@API_VERSION + endpoint) payload = build_payload(params) headers = build_headers(method, endpoint, payload, public) if ((method.upcase == 'GET') || (method.upcase == 'PUT')) && !payload.nil? uri.query = URI.encode_www_form payload payload = nil end do_request(method, uri, payload, headers) end |