Gem Version

Bungie Client

This gem makes possible to use Bungie API (and Destiny API too) with authentication if necessary.
It can be useful, if you decide create your application for Destiny Game.

Installation

Add this line to your application's Gemfile:

gem 'bungie_client'

Or install it yourself as:

$ gem install bungie_client

Usage

For public requests you must initialize a client of api with BungieClient::Client.

client = BungieClient::Client.new :api_key => '1234'

The option api_key only necessary for this class and API. For getting API key, please visit the bungie page.

After it you can send your request to Bungie Endpoint.

The full information about classes and modules and their methods you can find in yard-comments and rubydoc.

BungieClient::Auth

This module has two methods: authentication in bungie.net by PSN or Xbox Live and checking this authentication with cookies that was returned early.

Example:

# Authenticate and get bungie cookies
jar = BungieClient::Auth.auth  '[email protected]', 'example', 'psn'

# Check authentication
p BungieClient::Auth.auth_possible? (jar || [])

BungieClient::Cache

It's class created for easy wrapping another cache clients, e.g. Redis.

Examples of initialization:

require 'redis'

BungieClient::Cache.new(
  :ttl    => 900,
  :client => Redis.new,
  :get    => Proc.new { |c, key| c.get key },
  :set    => Proc.new { |c, key, value, ttl| c.setex key, ttl, value }
)

For Rails wrapper:

BungieClient::Cache.new(
  :ttl    => @ttl,
  :client => Rails.cache,
  :get    => Proc.new { |c, key| c.read key },
  :set    => Proc.new { |c, key, value, ttl| c.write key, value, expires_in: ttl }
)

BungieClient::Client

It's main class that makes possible to send any requests to Bungie and connects cache wrapper and auth module in one client.

For this you should initialize your client for the next example:

client = BungieClient::Client.new(
  :api_key => 'YOUR_API_KEY',
  :authentication => true,
  :username => '[email protected]',
  :password => '1234',
  :type     => 'psn',
  :cache => BungieClient::Cache.new(
    :ttl    => 900,
    :client => Redis.new,
    :get    => Proc.new { |c, key| c.get key },
    :set    => Proc.new { |c, key, value, ttl| c.setex key, ttl, value }
  )
)

How it initialized:

  • Before working the client tries to authenticate in bungie, if you pass authentication option with account data, and after it uses cookies for next requests.
  • If store your cookies in any place you can define they with cookies option without authentication.
  • For requests optimization you should use caching of your requests defined BungieClient::Cache in cache option.
  • After this operations your client is done for usage.

The authentication and caching are optional for client, it requires only api_key in your hash.

Sending requests

Now you can send requests, e.g. for finding user information and getting his profile:

# search account
s = client.get_response "Destiny/SearchDestinyPlayer/2/RuBAN-GT"

p s = s.first if !s.nil? && s.length == 1

# get profile with characters
p client.get_response "Destiny/#{s['membershipType']}/Account/#{s['membershipId']}" unless s.nil?

One sample of Rails integration

If you want to work with Rails you can create easy wrapper, such as:

require 'bungie_client'

class RailsBungieWrapper
  class << self
    def api_key=(value)
      @api_key = value
    end
    def ttl=(value)
      @ttl = value
    end

    def init
      yield(self) if block_given?
    end

    def client
      return @client unless @client.nil?

      @client = BungieClient::Client.new(
        :api_key => @api_key,
        :cache => BungieClient::Cache.new(
          :ttl    => @ttl,
          :client => Rails.cache,
          :get    => Proc.new { |c, key| c.read key },
          :set    => Proc.new { |c, key, value, ttl| c.write key, value, expires_in: ttl }
        )
      )
    end
  end
end

After it, you should add your initializer to config\initializers:

RailsBungieWrapper.init do |config|
  config.api_key = '1234'
end

In next operations the RailsBungieWrapper.client will be available for calls.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/RuBAN-GT/bungie_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Fireteam

If you want to fight with Oryx with me or create any interesting applications for Destiny, you can add me (https://www.bungie.net/en/Profile/254/12488384).