Class: Zapper::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zapper.rb

Overview

The main entry point for the client. You can instantiated a ‘Zapper::Client` instance.

Returns:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = "") ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
# File 'lib/zapper.rb', line 23

def initialize(api_key = "")
  @api_key = api_key
  @conn = Faraday.new(url: HOST) do |f|
    f.request :authorization, "Basic", -> { Base64.encode64 @api_key }
    f.response :json
  end
end

Instance Attribute Details

#api_keyObject (readonly)

API key for Zapper.fi



19
20
21
# File 'lib/zapper.rb', line 19

def api_key
  @api_key
end

#connObject (readonly)

The Faraday::Connection



21
22
23
# File 'lib/zapper.rb', line 21

def conn
  @conn
end

Instance Method Details

#apps(id = nil) ⇒ Faraday::Response

Returns all apps Zapper knows about. If an ‘id` is provided, it will return information on just that app.

Parameters:

  • id (String) (defaults to: nil)

    the app ID.

Returns:

  • (Faraday::Response)


36
37
38
# File 'lib/zapper.rb', line 36

def apps(id = nil)
  id ? conn.get("/v2/apps/#{id}") : conn.get("/v2/apps")
end

#balances(address) ⇒ Faraday::Response

Returns all apps Zapper knows about. If an ‘id` is provided, it will return information on just that app.

Parameters:

  • address (String)

    The hexidecimal address you want to query.

Returns:

  • (Faraday::Response)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zapper.rb', line 45

def balances(address)
  response = conn.get("/v2/balances") do |req|
    req.params["addresses[]"] = address
  end
  response.env.response_body = response
                               .body
                               .split("event: ")
                               .map { |x| x.split("balance\ndata: ")[1] }
                               .compact.map { |x| JSON.parse(x) }
  response
end