Class: Subscan::Api
- Inherits:
-
Object
show all
- Defined in:
- lib/subscan/api.rb
Instance Method Summary
collapse
Constructor Details
#initialize(url, api_key = nil) ⇒ Api
3
4
5
6
|
# File 'lib/subscan/api.rb', line 3
def initialize(url, api_key = nil)
@url = url
@api_key = api_key
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
45
46
47
48
|
# File 'lib/subscan/api.rb', line 45
def method_missing(method, *args)
module_name, action = method.to_s.split('_')
request(module_name, action, args[0] || {})
end
|
Instance Method Details
#request(module_name, action, params = {}) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/subscan/api.rb', line 8
def request(module_name, action, params = {})
params = params.reject { |_k, v| v.nil? }
url = "#{@url}/scan/#{module_name}/#{action}"
uri = URI(url)
Etherscan.logger.debug "Req: #{uri}"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['X-API-Key'] = @api_key unless @api_key.nil?
request.body = params.to_json
Etherscan.logger.debug "Body: #{request.body}"
response = http.request(request)
raise response.body if response.code != '200'
json_response = JSON.parse(response.body)
if json_response.key?('status')
raise json_response['message'] if json_response['status'] != '1'
json_response['result'] || json_response['message']
elsif json_response.key?('code')
raise json_response['message'] if json_response['code'] != 0
json_response['data']
end
end
|
#respond_to_missing?(*_args) ⇒ Boolean
41
42
43
|
# File 'lib/subscan/api.rb', line 41
def respond_to_missing?(*_args)
true
end
|