Class: VoiceTel::Client

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

Overview

Client is the entry point for the VoiceTel API. Construct with an API key (or call #login to exchange username/password) and reach the API through the resource accessors:

c = VoiceTel::Client.new(api_key: ENV.fetch("VOICETEL_API_KEY"))
c.numbers.list

Client is safe to share across threads as long as you don’t reassign the API key concurrently with in-flight requests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, timeout: 30, max_retries: 2, user_agent: nil, adapter: nil) ⇒ Client

Returns a new instance of Client.



36
37
38
39
40
41
42
43
44
45
# File 'lib/voicetel.rb', line 36

def initialize(api_key: nil, base_url: nil, timeout: 30, max_retries: 2, user_agent: nil, adapter: nil)
  @transport = Internal::Transport.new(
    base_url: base_url,
    api_key: api_key,
    timeout: timeout,
    max_retries: max_retries,
    user_agent: user_agent,
    adapter: adapter
  )
end

Instance Attribute Details

#transportObject (readonly)

Returns the value of attribute transport.



34
35
36
# File 'lib/voicetel.rb', line 34

def transport
  @transport
end

Instance Method Details

#accountObject

— resource accessors ————————————————



82
83
84
# File 'lib/voicetel.rb', line 82

def 
  @account ||= Resources::Account.new(@transport)
end

#aclObject



86
87
88
# File 'lib/voicetel.rb', line 86

def acl
  @acl ||= Resources::Acl.new(@transport)
end

#api_keyObject

Currently installed bearer token (nil if none).



71
72
73
# File 'lib/voicetel.rb', line 71

def api_key
  @transport.api_key
end

#authenticationObject



90
91
92
# File 'lib/voicetel.rb', line 90

def authentication
  @authentication ||= Resources::Authentication.new(@transport)
end

#base_urlObject

API endpoint this client is pointed at.



76
77
78
# File 'lib/voicetel.rb', line 76

def base_url
  @transport.base_url
end

#e911Object



94
95
96
# File 'lib/voicetel.rb', line 94

def e911
  @e911 ||= Resources::E911.new(@transport)
end

#gatewaysObject



98
99
100
# File 'lib/voicetel.rb', line 98

def gateways
  @gateways ||= Resources::Gateways.new(@transport)
end

#i_numberingObject



102
103
104
# File 'lib/voicetel.rb', line 102

def i_numbering
  @i_numbering ||= Resources::INumbering.new(@transport)
end

#login(username:, password:) ⇒ String

Exchange username + password for a 32-hex API key and install it on this client. Shares the 6 req/hour/IP rate limit with the rest of the account/* endpoints.

Returns:

  • (String)

    the freshly exchanged bearer token



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/voicetel.rb', line 52

def (username:, password:)
  data = @transport.request(
    :post, "/v2.2/account/api-key",
    body: { username: username, password: password },
    require_auth: false
  )
  key = data.is_a?(Hash) ? data["apikey"] : nil
  if key.nil? || key.empty?
    raise ApiError.new(
      "voicetel: api-key response did not contain data.apikey",
      kind: :authentication,
      body: data
    )
  end
  @transport.api_key = key
  key
end

#lookupsObject



106
107
108
# File 'lib/voicetel.rb', line 106

def lookups
  @lookups ||= Resources::Lookups.new(@transport)
end

#messagingObject



110
111
112
# File 'lib/voicetel.rb', line 110

def messaging
  @messaging ||= Resources::Messaging.new(@transport)
end

#numbersObject



114
115
116
# File 'lib/voicetel.rb', line 114

def numbers
  @numbers ||= Resources::Numbers.new(@transport)
end

#supportObject



118
119
120
# File 'lib/voicetel.rb', line 118

def support
  @support ||= Resources::Support.new(@transport)
end