Class: Etherscan::Client
- Inherits:
-
Object
- Object
- Etherscan::Client
- Defined in:
- lib/etherscan/client.rb
Constant Summary collapse
- URL =
'https://api.etherscan.io/'.freeze
- HEADERS =
{'Content-Type' => 'application/json', 'Accept' => 'application/json'}.freeze
Instance Attribute Summary collapse
- #adapter ⇒ Object
- #conn ⇒ Object
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#raise_exceptions ⇒ Object
readonly
Returns the value of attribute raise_exceptions.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #get(params = {}) ⇒ Object
-
#initialize(params = {}) {|_self| ... } ⇒ Client
constructor
A new instance of Client.
- #raise_exceptions? ⇒ Boolean
Constructor Details
#initialize(params = {}) {|_self| ... } ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/etherscan/client.rb', line 12 def initialize( params = {} ) @key = params.fetch(:key, Etherscan.config.key) @url = params.fetch(:url, Etherscan.config.url || URL) @adapter = params.fetch(:adapter, adapter) @conn = params.fetch(:conn, conn) @user_agent = params.fetch(:user_agent, "etherscan/#{Etherscan::VERSION};ruby") @headers = HEADERS.merge('User-Agent' => @user_agent) @raise_exceptions = params.fetch(:raise_exceptions, Etherscan.config.raise_exceptions || true) yield self if block_given? end |
Instance Attribute Details
#adapter ⇒ Object
42 43 44 |
# File 'lib/etherscan/client.rb', line 42 def adapter @adapter ||= Faraday.default_adapter end |
#conn ⇒ Object
35 36 37 38 39 40 |
# File 'lib/etherscan/client.rb', line 35 def conn @conn ||= Faraday.new(url: @url) do |conn| conn.request :url_encoded conn.adapter adapter end end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/etherscan/client.rb', line 9 def headers @headers end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/etherscan/client.rb', line 9 def key @key end |
#raise_exceptions ⇒ Object (readonly)
Returns the value of attribute raise_exceptions.
9 10 11 |
# File 'lib/etherscan/client.rb', line 9 def raise_exceptions @raise_exceptions end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/etherscan/client.rb', line 9 def url @url end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
9 10 11 |
# File 'lib/etherscan/client.rb', line 9 def user_agent @user_agent end |
Instance Method Details
#get(params = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/etherscan/client.rb', line 23 def get(params = {}) endpoint = 'api' merged_params = params.merge({apikey: key}) response = conn.get(endpoint) do |req| req.headers = headers req.params = merged_params end fail Etherscan::Exception, response if raise_exceptions? && response.status != 200 JSON(response.body) end |
#raise_exceptions? ⇒ Boolean
46 47 48 |
# File 'lib/etherscan/client.rb', line 46 def raise_exceptions? @raise_exceptions end |