bungie_client 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95a7ad6e7c964e46ac065ef6ad93b0f3f14fdaff
4
+ data.tar.gz: f8f66605d63fdb4df70bd8d36fad5ce18f9a12ee
5
+ SHA512:
6
+ metadata.gz: 542e0b3e888e493c3083ae64a1f27bdfbd50729b9ca33b7106b4eb17ad01a9add32530bc6ea446242cad0d49813481e98049448cb066854e016e2ccccdaa2a11
7
+ data.tar.gz: f20b477ced62bd43c2f77bf0f3a22c912a43bd15349ceb6dd5f1f0a55b939a686cb1b54e74df8e1c326915c57ccd6e14cbd636c3ec72d0c8fa8282e0ad3a1f2d
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at dkruban@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'redis'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dmitry Ruban
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,178 @@
1
+ [![Gem Version](https://badge.fury.io/rb/bungie_client.svg)](https://badge.fury.io/rb/bungie_client)
2
+
3
+ # Bungie Client
4
+
5
+ This gem makes possible to use [Bungie API](http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints) (and Destiny API too) with authentication if necessary.
6
+ It can be useful, if you decide create your application for [Destiny Game](https://www.bungie.net/en/pub/AboutDestiny).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'bungie_client'
14
+ ```
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install bungie_client
19
+
20
+ ## Usage
21
+
22
+ For public requests you must initialize a client of api with `BungieClient::Client`.
23
+
24
+ ~~~~ruby
25
+ client = BungieClient::Client.new :api_key => '1234'
26
+ ~~~~
27
+
28
+ The option `api_key` only necessary for this class and API. For getting API key, please visit the [bungie page](https://www.bungie.net/en/user/api).
29
+
30
+ After it you can send your request to [Bungie Endpoint](http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints).
31
+
32
+ The full information about classes and modules and their methods you can find in yard-comments and [rubydoc](http://www.rubydoc.info/gems/bungie_client).
33
+
34
+ ### BungieClient::Auth
35
+
36
+ This module has two methods: authentication in bungie.net by PSN or Xbox Live and checking this authentication with cookies that was returned early.
37
+
38
+ **Example:**
39
+
40
+ ~~~~ ruby
41
+ # Authenticate and get bungie cookies
42
+ jar = BungieClient::Auth.auth 'example@mail.com', 'example', 'psn'
43
+
44
+ # Check authentication
45
+ p BungieClient::Auth.auth_possible? (jar || [])
46
+ ~~~~
47
+
48
+ ### BungieClient::Cache
49
+
50
+ It's class created for easy wrapping another cache clients, e.g. [Redis](https://github.com/redis/redis-rb).
51
+
52
+ **Examples of initialization:**
53
+
54
+ ~~~~ruby
55
+ require 'redis'
56
+
57
+ BungieClient::Cache.new(
58
+ :ttl => 900,
59
+ :client => Redis.new,
60
+ :get => Proc.new { |c, key| c.get key },
61
+ :set => Proc.new { |c, key, value, ttl| c.setex key, ttl, value }
62
+ )
63
+ ~~~~
64
+
65
+ **For Rails wrapper:**
66
+
67
+ ~~~~ruby
68
+ BungieClient::Cache.new(
69
+ :ttl => @ttl,
70
+ :client => Rails.cache,
71
+ :get => Proc.new { |c, key| c.read key },
72
+ :set => Proc.new { |c, key, value, ttl| c.write key, value, expires_in: ttl }
73
+ )
74
+ ~~~~
75
+
76
+ ### BungieClient::Client
77
+
78
+ It's main class that makes possible to send any requests to Bungie and connects cache wrapper and auth module in one client.
79
+
80
+ **For this you should initialize your client for the next example:**
81
+
82
+ ~~~~ruby
83
+ client = BungieClient::Client.new(
84
+ :api_key => 'YOUR_API_KEY',
85
+ :authentication => true,
86
+ :username => 'test@test.test',
87
+ :password => '1234',
88
+ :type => 'psn',
89
+ :cache => BungieClient::Cache.new(
90
+ :ttl => 900,
91
+ :client => Redis.new,
92
+ :get => Proc.new { |c, key| c.get key },
93
+ :set => Proc.new { |c, key, value, ttl| c.setex key, ttl, value }
94
+ )
95
+ )
96
+ ~~~~
97
+
98
+ #### How it initialized:
99
+
100
+ * 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.
101
+ * If store your cookies in any place you can define they with `cookies` option without `authentication`.
102
+ * For requests optimization you should use caching of your requests defined `BungieClient::Cache` in `cache` option.
103
+ * After this operations your client is done for usage.
104
+
105
+ > The authentication and caching are optional for client, it requires only `api_key` in your hash.
106
+
107
+ #### Sending requests
108
+
109
+ **Now you can send requests, e.g. for finding user information and getting his profile:**
110
+
111
+ ~~~~ruby
112
+ # search account
113
+ s = client.get_response "Destiny/SearchDestinyPlayer/2/RuBAN-GT"
114
+
115
+ p s = s.first if !s.nil? && s.length == 1
116
+
117
+ # get profile with characters
118
+ p client.get_response "Destiny/#{s['membershipType']}/Account/#{s['membershipId']}" unless s.nil?
119
+ ~~~~
120
+
121
+ ## One sample of Rails integration
122
+
123
+ If you want to work with Rails you can create easy wrapper, such as:
124
+
125
+ ~~~~ ruby
126
+ require 'bungie_client'
127
+
128
+ class RailsBungieWrapper
129
+ class << self
130
+ def api_key=(value)
131
+ @api_key = value
132
+ end
133
+ def ttl=(value)
134
+ @ttl = value
135
+ end
136
+
137
+ def init
138
+ yield(self) if block_given?
139
+ end
140
+
141
+ def client
142
+ return @client unless @client.nil?
143
+
144
+ @client = BungieClient::Client.new(
145
+ :api_key => @api_key,
146
+ :cache => BungieClient::Cache.new(
147
+ :ttl => @ttl,
148
+ :client => Rails.cache,
149
+ :get => Proc.new { |c, key| c.read key },
150
+ :set => Proc.new { |c, key, value, ttl| c.write key, value, expires_in: ttl }
151
+ )
152
+ )
153
+ end
154
+ end
155
+ end
156
+ ~~~~
157
+
158
+ After it, you should add your initializer to `config\initializers`:
159
+
160
+ ~~~~ruby
161
+ RailsBungieWrapper.init do |config|
162
+ config.api_key = '1234'
163
+ end
164
+ ~~~~
165
+
166
+ In next operations the `RailsBungieWrapper.client` will be available for calls.
167
+
168
+ ## Contributing
169
+
170
+ 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](http://contributor-covenant.org) code of conduct.
171
+
172
+ ## License
173
+
174
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
175
+
176
+ ## Fireteam
177
+
178
+ 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](https://www.bungie.net/en/Profile/254/12488384)).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bungie_client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'bungie_client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bungie_client"
7
+ spec.version = BungieClient::VERSION
8
+ spec.authors = ["Dmitry Ruban"]
9
+ spec.email = ["dkruban@gmail.com"]
10
+
11
+ spec.summary = "This gem makes possible to use Bungie API (and Destiny API too) with authentication if necessary."
12
+ spec.description = "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 (https://www.bungie.net/en/pub/AboutDestiny)."
13
+ spec.homepage = "https://github.com/RuBAN-GT/bungie_client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency 'mechanize', "~> 2.7", ">= 2.7.0"
25
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bungie_client'
4
+
5
+ # Get bungie cookies
6
+ jar = BungieClient::Auth.auth 'example@mail.com', 'example', 'psn'
7
+ p (jar.nil?) ? 0 : jar.cookies.length
8
+
9
+ # Check cookies and authentication
10
+ p BungieClient::Auth.auth_possible? (jar || [])
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bungie_client'
4
+ require 'redis'
5
+
6
+ # init client
7
+ client = BungieClient::Client.new(
8
+ :api_key => '1234',
9
+ :cache => BungieClient::Cache.new(
10
+ :ttl => 900,
11
+ :client => Redis.new,
12
+ :get => Proc.new { |c, key| c.get key },
13
+ :set => Proc.new { |c, key, value, ttl| c.setex key, ttl, value }
14
+ )
15
+ )
16
+
17
+ # search account
18
+ s = client.get_response "Destiny/SearchDestinyPlayer/2/RuBAN-GT"
19
+
20
+ p s = s.first if !s.nil? && s.length == 1
21
+
22
+ # get profile with characters
23
+ p client.get_response "Destiny/#{s['membershipType']}/Account/#{s['membershipId']}" unless s.nil?
@@ -0,0 +1,6 @@
1
+ require "mechanize"
2
+
3
+ require "bungie_client/version"
4
+ require "bungie_client/auth"
5
+ require "bungie_client/client"
6
+ require "bungie_client/cache"
@@ -0,0 +1,136 @@
1
+ # Module Auth with two methods: authentication in bungie.net by PSN or Xbox Live and checking this authentication with cookies that was returned early.
2
+ module BungieClient::Auth
3
+ class << self
4
+ # Check cookies for private requests
5
+ #
6
+ # @param [Array] cookies with [HTTP::Cookie]
7
+ # @param [Boolean] raise cookie errors
8
+ #
9
+ # @return [Boolean]
10
+ def valid_cookies?(cookies, raise = false)
11
+ if cookies.is_a? Mechanize::CookieJar
12
+ cookies = cookies.cookies
13
+ elsif !(cookies.is_a?(Array) && cookies.all? { |c| c.is_a? HTTP::Cookie })
14
+ raise "Wrong cookie option: It must be Array with HTTP::Cookie elements or CookieJar." if raise
15
+
16
+ return false
17
+ end
18
+
19
+ needed = %w(bungled bungleatk bungles)
20
+ cookies.each do |cookie|
21
+ if needed.include?(cookie.name) && cookie.expired? == false
22
+ needed.delete cookie.name
23
+
24
+ return true if needed.length == 0
25
+ end
26
+ end
27
+
28
+ (raise) ? raise("Your argument doesn't have all needed cookies for private requests #{needed.join ','}.") : false
29
+ end
30
+
31
+ # Full authentication of user
32
+ #
33
+ # @param [String] username user email for auth
34
+ # @param [String] password user password for auth
35
+ # @param [String] type can be psn or live
36
+ #
37
+ # @return [Mechanize::CookieJar|nil]
38
+ def auth(username, password, type = 'psn')
39
+ # client init
40
+ agent = Mechanize.new
41
+
42
+ # getting index page
43
+ agent.get 'https://www.bungie.net/' do |page|
44
+ result = nil
45
+ link = page.link_with :text => search_query(type)
46
+
47
+ unless link.nil?
48
+ # call auth page
49
+ login = agent.click link
50
+
51
+ # sending form for sony or ms
52
+ if type == 'psn'
53
+ form = login.forms.first
54
+ unless form.nil?
55
+ name = form.field_with(:type => 'email')
56
+ pass = form.field_with(:type => 'password')
57
+
58
+ if !name.nil? && !pass.nil?
59
+ name.value = username
60
+ pass.value = password
61
+
62
+ result = form.click_button
63
+ end
64
+ end
65
+ else
66
+ # ms wanted enabled js, but we can send form without it
67
+ ppft = login.body.match(/name\="PPFT"(.*)value\="(.*?)"/)
68
+ url = login.body.match(/urlPost\:'(.*?)'/)
69
+
70
+ if !ppft.nil? && !url.nil?
71
+ result = agent.post url.to_a.last,
72
+ 'login' => username,
73
+ 'passwd' => password,
74
+ 'KMSI' => 1,
75
+ 'PPFT' => ppft.to_a.last
76
+ end
77
+ end
78
+ end
79
+
80
+ # if result not nil and after authentication we returned to bungie, all ok
81
+ if !result.nil? && result.uri.to_s.include?('?code')
82
+ needed = %w(bungled bungleatk bungledid bungles)
83
+ output = Mechanize::CookieJar.new
84
+
85
+ agent.cookies.each do |cookie|
86
+ output.add cookie if needed.include? cookie.name
87
+ end
88
+
89
+ return output
90
+ end
91
+ end
92
+
93
+ nil
94
+ end
95
+
96
+ # Try authenticate with cookies
97
+ #
98
+ # @param [Array|CookieJar] cookies array with [HTTP::Cookie] or [CookieJar]
99
+ #
100
+ # @return [Boolean]
101
+ def auth_possible?(cookies)
102
+ agent = Mechanize.new
103
+
104
+ valid_cookies? cookies, true
105
+
106
+ if cookies.is_a? Array
107
+ cookies.each do |cookie|
108
+ agent.cookie_jar.add cookie
109
+ end
110
+ else
111
+ agent.cookie_jar = cookies
112
+ end
113
+
114
+ result = nil
115
+ agent.get 'https://www.bungie.net/' do |page|
116
+ link = page.link_with :text => search_query('psn')
117
+
118
+ result = agent.click link unless link.nil?
119
+ end
120
+
121
+ !result.nil? && result.uri.host == "www.bungie.net"
122
+ end
123
+
124
+ protected
125
+
126
+ def search_query(type)
127
+ if type == 'psn'
128
+ 'PlayStation Network'
129
+ elsif type == 'live'
130
+ 'Xbox Live'
131
+ else
132
+ raise 'Wrong account type!'
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,53 @@
1
+ class BungieClient::Cache
2
+ attr_reader :client
3
+ attr_reader :ttl
4
+
5
+ # Get value
6
+ #
7
+ # @param [String] key
8
+ #
9
+ # @return [Object]
10
+ def get(key)
11
+ result = @get.call @client, key.to_s
12
+
13
+ Marshal.load result unless result.nil?
14
+ end
15
+
16
+ # Set value
17
+ #
18
+ # @param [String] key
19
+ # @param [Object] value it can be everything, because it serialized with [Marshal]
20
+ # @param [Integer|nil] ttl
21
+ def set(key, value, ttl = nil)
22
+ @set.call @client, key.to_s, Marshal.dump(value), (ttl || @ttl)
23
+ end
24
+
25
+ # Initialize handlers of cache client with options
26
+ #
27
+ # @param [Hash] options with
28
+ # @option options [Class] :client - basic object of cache client, e.g. [Redis]
29
+ # @option options [Proc] :get - method for getting data, it gets key of row on call
30
+ # @option options [Proc] :set - method for setting data, it gets key, value, ttl on call
31
+ # @option options [Integer] :ttl - time to live of row in cache
32
+ def initialize(options = {})
33
+ @ttl = (options[:ttl].is_a?(Integer) && options[:ttl] > 0) ? options[:ttl] : 900
34
+
35
+ if options[:client].nil?
36
+ raise 'You must define the client initialization.'
37
+ else
38
+ @client = options[:client]
39
+ end
40
+
41
+ if options[:get].is_a? Proc
42
+ @get = options[:get]
43
+ else
44
+ raise 'You must define the get method for caching.'
45
+ end
46
+
47
+ if options[:set].is_a? Proc
48
+ @set = options[:set]
49
+ else
50
+ raise 'You must define the set method for caching.'
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,162 @@
1
+ # Class Client for GET/POST requests to Bungie.
2
+ # For specific HTTP operations you can use @agent [Mechanzie].
3
+ class BungieClient::Client
4
+ BUNGIE_URI = 'https://www.bungie.net/Platform'
5
+
6
+ # Form uri for requests
7
+ #
8
+ # @param [String] uri
9
+ # @return [String]
10
+ def self.request_uri(uri)
11
+ "#{BUNGIE_URI}/#{uri.sub(/^\//, '').sub(/\/$/, '')}/"
12
+ end
13
+
14
+ attr_reader :api_key
15
+ attr_reader :username
16
+ attr_reader :password
17
+ attr_reader :type
18
+ attr_reader :agent
19
+ attr_reader :cache
20
+
21
+ # Init client
22
+ #
23
+ # @see https://www.bungie.net/en/user/api
24
+ #
25
+ # Initialize client for bungie api throw hash:
26
+ #
27
+ # @param [Hash] options
28
+ # @option options [String] :api_key
29
+ # @option options [Array] :cookies with [HTTP::Cookie]
30
+ # @option options [BungieClient::Cache] :cache client for saving respones
31
+ # @option options [Boolean] :authentication makes authentication with next three field for getting cookies for private requests
32
+ # @option options [String] :username username for authentication, necessary if set :authenticate
33
+ # @option options [String] :password password of user, necessary if set :authenticate
34
+ # @option options [String] :type of account, it can be 'psn' or 'live'
35
+ def initialize(options)
36
+ # checking options and @api_key
37
+ raise 'Wrong options: It must be Hash.' unless options.is_a? Hash
38
+
39
+ if options[:api_key].nil?
40
+ raise "The API-key required for every request to bungie."
41
+ else
42
+ @api_key = options[:api_key].to_s
43
+ end
44
+
45
+ # init @cache
46
+ unless options[:cache].nil?
47
+ if options[:cache].is_a? BungieClient::Cache
48
+ @cache = options[:cache]
49
+ else
50
+ raise 'Cache client must be inhereted from [BungieClient::Cache].'
51
+ end
52
+ end
53
+
54
+ # init @agent
55
+ @agent = Mechanize.new
56
+
57
+ # make authentication
58
+ if options[:authentication]
59
+ @username = options[:username] if options[:username].is_a? String
60
+ @password = options[:password] if options[:password].is_a? String
61
+ @type = options[:type].to_s if ['psn', 'live'].include? options[:type].to_s
62
+
63
+ cookies = BungieClient::Auth.auth @username, @password, (@type || 'psn')
64
+
65
+ if cookies.nil?
66
+ raise "Wrong authentication. Check your account data."
67
+ else
68
+ @agent.cookie_jar = cookies
69
+ end
70
+ end
71
+
72
+ # merge cookies with options
73
+ if BungieClient::Auth.valid_cookies? options[:cookies], true
74
+ cookies = (options[:cookies].is_a? Array) ? cookies : cookies.cookies
75
+
76
+ cookies.each do |cookie|
77
+ @agent.cookie_jar.add cookie
78
+ end
79
+ end unless options[:cookies].nil?
80
+ end
81
+
82
+ # Check options for allowing getting of cache
83
+ #
84
+ # @param [Hash] options of cache
85
+ #
86
+ # @return [Boolean]
87
+ def allow_get_cache(options = {})
88
+ allow_set_cache(options) && options[:cache_rewrite] != true
89
+ end
90
+
91
+ # Check options for allowing setting of cache
92
+ #
93
+ # @param [Hash] options of cache
94
+ #
95
+ # @return [Boolean]
96
+ def allow_set_cache(options = {})
97
+ !@cache.nil? && options[:cache_none] != true
98
+ end
99
+
100
+ # Get response from bungie services
101
+ #
102
+ # @see http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints
103
+ #
104
+ # @param [String] uri
105
+ # @param [Hash|Array] parameters for http-query
106
+ #
107
+ # @return [String|nil]
108
+ def get(uri, parameters = {})
109
+ @agent.get(self.class.request_uri(uri), parameters, nil, headers).body rescue nil
110
+ end
111
+
112
+ # Get Response field after get request to bungie
113
+ #
114
+ # @param [String] uri
115
+ # @param [Hash|Array] parameters for http-query
116
+ # @param [Hash] options for cache such as:
117
+ # @option options [Boolean] :cache_none - disable response caching
118
+ # @option options [Boolean] :cache_rewrite - update cache value
119
+ # @option options [Integer] :cache_ttl - special cache ttl
120
+ #
121
+ # @return [Array|Hash|nil]
122
+ def get_response(uri, parameters = {}, options = {})
123
+ if allow_get_cache options
124
+ result = @cache.get "#{uri}+#{parameters}"
125
+
126
+ return result unless result.nil?
127
+ end
128
+
129
+ result = get uri, parameters
130
+
131
+ if !result.nil? && result != ''
132
+ result = JSON.parse result
133
+
134
+ if !result['Response'].nil? && !result['ErrorCode'].nil? && result['ErrorCode'] == 1
135
+ @cache.set "#{uri}+#{parameters}", result['Response'], options[:cache_ttl] if allow_set_cache options
136
+
137
+ result['Response']
138
+ end
139
+ end
140
+ end
141
+
142
+ # Post data to bungie services
143
+ #
144
+ # @param [String] uri
145
+ # @param [Hash] query
146
+ #
147
+ # @return [String|nil]
148
+ def post(uri, query = {})
149
+ @agent.post(self.class.request_uri(uri), query, headers).body rescue nil
150
+ end
151
+
152
+ protected
153
+
154
+ # Headers for requests
155
+ def headers
156
+ {
157
+ 'Accept' => 'json',
158
+ 'Content-Type' => 'json',
159
+ 'X-API-Key' => @api_key
160
+ }
161
+ end
162
+ end
@@ -0,0 +1,3 @@
1
+ module BungieClient
2
+ VERSION = '0.5.0'
3
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bungie_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Ruban
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mechanize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.7'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.7.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '2.7'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.7.0
75
+ description: This gem makes possible to use Bungie API (and Destiny API too) with
76
+ authentication if necessary. It can be useful, if you decide create your application
77
+ for Destiny Game (https://www.bungie.net/en/pub/AboutDestiny).
78
+ email:
79
+ - dkruban@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".gitignore"
85
+ - ".rspec"
86
+ - ".travis.yml"
87
+ - CODE_OF_CONDUCT.md
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - bin/console
93
+ - bin/setup
94
+ - bungie_client.gemspec
95
+ - examples/auth_usage.rb
96
+ - examples/client_usage.rb
97
+ - lib/bungie_client.rb
98
+ - lib/bungie_client/auth.rb
99
+ - lib/bungie_client/cache.rb
100
+ - lib/bungie_client/client.rb
101
+ - lib/bungie_client/version.rb
102
+ homepage: https://github.com/RuBAN-GT/bungie_client
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.5.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: This gem makes possible to use Bungie API (and Destiny API too) with authentication
126
+ if necessary.
127
+ test_files: []
128
+ has_rdoc: