The Telnyx Ruby library provides convenient access to the Telnyx API from applications written in the Ruby language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses.
The library also provides other features. For example:
- Easy configuration path for fast setup and use.
- Helpers for pagination.
- Tracking of "fresh" values in API resources so that partial updates can be executed.
- Built-in mechanisms for the serialization of parameters according to the expectations of Telnyx's API.
See the API docs.
You don't need this source code unless you want to modify the gem. If you just want to use the package, just run:
gem install telnyx
If you want to build the gem from source:
gem build telnyx.gemspec
- Ruby 2.1.0+
If you are installing via bundler, you should be sure to use the https rubygems source in your Gemfile, as any gems fetched over http could potentially be compromised in transit and alter the code of gems fetched securely over https:
source'https://rubygems.org'gem'telnyx'The library needs to be configured with your account's secret key which is
available in your Telnyx Portal. Set Telnyx.api_key to its
value:
require"telnyx"Telnyx.api_key="YOUR_API_KEY"# list messaging profilesTelnyx::MessagingProfile.list()# retrieve single messaging profileTelnyx::MessagingProfile.retrieve("123")API resources are paginated and the library comes with a handful of methods to ease dealing with them seemlessly.
# list messaging profilesfirst_page=Telnyx::MessagingProfile.list()# check whether there are more pages to go throughiffirst_page.more?puts("There are still more pages to go.")elseputs("This is the last page.")end# get current page's size and numberfirst_page.page_sizefirst_page.page_number# fetch the next and previous pagessecond_page=first_page.next_pagefirst_page=second_page.previous_page# iterate over the results of a *single page*second_page.eachdo |messaging_profile|
puts(messaging_profile.id)end# iterate over *all of the messaging profiles* starting at `first_page`# similar to `each`, but requests subsequent pages as neededfirst_page.auto_paging_eachdo |messaging_profile|
puts(messaging_profile.id)endWhile a default HTTP client is used by default, it's also possible to have the
library use any client supported by Faraday by initializing a
Telnyx::TelnyxClient object and giving it a connection:
conn=Faraday.newclient=Telnyx::TelnyxClient.new(conn)connection,resp=client.requestdoTelnyx::MessagingProfile.retrieve("123",)endputsresp.request_idThe library can be configured to automatically retry requests that fail due to an intermittent network problem:
Telnyx.max_network_retries = 2
Open and read timeouts are configurable:
Telnyx.open_timeout=30# in secondsTelnyx.read_timeout=80Please take care to set conservative read timeouts. Some API requests can take some time, and a short timeout increases the likelihood of a problem within our servers.
The library can be configured to emit logging that will give you better insight
into what it's doing. The info logging level is usually most appropriate for
production use, but debug is also available for more verbosity.
There are a few options for enabling it:
Set the environment variable
TELNYX_LOG_LEVELto the valuedebugorinfo:$ export TELNYX_LOG_LEVEL=infoSet
Telnyx.log_level:Telnyx.log_level=Telnyx::LEVEL_INFO
The test suite depends on telnyx-mock, so make sure to fetch and run it from a background terminal (telnyx-mock's README also contains instructions for installing via Homebrew and other methods):
go get -u github.com/telnyx/telnyx-mock
telnyx-mock
Run all tests:
bundle exec rake test
Run a single test suite:
bundle exec ruby -Ilib/ test/telnyx/util_test.rb
Run a single test:
bundle exec ruby -Ilib/ test/telnyx/util_test.rb -n /should.convert.names.to.symbols/
Run the linter:
bundle exec rake rubocop
Run guard:
bundle exec guard
Update the bundled telnyx-mock by editing the version number found in
.travis.yml.
To add a new resource:
- Add the class for the resource under
lib/telnyx/. - Require the new class in
lib/telnyx.rb. - Add the association between
OBJECT_NAMEand class name in theobject_classeshash inlib/telnyx/util.rb. - Add tests to validate the behaviour of the new class.
The contributors and maintainers of Telnyx Ruby would like to extend their deep gratitude to the authors of Stripe Ruby, upon which this project is based. Thank you for developing such elegant, usable, extensible code and for sharing it with the community.