class Web3::Eth::Etherscan

Constants

DEFAULT_CONNECT_OPTIONS

Attributes

api_key[R]
connect_options[R]

Public Class Methods

new(api_key, connect_options: DEFAULT_CONNECT_OPTIONS) click to toggle source
# File lib/web3/eth/etherscan.rb, line 15
def initialize api_key, connect_options: DEFAULT_CONNECT_OPTIONS
  @api_key = api_key
  @connect_options = connect_options
end

Public Instance Methods

method_missing(m, *args) click to toggle source
# File lib/web3/eth/etherscan.rb, line 20
def method_missing m, *args
  api_module, action = m.to_s.split '_', 2
  raise "Calling method must be in form <module>_<action>" unless action

  arguments = args[0].kind_of?(String) ? { address: args[0] } : args[0]
  result = request api_module, action, arguments

  if connect_options[:parse_result]
    begin
      JSON.parse result
    rescue
      result
    end
  else
    result
  end

end

Private Instance Methods

request(api_module, action, args = {}) click to toggle source
# File lib/web3/eth/etherscan.rb, line 42
def request api_module, action, args = {}

  uri = URI connect_options[:url]
  uri.query = URI.encode_www_form({
      module: api_module,
      action: action,
      apikey: api_key
                                  }.merge(args))

  Net::HTTP.start(uri.host, uri.port,
                  connect_options.merge(use_ssl: uri.scheme=='https' )) do |http|

    request = Net::HTTP::Get.new uri
    response = http.request request

    raise "Error code #{response.code} on request #{uri.to_s} #{request.body}" unless response.kind_of? Net::HTTPOK

    json = JSON.parse(response.body)

    raise "Response #{json['message']} on request #{uri.to_s}" unless json['status']=='1'

    json['result']

  end
end