Class: Etherscan::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



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

#adapterObject



42
43
44
# File 'lib/etherscan/client.rb', line 42

def adapter
  @adapter ||= Faraday.default_adapter
end

#connObject



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

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/etherscan/client.rb', line 9

def headers
  @headers
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/etherscan/client.rb', line 9

def key
  @key
end

#raise_exceptionsObject (readonly)

Returns the value of attribute raise_exceptions.



9
10
11
# File 'lib/etherscan/client.rb', line 9

def raise_exceptions
  @raise_exceptions
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/etherscan/client.rb', line 9

def url
  @url
end

#user_agentObject (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

Returns:

  • (Boolean)


46
47
48
# File 'lib/etherscan/client.rb', line 46

def raise_exceptions?
  @raise_exceptions
end