class ZcashRubyExplorer::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, limit = 10, offset = 0) click to toggle source
# File lib/zcash_ruby_explorer/api.rb, line 21
def address_full_txs(address, limit = 10, offset = 0)
  query = { limit: limit, offset: offset }
  api_http_get("accounts/#{address}/recv", query: query)
end
block_full_txs(hash, direction = 'ascending', limit = 10, offset = 0) click to toggle source

direction: ascending | descending limit: integer offset: integer

# File lib/zcash_ruby_explorer/api.rb, line 10
def block_full_txs(hash, direction = 'ascending', limit = 10, offset = 0)
  query = {
    sort: 'index',
    direction: direction,
    limit: limit,
    offset: offset
  }

  api_http_get("blocks/#{hash}/transactions", query: query)
end
tx_info(hash) click to toggle source
# File lib/zcash_ruby_explorer/api.rb, line 26
def tx_info(hash)
  api_http_get("transactions/#{hash}")
end

Private Instance Methods

api_http_call(http_method, api_path, query, json_payload: nil) click to toggle source
# File lib/zcash_ruby_explorer/api.rb, line 32
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/zcash_ruby_explorer/api.rb, line 74
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/zcash_ruby_explorer/api.rb, line 78
def endpoint_uri(api_path, query)
  uri = URI("https://api.zcha.in/v2/mainnet/#{api_path}")
  uri.query = URI.encode_www_form(query) unless query.empty?
  uri
end