bungie_client 0.6.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f0b5c103d49481ce7300bd2724679f860977880
4
- data.tar.gz: 998c54fc103ba3ccec2473bb9e6e53f747293781
3
+ metadata.gz: dbcc8ccd4f843c09b035c1cadf0d3104cc2cb6c2
4
+ data.tar.gz: d3e03ebaae92ef497c8388b02f3a1239d7501965
5
5
  SHA512:
6
- metadata.gz: 9f3f1ef5947d8e36f9109d7c9aa86a2bae01ffa84b2432e16c64f063b4821b246fa8cefe97a09495f25a0f81ddce9b74854b222b0bb95ae1c93778263a10f6aa
7
- data.tar.gz: 270f89f6c368a7dc9f756329fc8ba3f6d7dde138d2036e5cedabf65bc53851bb343d6f1c9bb40b93bf52f49c2c7f21da65c712431a0981420bd8cc5e00de827a
6
+ metadata.gz: 125fcbc0d2023e58cf1c189d88a0ea4ba781696d138f495dd3464d2d9ed2f554f18470d1be1ec156c092abea888c1b0ef0a412231d4cd98db244618ba1231a12
7
+ data.tar.gz: 58abe874906be78e8859167be290386b048ecd8632935b6123e9dc30562e0672b4f622ddde7a538821abc76808337d5840e2550b8f8d02a6a0294b9fb0b2aebb
data/.gitignore CHANGED
@@ -7,5 +7,8 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .rspec
11
+ .travis.yml
10
12
  .idea
11
13
  *.gem
14
+ Gemfile.lock
data/Gemfile CHANGED
@@ -1,5 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
4
-
5
- gem 'redis'
3
+ gemspec
data/README.md CHANGED
@@ -19,21 +19,56 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- For public requests you must initialize a client of api with `BungieClient::Client`.
22
+ This gem contains two main classes: **BungieClient::Client**, **BungieClient::Wrappers::Default** and his inherits.
23
+
24
+ ### BungieClient::Client
25
+
26
+ It's main class that makes possible to send any requests to Bungie and connects auth module in one client. It needs for public/private requests to Bungie API.
27
+
28
+ **For this you should initialize your client for the next example:**
23
29
 
24
30
  ~~~~ruby
25
- client = BungieClient::Client.new :api_key => '1234'
31
+ @client = BungieClient::Client.new(
32
+ :api_key => 'YOUR_API_KEY',
33
+ :username => 'test@test.test',
34
+ :password => '1234',
35
+ :type => 'psn'
36
+ )
26
37
  ~~~~
27
38
 
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).
39
+ **or simply:**
40
+
41
+ ~~~~ruby
42
+ @client = BungieClient::Client.new :api_key => 'YOUR_API_KEY'
43
+ ~~~~
44
+
45
+ * 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).
46
+ * After it you can send your request to [Bungie Endpoint](http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints).
47
+ * 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).
48
+
49
+ #### How it initialized:
50
+
51
+ * Before working the client tries to authenticate in bungie, if you pass `username` and `password` option with account data, and after it uses cookies for next requests.
52
+ * If you want to store your cookies in any place you can define they with `cookies` option without authentication.
53
+ * After this operations your client is done for usage.
54
+
55
+ > The authentication optional for client, it requires only `api_key` in your hash for initializer.
56
+
57
+ #### Sending requests
29
58
 
30
- After it you can send your request to [Bungie Endpoint](http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints).
59
+ **Now you can send requests, e.g. for finding user information and getting his profile:**
31
60
 
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).
61
+ ~~~~ruby
62
+ @client.get_response "Destiny/SearchDestinyPlayer/2/RuBAN-GT"
63
+ ~~~~
64
+
65
+ #### Note
66
+
67
+ * For requests optimization you should use any caching of your requests.
33
68
 
34
69
  ### BungieClient::Auth
35
70
 
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.
71
+ 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
72
 
38
73
  **Example:**
39
74
 
@@ -45,126 +80,34 @@ jar = BungieClient::Auth.auth 'example@mail.com', 'example', 'psn'
45
80
  p BungieClient::Auth.auth_possible? (jar || [])
46
81
  ~~~~
47
82
 
48
- ### BungieClient::Cache
83
+ ### BungieClient::Wrappers::Default
49
84
 
50
- It's class created for easy wrapping another cache clients, e.g. [Redis](https://github.com/redis/redis-rb).
85
+ If you don't like long code as for me you should use **Wrappers**. It's classes can call api services with dynamically generated methods with snake case like name services. Also it can change url parameters to needed values and in inherits of this class it can be do automatically.
51
86
 
52
- **Examples of initialization:**
87
+ The initialization of **Wrappers::Default** is similar to **Client**: all arguments of initializer will be passed to **Client** which is class variable of wrapper.
53
88
 
54
89
  ~~~~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
- )
90
+ @wrapper = BungieClient::Wrappers::Default.new :api_key => 'YOUR_API_KEY'
63
91
  ~~~~
64
92
 
65
- **For Rails wrapper:**
93
+ Now you can sending your requests with beautiful and effective code:
66
94
 
67
95
  ~~~~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
- )
96
+ @wrapper.search_destiny_player :membershipType => '2', :displayName => 'RuBAN-GT'
74
97
  ~~~~
75
98
 
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:**
99
+ If you need **more** you can define your own wrappers such as **Wrappers::User**:
81
100
 
82
101
  ~~~~ruby
83
- client = BungieClient::Client.new(
102
+ @user = BungieClient::Wrappers::User.new(
84
103
  :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
- )
104
+ :display_name => 'RuBAN-GT',
105
+ :membership_type => '2'
95
106
  )
96
- ~~~~
97
-
98
- #### How it initialized:
99
107
 
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
108
+ @user.search_destiny_player
156
109
  ~~~~
157
110
 
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
111
  ## Contributing
169
112
 
170
113
  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.
@@ -176,3 +119,7 @@ The gem is available as open source under the terms of the [MIT License](http://
176
119
  ## Fireteam
177
120
 
178
121
  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)).
122
+
123
+ ## Note
124
+
125
+ * In the source code you can fine `services_parser.rb`. It's script created for getting full list of Bungie API services, for result it generates `services.yml` in lib.
@@ -18,9 +18,12 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = "~> 2.3.0"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.12"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
+
24
27
  spec.add_development_dependency "hashie", "~> 3.4"
25
28
  spec.add_development_dependency 'mechanize', "~> 2.7", ">= 2.7.0"
26
29
  end
@@ -1,17 +1,10 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
 
3
3
  require 'bungie_client'
4
- require 'redis'
5
4
 
6
5
  # init client
7
6
  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
- )
7
+ :api_key => 'YOUR_API_KEY'
15
8
  )
16
9
 
17
10
  # search account
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bungie_client'
4
+
5
+ # init client
6
+ @user = BungieClient::Wrappers::User.new(
7
+ :api_key => 'YOUR_API_KEY',
8
+ :display_name => 'RuBAN-GT',
9
+ :membership_type => '2'
10
+ )
11
+
12
+ p @user.get_bungie_account.keys
data/lib/bungie_client.rb CHANGED
@@ -5,4 +5,6 @@ require "mechanize"
5
5
  require "bungie_client/version"
6
6
  require "bungie_client/auth"
7
7
  require "bungie_client/client"
8
- require "bungie_client/cache"
8
+ require "bungie_client/service"
9
+ require "bungie_client/wrappers/default"
10
+ require "bungie_client/wrappers/user"
@@ -37,12 +37,16 @@ module BungieClient::Auth
37
37
  # @return [Mechanize::CookieJar|nil]
38
38
  def auth(username, password, type = 'psn')
39
39
  # client init
40
- agent = Mechanize.new
40
+ agent = Mechanize.new do |config|
41
+ config.open_timeout = 60
42
+ config.read_timeout = 60
43
+ config.idle_timeout = 120
44
+ end
41
45
 
42
46
  # getting index page
43
47
  agent.get 'https://www.bungie.net/' do |page|
44
48
  result = nil
45
- link = page.link_with :text => search_query(type)
49
+ link = page.link_with :text => search_query(type)
46
50
 
47
51
  unless link.nil?
48
52
  # call auth page
@@ -65,7 +69,7 @@ module BungieClient::Auth
65
69
  else
66
70
  # ms wanted enabled js, but we can send form without it
67
71
  ppft = login.body.match(/name\="PPFT"(.*)value\="(.*?)"/)
68
- url = login.body.match(/urlPost\:'(.*?)'/)
72
+ url = login.body.match(/urlPost\:'(.*?)'/)
69
73
 
70
74
  if !ppft.nil? && !url.nil?
71
75
  result = agent.post url.to_a.last,
@@ -11,12 +11,30 @@ class BungieClient::Client
11
11
  "#{BUNGIE_URI}/#{uri.sub(/^\//, '').sub(/\/$/, '')}/"
12
12
  end
13
13
 
14
+ # Format answer from bungie
15
+ #
16
+ # @param [String] response
17
+ #
18
+ # @return [Hashie::Mash]
19
+ def self.parse_response(response)
20
+ if !response.nil? && response != ''
21
+ response = JSON.parse response
22
+
23
+ if response.is_a?(Hash) && !response['Response'].nil? && response['ErrorCode'] == 1
24
+ response = Hashie::Mash.new response
25
+
26
+ return response['Response']
27
+ end
28
+ end
29
+
30
+ Hashie::Mash.new
31
+ end
32
+
14
33
  attr_reader :api_key
15
34
  attr_reader :username
16
35
  attr_reader :password
17
36
  attr_reader :type
18
37
  attr_reader :agent
19
- attr_reader :cache
20
38
 
21
39
  # Init client
22
40
  #
@@ -26,9 +44,7 @@ class BungieClient::Client
26
44
  #
27
45
  # @param [Hash] options
28
46
  # @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
47
+ # @option options [Array|CookieJar] :cookies with [HTTP::Cookie] or [CookieJar]
32
48
  # @option options [String] :username username for authentication, necessary if set :authenticate
33
49
  # @option options [String] :password password of user, necessary if set :authenticate
34
50
  # @option options [String] :type of account, it can be 'psn' or 'live'
@@ -42,59 +58,32 @@ class BungieClient::Client
42
58
  @api_key = options[:api_key].to_s
43
59
  end
44
60
 
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
61
  # 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
62
+ @agent = Mechanize.new do |config|
63
+ config.read_timeout = 5
70
64
  end
71
65
 
72
66
  # merge cookies with options
73
67
  if BungieClient::Auth.valid_cookies? options[:cookies], true
74
- cookies = (options[:cookies].is_a? Array) ? cookies : cookies.cookies
68
+ cookies = (options[:cookies].is_a? CookieJar) ? options[:cookies].cookies : options[:cookies]
75
69
 
76
70
  cookies.each do |cookie|
77
71
  @agent.cookie_jar.add cookie
78
72
  end
79
73
  end unless options[:cookies].nil?
80
- end
81
74
 
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
75
+ # make authentication and save new cookies in client
76
+ unless options[:username].nil? || options[:password].nil?
77
+ jar = BungieClient::Auth.auth options[:username].to_s, options[:password].to_s, (options[:type].to_s || 'psn')
90
78
 
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
79
+ if jar.nil?
80
+ raise "Wrong authentication. Check your account data."
81
+ else
82
+ jar.cookies.each do |cookie|
83
+ @agent.cookie_jar.add cookie
84
+ end
85
+ end
86
+ end
98
87
  end
99
88
 
100
89
  # Get response from bungie services
@@ -109,38 +98,14 @@ class BungieClient::Client
109
98
  @agent.get(self.class.request_uri(uri), parameters, nil, headers).body rescue nil
110
99
  end
111
100
 
112
- # Get Response field after get request to bungie
101
+ # Get Response field after sending GET request to bungie
113
102
  #
114
103
  # @param [String] uri
115
104
  # @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
- # @option options [Boolean] :cache_only - allow get result only from cache
121
105
  #
122
- # @return [Hash|nil]
123
- def get_response(uri, parameters = {}, options = {})
124
- if allow_get_cache options
125
- result = @cache.get "#{uri}+#{parameters}"
126
-
127
- return result unless result.nil?
128
- end
129
- return nil if options[:cache_only]
130
-
131
- result = get uri, parameters
132
-
133
- if !result.nil? && result != ''
134
- result = JSON.parse result
135
-
136
- if result.is_a?(Hash) && !result['Response'].nil? && result['ErrorCode'] == 1
137
- result = Hashie::Mash.new result
138
-
139
- @cache.set "#{uri}+#{parameters}", result['Response'], options[:cache_ttl] if allow_set_cache options
140
-
141
- result['Response']
142
- end
143
- end
106
+ # @return [Hashie::Mash]
107
+ def get_response(uri, parameters = {})
108
+ self.class.parse_response get(uri, parameters)
144
109
  end
145
110
 
146
111
  # Post data to bungie services
@@ -158,19 +123,9 @@ class BungieClient::Client
158
123
  # @param [String] uri
159
124
  # @param [Hash] query for post
160
125
  #
161
- # @return [Hash|nil]
126
+ # @return [Hashie::Mash]
162
127
  def post_response(uri, query = {})
163
- result = post uri, query
164
-
165
- if !result.nil? && result != ''
166
- result = JSON.parse result
167
-
168
- if result.is_a?(Hash) && !result['Response'].nil? && result['ErrorCode'] == 1
169
- result = Hashie::Mash.new result
170
-
171
- result['Response']
172
- end
173
- end
128
+ self.class.parse_response post(uri, query)
174
129
  end
175
130
 
176
131
  protected