Class: Peatio::Nem2::Client
- Inherits:
-
Object
- Object
- Peatio::Nem2::Client
- Defined in:
- lib/peatio/nem2/client.rb
Defined Under Namespace
Classes: ResponseError
Constant Summary collapse
- Error =
Class.new(StandardError)
- ConnectionError =
Class.new(Error)
Instance Method Summary collapse
-
#initialize(endpoint) ⇒ Client
constructor
A new instance of Client.
- #rest_api(verb, path, data = nil) ⇒ Object
Constructor Details
#initialize(endpoint) ⇒ Client
Returns a new instance of Client.
16 17 18 |
# File 'lib/peatio/nem2/client.rb', line 16 def initialize(endpoint) @endpoint = URI.parse(endpoint) end |
Instance Method Details
#rest_api(verb, path, data = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/peatio/nem2/client.rb', line 20 def rest_api(verb, path, data = nil) args = [@endpoint.to_s + path] if data if %i[post put patch].include?(verb) args << data.compact.to_json args << { 'Content-Type' => 'application/json' } else args << data.compact args << {} end else args << nil args << {} end args.last['Accept'] = 'application/json' response = Faraday.send(verb, *args) response.assert_success! response = JSON.parse(response.body) response['error'].tap { |error| raise ResponseError.new(error) if error } response rescue Faraday::Error => e if e.is_a?(Faraday::ConnectionFailed) || e.is_a?(Faraday::TimeoutError) raise ConnectionError, e else raise ConnectionError, JSON.parse(e.response.body)['message'] end end |