Class: Elrond::Api::Wallet::Client
- Inherits:
-
Object
- Object
- Elrond::Api::Wallet::Client
- Includes:
- Endpoints::Address, Endpoints::Transactions
- Defined in:
- lib/elrond/api/wallet/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #delete(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #get(path, parameters: {}, headers: {}, options: {}) ⇒ Object
-
#initialize(proxy: nil, user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36", configuration: ::Elrond::Api.configuration, options: {}) ⇒ Client
constructor
A new instance of Client.
- #log(tag = self.class.name, message) ⇒ Object
- #patch(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #post(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #put(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, retries: 3) ⇒ Object
- #set_connection ⇒ Object
- #set_headers ⇒ Object
Methods included from Endpoints::Transactions
Methods included from Endpoints::Address
Constructor Details
#initialize(proxy: nil, user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36", configuration: ::Elrond::Api.configuration, options: {}) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 |
# File 'lib/elrond/api/wallet/client.rb', line 8 def initialize(proxy: nil, user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36", configuration: ::Elrond::Api.configuration, options: {}) self.proxy = proxy self.user_agent = user_agent self.configuration = configuration set_headers set_connection end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
6 7 8 |
# File 'lib/elrond/api/wallet/client.rb', line 6 def configuration @configuration end |
#connection ⇒ Object
Returns the value of attribute connection.
6 7 8 |
# File 'lib/elrond/api/wallet/client.rb', line 6 def connection @connection end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/elrond/api/wallet/client.rb', line 6 def headers @headers end |
#proxy ⇒ Object
Returns the value of attribute proxy.
6 7 8 |
# File 'lib/elrond/api/wallet/client.rb', line 6 def proxy @proxy end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
6 7 8 |
# File 'lib/elrond/api/wallet/client.rb', line 6 def user_agent @user_agent end |
Instance Method Details
#delete(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
56 57 58 |
# File 'lib/elrond/api/wallet/client.rb', line 56 def delete(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :delete, parameters: parameters, data: data, headers: headers, options: end |
#get(path, parameters: {}, headers: {}, options: {}) ⇒ Object
40 41 42 |
# File 'lib/elrond/api/wallet/client.rb', line 40 def get(path, parameters: {}, headers: {}, options: {}) request path, method: :get, parameters: parameters, headers: headers, options: end |
#log(tag = self.class.name, message) ⇒ Object
93 94 95 |
# File 'lib/elrond/api/wallet/client.rb', line 93 def log(tag = self.class.name, ) puts "[#{tag}] - #{Time.now}: #{}" if self.configuration.verbose end |
#patch(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
52 53 54 |
# File 'lib/elrond/api/wallet/client.rb', line 52 def patch(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :patch, parameters: parameters, data: data, headers: headers, options: end |
#post(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
44 45 46 |
# File 'lib/elrond/api/wallet/client.rb', line 44 def post(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :post, parameters: parameters, data: data, headers: headers, options: end |
#put(path, parameters: {}, data: {}, headers: {}, options: {}) ⇒ Object
48 49 50 |
# File 'lib/elrond/api/wallet/client.rb', line 48 def put(path, parameters: {}, data: {}, headers: {}, options: {}) request path, method: :put, parameters: parameters, data: data, headers: headers, options: end |
#request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, retries: 3) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/elrond/api/wallet/client.rb', line 60 def request(path, method: :get, parameters: {}, data: {}, headers: {}, options: {}, retries: 3) response = nil response = case method when :get self.connection.get do |request| request.url path request.headers = self.connection.headers.merge(headers) request.params = parameters if parameters && !parameters.empty? end when :head self.connection.head do |request| request.url path request.headers = self.connection.headers.merge(headers) request.params = parameters if parameters && !parameters.empty? end when :post, :put, :patch, :delete self.connection.send(method) do |request| request.url path request.headers = self.connection.headers.merge(headers) request.body = data if data && !data.empty? request.params = parameters if parameters && !parameters.empty? end end return response end |
#set_connection ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/elrond/api/wallet/client.rb', line 17 def set_connection self.connection = ::Faraday.new(self.configuration.endpoints.dig(:wallet, :host)) do |builder| builder.[:timeout] = self.configuration.faraday.fetch(:timeout, nil) if self.configuration.faraday.fetch(:timeout, nil) builder.[:open_timeout] = self.configuration.faraday.fetch(:open_timeout, nil) if self.configuration.faraday.fetch(:open_timeout, nil) builder.headers = self.headers if self.headers && !self.headers.empty? builder.request :json builder.response :logger, ::Logger.new(STDOUT), bodies: true if self.configuration.verbose builder.response :json, content_type: /\bjson$/ builder.use ::FaradayMiddleware::FollowRedirects, limit: 10 if self.proxy builder.proxy = self.proxy log("Will use proxy: #{builder.proxy.inspect}") end builder.adapter self.configuration.faraday.fetch(:adapter, ::Faraday.default_adapter) end end |
#set_headers ⇒ Object
88 89 90 91 |
# File 'lib/elrond/api/wallet/client.rb', line 88 def set_headers self.headers = self.configuration.endpoints.dig(:wallet, :headers) self.headers["User-Agent"] = self.user_agent unless self.user_agent.to_s.empty? end |