class StellarRubyExplorer::Api

Main class of this library, contains the following public methods: address_full_txs and tx_info

Public Instance Methods

address_full_txs(address) click to toggle source
# File lib/stellar_ruby_explorer/api.rb, line 7
def address_full_txs(address)
  query = { limit: 20, order: 'desc' }
  api_http_get("accounts/#{address}/transactions", query)
end
tx_info(hash) click to toggle source
# File lib/stellar_ruby_explorer/api.rb, line 12
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/stellar_ruby_explorer/api.rb, line 18
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/stellar_ruby_explorer/api.rb, line 60
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/stellar_ruby_explorer/api.rb, line 64
def endpoint_uri(api_path, query)
  uri = URI("https://horizon.stellar.org/#{api_path}")
  uri.query = URI.encode_www_form(query) unless query.empty?
  uri
end