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.
@@ -1,3 +1,3 @@
1
1
  module BungieClient
2
- VERSION = '0.6.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -0,0 +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
@@ -0,0 +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
53
+ end
data/lib/underscore.rb ADDED
@@ -0,0 +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
10
+ end
@@ -0,0 +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
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: 0.6.0
4
+ version: 1.0.0
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-09-30 00:00:00.000000000 Z
11
+ date: 2016-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,11 +108,17 @@ files:
108
108
  - bungie_client.gemspec
109
109
  - examples/auth_usage.rb
110
110
  - examples/client_usage.rb
111
+ - examples/wrapper_usage.rb
111
112
  - lib/bungie_client.rb
112
113
  - lib/bungie_client/auth.rb
113
- - lib/bungie_client/cache.rb
114
114
  - lib/bungie_client/client.rb
115
+ - lib/bungie_client/service.rb
116
+ - lib/bungie_client/services.yml
115
117
  - lib/bungie_client/version.rb
118
+ - lib/bungie_client/wrappers/default.rb
119
+ - lib/bungie_client/wrappers/user.rb
120
+ - lib/underscore.rb
121
+ - services_parser.rb
116
122
  homepage: https://github.com/RuBAN-GT/bungie_client
117
123
  licenses:
118
124
  - MIT
@@ -123,9 +129,9 @@ require_paths:
123
129
  - lib
124
130
  required_ruby_version: !ruby/object:Gem::Requirement
125
131
  requirements:
126
- - - ">="
132
+ - - "~>"
127
133
  - !ruby/object:Gem::Version
128
- version: '0'
134
+ version: 2.3.0
129
135
  required_rubygems_version: !ruby/object:Gem::Requirement
130
136
  requirements:
131
137
  - - ">="
@@ -1,53 +0,0 @@
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