bungie_client 1.0.0 → 1.0.1

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: dbcc8ccd4f843c09b035c1cadf0d3104cc2cb6c2
4
- data.tar.gz: d3e03ebaae92ef497c8388b02f3a1239d7501965
3
+ metadata.gz: 4ccf05da80fe1ab4ea8c4079a8745910cb9e314d
4
+ data.tar.gz: f18c2c38bea8190ff3da9cc22dec541b0681fc65
5
5
  SHA512:
6
- metadata.gz: 125fcbc0d2023e58cf1c189d88a0ea4ba781696d138f495dd3464d2d9ed2f554f18470d1be1ec156c092abea888c1b0ef0a412231d4cd98db244618ba1231a12
7
- data.tar.gz: 58abe874906be78e8859167be290386b048ecd8632935b6123e9dc30562e0672b4f622ddde7a538821abc76808337d5840e2550b8f8d02a6a0294b9fb0b2aebb
6
+ metadata.gz: 8346d4f33f066079679c8d62b6b1de8d3f8fbd30906cdd5859fabd58713820ab79a2cd29201a9293eca474bbf1513430152f63f30402ab2df2a94ead8b1fd721
7
+ data.tar.gz: da0cbdd11820a827f7795644ce633de03e8169d1aedf368327f0ec940c6f8504ccc9670a14c0b32341543227e73929636bdfe7d3085acf1eec4ea73d3d48883a
data/.gitignore CHANGED
File without changes
data/CODE_OF_CONDUCT.md CHANGED
File without changes
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
data/bin/console CHANGED
File without changes
data/bin/setup CHANGED
File without changes
File without changes
@@ -1,10 +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 || [])
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 || [])
@@ -1,16 +1,16 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
-
3
- require 'bungie_client'
4
-
5
- # init client
6
- client = BungieClient::Client.new(
7
- :api_key => 'YOUR_API_KEY'
8
- )
9
-
10
- # search account
11
- s = client.get_response "Destiny/SearchDestinyPlayer/2/RuBAN-GT"
12
-
13
- p s = s.first if !s.nil? && s.length == 1
14
-
15
- # get profile with characters
16
- p client.get_response "Destiny/#{s['membershipType']}/Account/#{s['membershipId']}" unless s.nil?
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bungie_client'
4
+
5
+ # init client
6
+ client = BungieClient::Client.new(
7
+ :api_key => 'YOUR_API_KEY'
8
+ )
9
+
10
+ # search account
11
+ s = client.get_response "Destiny/SearchDestinyPlayer/2/RuBAN-GT"
12
+
13
+ p s = s.first if !s.nil? && s.length == 1
14
+
15
+ # get profile with characters
16
+ p client.get_response "Destiny/#{s['membershipType']}/Account/#{s['membershipId']}" unless s.nil?
@@ -1,12 +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
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
File without changes
@@ -1,140 +1,140 @@
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 do |config|
41
- config.open_timeout = 60
42
- config.read_timeout = 60
43
- config.idle_timeout = 120
44
- end
45
-
46
- # getting index page
47
- agent.get 'https://www.bungie.net/' do |page|
48
- result = nil
49
- link = page.link_with :text => search_query(type)
50
-
51
- unless link.nil?
52
- # call auth page
53
- login = agent.click link
54
-
55
- # sending form for sony or ms
56
- if type == 'psn'
57
- form = login.forms.first
58
- unless form.nil?
59
- name = form.field_with(:type => 'email')
60
- pass = form.field_with(:type => 'password')
61
-
62
- if !name.nil? && !pass.nil?
63
- name.value = username
64
- pass.value = password
65
-
66
- result = form.click_button
67
- end
68
- end
69
- else
70
- # ms wanted enabled js, but we can send form without it
71
- ppft = login.body.match(/name\="PPFT"(.*)value\="(.*?)"/)
72
- url = login.body.match(/urlPost\:'(.*?)'/)
73
-
74
- if !ppft.nil? && !url.nil?
75
- result = agent.post url.to_a.last,
76
- 'login' => username,
77
- 'passwd' => password,
78
- 'KMSI' => 1,
79
- 'PPFT' => ppft.to_a.last
80
- end
81
- end
82
- end
83
-
84
- # if result not nil and after authentication we returned to bungie, all ok
85
- if !result.nil? && result.uri.to_s.include?('?code')
86
- needed = %w(bungled bungleatk bungledid bungles)
87
- output = Mechanize::CookieJar.new
88
-
89
- agent.cookies.each do |cookie|
90
- output.add cookie if needed.include? cookie.name
91
- end
92
-
93
- return output
94
- end
95
- end
96
-
97
- nil
98
- end
99
-
100
- # Try authenticate with cookies
101
- #
102
- # @param [Array|CookieJar] cookies array with [HTTP::Cookie] or [CookieJar]
103
- #
104
- # @return [Boolean]
105
- def auth_possible?(cookies)
106
- agent = Mechanize.new
107
-
108
- valid_cookies? cookies, true
109
-
110
- if cookies.is_a? Array
111
- cookies.each do |cookie|
112
- agent.cookie_jar.add cookie
113
- end
114
- else
115
- agent.cookie_jar = cookies
116
- end
117
-
118
- result = nil
119
- agent.get 'https://www.bungie.net/' do |page|
120
- link = page.link_with :text => search_query('psn')
121
-
122
- result = agent.click link unless link.nil?
123
- end
124
-
125
- !result.nil? && result.uri.host == "www.bungie.net"
126
- end
127
-
128
- protected
129
-
130
- def search_query(type)
131
- if type == 'psn'
132
- 'PlayStation Network'
133
- elsif type == 'live'
134
- 'Xbox Live'
135
- else
136
- raise 'Wrong account type!'
137
- end
138
- end
139
- end
140
- end
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 do |config|
41
+ config.open_timeout = 60
42
+ config.read_timeout = 60
43
+ config.idle_timeout = 120
44
+ end
45
+
46
+ # getting index page
47
+ agent.get 'https://www.bungie.net/' do |page|
48
+ result = nil
49
+ link = page.link_with :text => search_query(type)
50
+
51
+ unless link.nil?
52
+ # call auth page
53
+ login = agent.click link
54
+
55
+ # sending form for sony or ms
56
+ if type == 'psn'
57
+ form = login.forms.first
58
+ unless form.nil?
59
+ name = form.field_with(:type => 'email')
60
+ pass = form.field_with(:type => 'password')
61
+
62
+ if !name.nil? && !pass.nil?
63
+ name.value = username
64
+ pass.value = password
65
+
66
+ result = form.click_button
67
+ end
68
+ end
69
+ else
70
+ # ms wanted enabled js, but we can send form without it
71
+ ppft = login.body.match(/name\="PPFT"(.*)value\="(.*?)"/)
72
+ url = login.body.match(/urlPost\:'(.*?)'/)
73
+
74
+ if !ppft.nil? && !url.nil?
75
+ result = agent.post url.to_a.last,
76
+ 'login' => username,
77
+ 'passwd' => password,
78
+ 'KMSI' => 1,
79
+ 'PPFT' => ppft.to_a.last
80
+ end
81
+ end
82
+ end
83
+
84
+ # if result not nil and after authentication we returned to bungie, all ok
85
+ if !result.nil? && result.uri.to_s.include?('?code')
86
+ needed = %w(bungled bungleatk bungledid bungles)
87
+ output = Mechanize::CookieJar.new
88
+
89
+ agent.cookies.each do |cookie|
90
+ output.add cookie if needed.include? cookie.name
91
+ end
92
+
93
+ return output
94
+ end
95
+ end
96
+
97
+ nil
98
+ end
99
+
100
+ # Try authenticate with cookies
101
+ #
102
+ # @param [Array|CookieJar] cookies array with [HTTP::Cookie] or [CookieJar]
103
+ #
104
+ # @return [Boolean]
105
+ def auth_possible?(cookies)
106
+ agent = Mechanize.new
107
+
108
+ valid_cookies? cookies, true
109
+
110
+ if cookies.is_a? Array
111
+ cookies.each do |cookie|
112
+ agent.cookie_jar.add cookie
113
+ end
114
+ else
115
+ agent.cookie_jar = cookies
116
+ end
117
+
118
+ result = nil
119
+ agent.get 'https://www.bungie.net/' do |page|
120
+ link = page.link_with :text => search_query('psn')
121
+
122
+ result = agent.click link unless link.nil?
123
+ end
124
+
125
+ !result.nil? && result.uri.host == "www.bungie.net"
126
+ end
127
+
128
+ protected
129
+
130
+ def search_query(type)
131
+ if type == 'psn'
132
+ 'PlayStation Network'
133
+ elsif type == 'live'
134
+ 'Xbox Live'
135
+ else
136
+ raise 'Wrong account type!'
137
+ end
138
+ end
139
+ end
140
+ end
@@ -1,141 +1,141 @@
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
- # 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
-
33
- attr_reader :api_key
34
- attr_reader :username
35
- attr_reader :password
36
- attr_reader :type
37
- attr_reader :agent
38
-
39
- # Init client
40
- #
41
- # @see https://www.bungie.net/en/user/api
42
- #
43
- # Initialize client for bungie api throw hash:
44
- #
45
- # @param [Hash] options
46
- # @option options [String] :api_key
47
- # @option options [Array|CookieJar] :cookies with [HTTP::Cookie] or [CookieJar]
48
- # @option options [String] :username username for authentication, necessary if set :authenticate
49
- # @option options [String] :password password of user, necessary if set :authenticate
50
- # @option options [String] :type of account, it can be 'psn' or 'live'
51
- def initialize(options)
52
- # checking options and @api_key
53
- raise 'Wrong options: It must be Hash.' unless options.is_a? Hash
54
-
55
- if options[:api_key].nil?
56
- raise "The API-key required for every request to bungie."
57
- else
58
- @api_key = options[:api_key].to_s
59
- end
60
-
61
- # init @agent
62
- @agent = Mechanize.new do |config|
63
- config.read_timeout = 5
64
- end
65
-
66
- # merge cookies with options
67
- if BungieClient::Auth.valid_cookies? options[:cookies], true
68
- cookies = (options[:cookies].is_a? CookieJar) ? options[:cookies].cookies : options[:cookies]
69
-
70
- cookies.each do |cookie|
71
- @agent.cookie_jar.add cookie
72
- end
73
- end unless options[:cookies].nil?
74
-
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')
78
-
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
87
- end
88
-
89
- # Get response from bungie services
90
- #
91
- # @see http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints
92
- #
93
- # @param [String] uri
94
- # @param [Hash|Array] parameters for http-query
95
- #
96
- # @return [String|nil]
97
- def get(uri, parameters = {})
98
- @agent.get(self.class.request_uri(uri), parameters, nil, headers).body rescue nil
99
- end
100
-
101
- # Get Response field after sending GET request to bungie
102
- #
103
- # @param [String] uri
104
- # @param [Hash|Array] parameters for http-query
105
- #
106
- # @return [Hashie::Mash]
107
- def get_response(uri, parameters = {})
108
- self.class.parse_response get(uri, parameters)
109
- end
110
-
111
- # Post data to bungie services
112
- #
113
- # @param [String] uri
114
- # @param [Hash] query
115
- #
116
- # @return [String|nil]
117
- def post(uri, query = {})
118
- @agent.post(self.class.request_uri(uri), query, headers).body rescue nil
119
- end
120
-
121
- # Get Response field after post request to bungie
122
- #
123
- # @param [String] uri
124
- # @param [Hash] query for post
125
- #
126
- # @return [Hashie::Mash]
127
- def post_response(uri, query = {})
128
- self.class.parse_response post(uri, query)
129
- end
130
-
131
- protected
132
-
133
- # Headers for requests
134
- def headers
135
- {
136
- 'Accept' => 'json',
137
- 'Content-Type' => 'json',
138
- 'X-API-Key' => @api_key
139
- }
140
- end
141
- end
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
+ # 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
+
33
+ attr_reader :api_key
34
+ attr_reader :username
35
+ attr_reader :password
36
+ attr_reader :type
37
+ attr_reader :agent
38
+
39
+ # Init client
40
+ #
41
+ # @see https://www.bungie.net/en/user/api
42
+ #
43
+ # Initialize client for bungie api throw hash:
44
+ #
45
+ # @param [Hash] options
46
+ # @option options [String] :api_key
47
+ # @option options [Array|CookieJar] :cookies with [HTTP::Cookie] or [CookieJar]
48
+ # @option options [String] :username username for authentication, necessary if set :authenticate
49
+ # @option options [String] :password password of user, necessary if set :authenticate
50
+ # @option options [String] :type of account, it can be 'psn' or 'live'
51
+ def initialize(options)
52
+ # checking options and @api_key
53
+ raise 'Wrong options: It must be Hash.' unless options.is_a? Hash
54
+
55
+ if options[:api_key].nil?
56
+ raise "The API-key required for every request to bungie."
57
+ else
58
+ @api_key = options[:api_key].to_s
59
+ end
60
+
61
+ # init @agent
62
+ @agent = Mechanize.new do |config|
63
+ config.read_timeout = 5
64
+ end
65
+
66
+ # merge cookies with options
67
+ if BungieClient::Auth.valid_cookies? options[:cookies], true
68
+ cookies = (options[:cookies].is_a? CookieJar) ? options[:cookies].cookies : options[:cookies]
69
+
70
+ cookies.each do |cookie|
71
+ @agent.cookie_jar.add cookie
72
+ end
73
+ end unless options[:cookies].nil?
74
+
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')
78
+
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
87
+ end
88
+
89
+ # Get response from bungie services
90
+ #
91
+ # @see http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints
92
+ #
93
+ # @param [String] uri
94
+ # @param [Hash|Array] parameters for http-query
95
+ #
96
+ # @return [String|nil]
97
+ def get(uri, parameters = {})
98
+ @agent.get(self.class.request_uri(uri), parameters, nil, headers).body rescue nil
99
+ end
100
+
101
+ # Get Response field after sending GET request to bungie
102
+ #
103
+ # @param [String] uri
104
+ # @param [Hash|Array] parameters for http-query
105
+ #
106
+ # @return [Hashie::Mash]
107
+ def get_response(uri, parameters = {})
108
+ self.class.parse_response get(uri, parameters)
109
+ end
110
+
111
+ # Post data to bungie services
112
+ #
113
+ # @param [String] uri
114
+ # @param [Hash] query
115
+ #
116
+ # @return [String|nil]
117
+ def post(uri, query = {})
118
+ @agent.post(self.class.request_uri(uri), query, headers).body rescue nil
119
+ end
120
+
121
+ # Get Response field after post request to bungie
122
+ #
123
+ # @param [String] uri
124
+ # @param [Hash] query for post
125
+ #
126
+ # @return [Hashie::Mash]
127
+ def post_response(uri, query = {})
128
+ self.class.parse_response post(uri, query)
129
+ end
130
+
131
+ protected
132
+
133
+ # Headers for requests
134
+ def headers
135
+ {
136
+ 'Accept' => 'json',
137
+ 'Content-Type' => 'json',
138
+ 'X-API-Key' => @api_key
139
+ }
140
+ end
141
+ end
@@ -1,10 +1,10 @@
1
- # Simple class for service structure
2
- class BungieClient::Service
3
- attr_reader :method_type, :name, :endpoint
4
-
5
- def initialize(options)
6
- @method_type = ((%w(get post).include? options[:method]) ? options[:method] : 'get')
7
- @name = options[:name].to_s
8
- @endpoint = options[:endpoint].to_s
9
- end
1
+ # Simple class for service structure
2
+ class BungieClient::Service
3
+ attr_reader :method_type, :name, :endpoint
4
+
5
+ def initialize(options)
6
+ @method_type = ((%w(get post).include? options[:method]) ? options[:method] : 'get')
7
+ @name = options[:name].to_s
8
+ @endpoint = options[:endpoint].to_s
9
+ end
10
10
  end
File without changes
@@ -1,3 +1,3 @@
1
- module BungieClient
2
- VERSION = '1.0.0'
3
- end
1
+ module BungieClient
2
+ VERSION = '1.0.1'
3
+ end
@@ -1,84 +1,84 @@
1
- # Wrapper class for simple api requests
2
- module BungieClient::Wrappers
3
- class Default
4
- # Get list of services
5
- #
6
- # @see http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints
7
- #
8
- # @return [Hash]
9
- def self.services
10
- return @services unless @services.nil?
11
-
12
- @services = YAML.load_file "#{File.dirname(__FILE__)}/../services.yml" || {}
13
- end
14
-
15
- # Initialize wrapper with client
16
- #
17
- # This initializer create wrapper object with client.
18
- # If you `options` contain `:client` key, it will be taken as client object [BungieClient::Client]. Otherwise it will be passed in client initializer.
19
- #
20
- # @see BungieClient::Client
21
- #
22
- # @param [Hash] options
23
- def initialize(options)
24
- if options[:client].nil?
25
- @options = options
26
- elsif options[:client].is_a? BungieClient::Client
27
- @client = options[:client]
28
- end
29
- end
30
-
31
- # Get wrapper client
32
- #
33
- # @return [BungieClient::Client]
34
- def client
35
- return @client unless @client.nil?
36
-
37
- @client = BungieClient::Client.new @options
38
- @options = nil
39
-
40
- @client
41
- end
42
-
43
- # Change all url parameters to hash value
44
- #
45
- # @param [String] url
46
- # @param [Hash] params
47
- #
48
- # @return [String]
49
- def fill_url(url, params)
50
- params.each do |key, value|
51
- url.gsub! "{#{key}}", value.to_s
52
- end
53
-
54
- url
55
- end
56
-
57
- # Call needed service from services list
58
- #
59
- # @param [String] service name in snake case
60
- # @param [Hash] params service parameters
61
- # @param [Hash] options for client request (get/post)
62
- #
63
- # @return [Hashie::Mash]
64
- def call_service(service, params = {}, options = {})
65
- # try to fine service
66
- service = self.class.services[service]
67
-
68
- raise NoMethodError if service.nil?
69
-
70
- # init service
71
- service = BungieClient::Service.new service
72
-
73
- # change url
74
- url = self.fill_url service.endpoint, params
75
-
76
- # send request
77
- client.send "#{service.method_type}_response", url, options
78
- end
79
-
80
- def method_missing(*args)
81
- call_service args[0].to_s, args[1], args[2]
82
- end
83
- end
84
- end
1
+ # Wrapper class for simple api requests
2
+ module BungieClient::Wrappers
3
+ class Default
4
+ # Get list of services
5
+ #
6
+ # @see http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints
7
+ #
8
+ # @return [Hash]
9
+ def self.services
10
+ return @services unless @services.nil?
11
+
12
+ @services = YAML.load_file "#{File.dirname(__FILE__)}/../services.yml" || {}
13
+ end
14
+
15
+ # Initialize wrapper with client
16
+ #
17
+ # This initializer create wrapper object with client.
18
+ # If you `options` contain `:client` key, it will be taken as client object [BungieClient::Client]. Otherwise it will be passed in client initializer.
19
+ #
20
+ # @see BungieClient::Client
21
+ #
22
+ # @param [Hash] options
23
+ def initialize(options)
24
+ if options[:client].nil?
25
+ @options = options
26
+ elsif options[:client].is_a? BungieClient::Client
27
+ @client = options[:client]
28
+ end
29
+ end
30
+
31
+ # Get wrapper client
32
+ #
33
+ # @return [BungieClient::Client]
34
+ def client
35
+ return @client unless @client.nil?
36
+
37
+ @client = BungieClient::Client.new @options
38
+ @options = nil
39
+
40
+ @client
41
+ end
42
+
43
+ # Change all url parameters to hash value
44
+ #
45
+ # @param [String] url
46
+ # @param [Hash] params
47
+ #
48
+ # @return [String]
49
+ def fill_url(url, params)
50
+ params.each do |key, value|
51
+ url = url.gsub "{#{key}}", value.to_s
52
+ end
53
+
54
+ url
55
+ end
56
+
57
+ # Call needed service from services list
58
+ #
59
+ # @param [String] service name in snake case
60
+ # @param [Hash] params service parameters
61
+ # @param [Hash] options for client request (get/post)
62
+ #
63
+ # @return [Hashie::Mash]
64
+ def call_service(service, params = {}, options = {})
65
+ # try to fine service
66
+ service = self.class.services[service]
67
+
68
+ raise NoMethodError if service.nil?
69
+
70
+ # init service
71
+ service = BungieClient::Service.new service
72
+
73
+ # change url
74
+ url = self.fill_url service.endpoint, params
75
+
76
+ # send request
77
+ client.send "#{service.method_type}_response", url, options
78
+ end
79
+
80
+ def method_missing(*args)
81
+ call_service args[0].to_s, args[1], args[2]
82
+ end
83
+ end
84
+ end
@@ -1,53 +1,53 @@
1
- module BungieClient::Wrappers
2
- class User < Default
3
- attr_reader :membership_type, :display_name
4
-
5
- # Initialize wrapper with user's preset
6
- #
7
- # @note maybe add the `destiny_membership_id` as an alternative to `display_name`
8
- #
9
- # @option options [Integer] :membership_type Platform type number (xbox - 1, playstation - 2, all - 256)
10
- # @option options [String] :display_name Simple user name
11
- def initialize(options)
12
- super
13
-
14
- # set membershipType
15
- if %w(1 2 256).include? options[:membership_type].to_s
16
- @membership_type = options[:membership_type].to_s
17
- else
18
- @membership_type = '256'
19
- end
20
-
21
- # set displayName
22
- if options[:display_name].is_a? String
23
- @display_name = options[:display_name]
24
- else
25
- raise 'You must set user display name'
26
- end
27
- end
28
-
29
- # Get DestinyMembershipId of selected user
30
- def destiny_membership_id
31
- return @destiny_membership_id unless @destiny_membership_id.nil?
32
-
33
- @destiny_membership_id = call_service(
34
- 'get_membership_id_by_display_name',
35
- {
36
- :displayName => @display_name,
37
- :membershipType => @membership_type
38
- }
39
- )
40
- end
41
-
42
- def fill_url(url, params)
43
- params = {} unless params.is_a? Hash
44
-
45
- params[:displayName] = @display_name
46
- params[:membershipType] = @membership_type
47
- params[:destinyMembershipId] = destiny_membership_id if url.include? '{destinyMembershipId}'
48
- params[:membershipId] = destiny_membership_id if url.include? '{membershipId}'
49
-
50
- super
51
- end
52
- end
1
+ module BungieClient::Wrappers
2
+ class User < Default
3
+ attr_reader :membership_type, :display_name
4
+
5
+ # Initialize wrapper with user's preset
6
+ #
7
+ # @note maybe add the `destiny_membership_id` as an alternative to `display_name`
8
+ #
9
+ # @option options [Integer] :membership_type Platform type number (xbox - 1, playstation - 2, all - 256)
10
+ # @option options [String] :display_name Simple user name
11
+ def initialize(options)
12
+ super
13
+
14
+ # set membershipType
15
+ if %w(1 2 256).include? options[:membership_type].to_s
16
+ @membership_type = options[:membership_type].to_s
17
+ else
18
+ @membership_type = '256'
19
+ end
20
+
21
+ # set displayName
22
+ if options[:display_name].is_a? String
23
+ @display_name = options[:display_name]
24
+ else
25
+ raise 'You must set user display name'
26
+ end
27
+ end
28
+
29
+ # Get DestinyMembershipId of selected user
30
+ def destiny_membership_id
31
+ return @destiny_membership_id unless @destiny_membership_id.nil?
32
+
33
+ @destiny_membership_id = call_service(
34
+ 'get_membership_id_by_display_name',
35
+ {
36
+ :displayName => @display_name,
37
+ :membershipType => @membership_type
38
+ }
39
+ )
40
+ end
41
+
42
+ def fill_url(url, params)
43
+ params = {} unless params.is_a? Hash
44
+
45
+ params[:displayName] = @display_name
46
+ params[:membershipType] = @membership_type
47
+ params[:destinyMembershipId] = destiny_membership_id if url.include? '{destinyMembershipId}'
48
+ params[:membershipId] = destiny_membership_id if url.include? '{membershipId}'
49
+
50
+ super
51
+ end
52
+ end
53
53
  end
data/lib/underscore.rb CHANGED
@@ -1,10 +1,10 @@
1
- class String
2
- # Transform string to snake case
3
- def underscore
4
- self.gsub(/::/, '/').
5
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
- tr("- ", "_").
8
- downcase
9
- end
1
+ class String
2
+ # Transform string to snake case
3
+ def underscore
4
+ self.gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
+ tr("- ", "_").
8
+ downcase
9
+ end
10
10
  end
data/services_parser.rb CHANGED
@@ -1,33 +1,33 @@
1
- # This script can parse the list of bungie services from *destinydevs.github.io* for client.
2
-
3
- require 'yaml'
4
- require 'hashie'
5
- require 'mechanize'
6
- require 'underscore'
7
-
8
- # preset
9
- client = Mechanize.new
10
- url = 'http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints'
11
- services = {}
12
-
13
- # get services
14
- client.get url do |page|
15
- trs = page.parser.search '.container .table tbody tr'
16
-
17
- trs.each do |tr|
18
- tds = tr.search 'td'
19
-
20
- next if tds.nil? || tds[1].nil?
21
-
22
- services[tds[1].text.underscore] = {
23
- :name => tds[1].text,
24
- :method => (tds[0]&.text.downcase || 'get'),
25
- :endpoint => (tds[2]&.text || '')
26
- }
27
- end unless trs.nil?
28
- end
29
-
30
- # save yaml
31
- File.open "#{File.dirname(__FILE__)}/lib/bungie_client/services.yml", 'w' do |f|
32
- f.write services.to_yaml
1
+ # This script can parse the list of bungie services from *destinydevs.github.io* for client.
2
+
3
+ require 'yaml'
4
+ require 'hashie'
5
+ require 'mechanize'
6
+ require 'underscore'
7
+
8
+ # preset
9
+ client = Mechanize.new
10
+ url = 'http://destinydevs.github.io/BungieNetPlatform/docs/Endpoints'
11
+ services = {}
12
+
13
+ # get services
14
+ client.get url do |page|
15
+ trs = page.parser.search '.container .table tbody tr'
16
+
17
+ trs.each do |tr|
18
+ tds = tr.search 'td'
19
+
20
+ next if tds.nil? || tds[1].nil?
21
+
22
+ services[tds[1].text.underscore] = {
23
+ :name => tds[1].text,
24
+ :method => (tds[0]&.text.downcase || 'get'),
25
+ :endpoint => (tds[2]&.text || '')
26
+ }
27
+ end unless trs.nil?
28
+ end
29
+
30
+ # save yaml
31
+ File.open "#{File.dirname(__FILE__)}/lib/bungie_client/services.yml", 'w' do |f|
32
+ f.write services.to_yaml
33
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bungie_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Ruban
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-03 00:00:00.000000000 Z
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler