Class: VoiceTel::Client
- Inherits:
-
Object
- Object
- VoiceTel::Client
- 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
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
-
#account ⇒ Object
— resource accessors ————————————————.
- #acl ⇒ Object
-
#api_key ⇒ Object
Currently installed bearer token (nil if none).
- #authentication ⇒ Object
-
#base_url ⇒ Object
API endpoint this client is pointed at.
- #e911 ⇒ Object
- #gateways ⇒ Object
- #i_numbering ⇒ Object
-
#initialize(api_key: nil, base_url: nil, timeout: 30, max_retries: 2, user_agent: nil, adapter: nil) ⇒ Client
constructor
A new instance of Client.
-
#login(username:, password:) ⇒ String
Exchange username + password for a 32-hex API key and install it on this client.
- #lookups ⇒ Object
- #messaging ⇒ Object
- #numbers ⇒ Object
- #support ⇒ Object
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
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
34 35 36 |
# File 'lib/voicetel.rb', line 34 def transport @transport end |
Instance Method Details
#account ⇒ Object
— resource accessors ————————————————
82 83 84 |
# File 'lib/voicetel.rb', line 82 def account @account ||= Resources::Account.new(@transport) end |
#acl ⇒ Object
86 87 88 |
# File 'lib/voicetel.rb', line 86 def acl @acl ||= Resources::Acl.new(@transport) end |
#api_key ⇒ Object
Currently installed bearer token (nil if none).
71 72 73 |
# File 'lib/voicetel.rb', line 71 def api_key @transport.api_key end |
#authentication ⇒ Object
90 91 92 |
# File 'lib/voicetel.rb', line 90 def authentication @authentication ||= Resources::Authentication.new(@transport) end |
#base_url ⇒ Object
API endpoint this client is pointed at.
76 77 78 |
# File 'lib/voicetel.rb', line 76 def base_url @transport.base_url end |
#e911 ⇒ Object
94 95 96 |
# File 'lib/voicetel.rb', line 94 def e911 @e911 ||= Resources::E911.new(@transport) end |
#gateways ⇒ Object
98 99 100 |
# File 'lib/voicetel.rb', line 98 def gateways @gateways ||= Resources::Gateways.new(@transport) end |
#i_numbering ⇒ Object
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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/voicetel.rb', line 52 def login(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 |
#lookups ⇒ Object
106 107 108 |
# File 'lib/voicetel.rb', line 106 def lookups @lookups ||= Resources::Lookups.new(@transport) end |
#messaging ⇒ Object
110 111 112 |
# File 'lib/voicetel.rb', line 110 def messaging @messaging ||= Resources::Messaging.new(@transport) end |