Skip to content

nickjacques/ruby-twitch-api

 
 

Repository files navigation

Gem Downloads Cirrus CI - Base Branch Build Status License

Ruby Twitch API

This library is a Ruby implementation of the Twitch Helix API.

The goal is to provide access for the newest supported APIs provided by Twitch, while keeping extensibility for their future expansion. These are still in development, as is this library which should remain in pace with changes made.

Guaranteed supported APIs include:

  • Helix REST (full rolling support)
  • Helix Webhooks (coming soon)

The future may bring:

  • PubSub
  • TMI/chat

These will not be considered:

Installation

Add to your application's Gemfile:

# If you want a full release
gem 'twitch-api'
# If you want to live on the edge
gem 'twitch-api', :git => 'https://github.com/mauricew/ruby-twitch-api'

And then execute:

$ bundle

Or install it yourself as:

$ gem install twitch-api

Usage

Authentication

Twitch documentation.

Client (application) flow

This is default flow (:token_type).

twitch_client = Twitch::Client.new(
  client_id: client_id,
  client_secret: client_secret,

  ## this is default
  # token_type: :application,

  ## this can be required by some Twitch end-points
  # scopes: scopes,

  ## if you already have one
  # access_token: access_token
)

Authorization (user) flow

This is flow required for user-specific actions.

If there are no access_token and refresh_token, TwitchOAuth2::Error will be raised with #metadata[:link].

If you have a web-application with N users, you can redirect them to this link and use redirect_uri to your application for callbacks.

Otherwise, if you have something like CLI tool, you can print instructions with a link for user.

Then you can use #token(token_type: :user, code: 'a code from params in redirect uri') and get your :access_token and :refresh_token.

twitch_client = Twitch::Client.new(
  client_id: client_id,
  client_secret: client_secret,
  token_type: :user,

  ## `localhost` by default, can be your application end-point
  # redirect_uri: redirect_uri,

  ## this can be required by some Twitch end-points
  # scopes: scopes,

  ## if you already have these
  # access_token: access_token,
  # refresh_token: refresh_token
)

After initialization

twitch_client.check_tokens! # old tokens, if they're actual, or new tokens

If you've passed refresh_token to initialization and your access_token is invalid, requests that require access_token will automatically refresh it.

Later you can access tokens:

twitch_client.tokens # => { access_token: 'abcdef', refresh_token: 'ghijkl' }
twitch_client.access_token # => 'abcdef'
twitch_client.refresh_token # => 'ghijkl'

Calls

Because data may change for certain endpoints, there is also the raw response for any request called.

Retrieval methods take in a hash equal to the parameters of the API endpoint, and return a response object containing the data and other associated request information:

  • data is the data you would get back. Even if it's an array of one object, it remains the same as what comes from the API.
  • rate_limit and associated fields contain API request limit information. Clip creation counts for a second limit (duration currently unknown).
  • pagination is a hash that appears when data can be traversed, and contains one member (cursor) which lets you paginate through certain requests.
  • raw is the raw HTTP response data.
# Get top live streams
client.get_streams.data
# Get a single user
client.get_users({login: "disguisedtoasths"}).data.first
# Find some games
# (use an array for up to 100 of most queryable resources)
client.get_games({name: ["Heroes of the Storm", "Super Mario Odyssey"]}).data

Error handling

An APIError is raised whenever an HTTP error response is returned. Rescue it to access the body of the response, which should include an error message.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. (Tests require a Twitch Client ID; since cassettes exist you can use any value.)

You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub.

About

Ruby client for the Twitch Helix API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 99.5%
  • Shell 0.5%