class CardanoRubyExplorer::Api
Main class of this library, contains the following public methods: block_full_txs
, address_full_txs
and tx_info
Public Instance Methods
address_full_txs(address)
click to toggle source
# File lib/cardano_ruby_explorer/api.rb, line 12 def address_full_txs(address) api_http_get("addresses/summary/#{address}") end
block_full_txs(block, limit = 5000)
click to toggle source
# File lib/cardano_ruby_explorer/api.rb, line 7 def block_full_txs(block, limit = 5000) query = { limit: limit } api_http_get("blocks/txs/#{block}", query: query) end
tx_info(hash)
click to toggle source
# File lib/cardano_ruby_explorer/api.rb, line 16 def tx_info(hash) api_http_get("txs/summary/#{hash}") end
Private Instance Methods
api_http_call(http_method, api_path, query, json_payload: nil)
click to toggle source
# File lib/cardano_ruby_explorer/api.rb, line 22 def api_http_call(http_method, api_path, query, json_payload: nil) uri = endpoint_uri(api_path, query) # Build the connection http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true # Build the Request if http_method == :post request = Net::HTTP::Post.new(uri.request_uri) elsif http_method == :get request = Net::HTTP::Get.new(uri.request_uri) elsif http_method == :delete request = Net::HTTP::Delete.new(uri.request_uri) else raise 'Invalid HTTP method' end unless json_payload.nil? request.content_type = 'application/json' request.body = json_payload.to_json end response = http.request(request) response_code = response.code.to_i # Detect errors/return 204 empty body if response_code >= 400 raise Error.new(uri.to_s + ' Response:' + response.body) elsif response_code == 204 return nil end # Process the response begin json_response = JSON.parse(response.body) return json_response rescue => e raise "Unable to parse JSON response #{e.inspect}, #{response.body}" end end
api_http_get(api_path, query: {})
click to toggle source
# File lib/cardano_ruby_explorer/api.rb, line 64 def api_http_get(api_path, query: {}) api_http_call(:get, api_path, query) end
endpoint_uri(api_path, query)
click to toggle source
# File lib/cardano_ruby_explorer/api.rb, line 68 def endpoint_uri(api_path, query) uri = URI("https://cardanoexplorer.com/api/#{api_path}") uri.query = URI.encode_www_form(query) unless query.empty? uri end