halo-api 0.3.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -1
- data/.rspec +2 -1
- data/.travis.yml +3 -3
- data/README.md +13 -1
- data/halo-api.gemspec +7 -6
- data/lib/halo-api/api_response.rb +51 -8
- data/lib/halo-api/client.rb +20 -18
- data/lib/halo-api/configuration.rb +4 -2
- data/lib/halo-api/version.rb +1 -1
- metadata +25 -13
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d241a3a1fe1f27c5cb995fafd2d02b4a9d861a9e
|
4
|
+
data.tar.gz: 2f16787de453f9975aba2c683ef74b4cebc9ab14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98e71bf3bb6b85f0d9386ed8c9e83615e11a5a2abdb4228817af018557c4aebfd596d0c48a4a173b0acacbfa4e8efdbc3edfb27ee5e9fa1cd808504283cfbc82
|
7
|
+
data.tar.gz: b9d1cc843c195d556b896ccc87fb7d7e3656ca46ac40a68103102ac37914e6853b6e0dc54bc618d6f27aa0ce7373a78865c7b79ae723fb8fca8a0e64fc4da511
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -25,11 +25,19 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
Basic
|
28
|
+
Basic configuration:
|
29
|
+
````ruby
|
30
|
+
Halo.configure do |config|
|
31
|
+
config.api_key = ENV['HALO_API_KEY']
|
32
|
+
end
|
33
|
+
````
|
34
|
+
|
35
|
+
With redis and region (optional parameters)
|
29
36
|
````ruby
|
30
37
|
Halo.configure do |config|
|
31
38
|
config.api_key = ENV['HALO_API_KEY']
|
32
39
|
config.region = ENV['HALO_REGION']
|
40
|
+
config.redis = ENV['HALO_REDIS']
|
33
41
|
end
|
34
42
|
````
|
35
43
|
|
@@ -47,6 +55,10 @@ h2w_campaign_levels = halo_wars2_client.metadata.campaign_levels
|
|
47
55
|
|
48
56
|
- [x] Halo 5 endpoints
|
49
57
|
- [x] Halo Wars 2 endpoints
|
58
|
+
- [x] Redis response caching support
|
59
|
+
- [ ] Request rate limiter
|
60
|
+
- [ ] Custom query builder
|
61
|
+
- [ ] Paged requests
|
50
62
|
- [ ] Documentation
|
51
63
|
|
52
64
|
## Contributing
|
data/halo-api.gemspec
CHANGED
@@ -22,18 +22,19 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
-
# This gem will work with 2.
|
26
|
-
spec.required_ruby_version = '>= 2.
|
25
|
+
# This gem will work with 2.2.2 or greater
|
26
|
+
spec.required_ruby_version = '>= 2.2.2'
|
27
27
|
|
28
28
|
spec.add_runtime_dependency 'httparty', '~> 0.15.6'
|
29
29
|
spec.add_runtime_dependency 'addressable', '~> 2.5', '>= 2.5.2'
|
30
|
+
spec.add_runtime_dependency 'redis', '~> 4.0'
|
30
31
|
|
31
32
|
spec.add_development_dependency 'bundler', '~> 1.15'
|
32
|
-
spec.add_development_dependency 'rake', '~> 12.
|
33
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
33
|
+
spec.add_development_dependency 'rake', '~> 12.1'
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.6'
|
34
35
|
spec.add_development_dependency 'dotenv', '~> 2.2', '>= 2.2.1'
|
35
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
36
|
+
spec.add_development_dependency 'rubocop', '~> 0.50'
|
36
37
|
spec.add_development_dependency 'coveralls', '~> 0.8.21'
|
37
|
-
spec.add_development_dependency 'pry', '~> 0.
|
38
|
+
spec.add_development_dependency 'pry', '~> 0.11'
|
38
39
|
spec.add_development_dependency 'wwtd', '~> 1.3'
|
39
40
|
end
|
@@ -1,18 +1,61 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'addressable/uri'
|
3
|
+
|
1
4
|
module Halo
|
2
|
-
class
|
5
|
+
class InvalidCacheStore < StandardError; end
|
3
6
|
|
4
|
-
|
7
|
+
class APIResponse
|
8
|
+
include HTTParty
|
5
9
|
|
6
10
|
def initialize(options = {})
|
7
|
-
|
8
|
-
@
|
11
|
+
client = options.delete(:client)
|
12
|
+
@api_key = client.api_key
|
13
|
+
@region = client.region
|
14
|
+
@cache_store = client.cache_store || {}
|
15
|
+
|
16
|
+
raise InvalidCacheStore if cached? && !redis_store.is_a?(Redis)
|
17
|
+
|
18
|
+
self.class.base_uri 'https://www.haloapi.com'
|
9
19
|
end
|
10
20
|
|
11
|
-
def get_data(path, options)
|
12
|
-
|
13
|
-
|
14
|
-
|
21
|
+
def get_data(path, options = {})
|
22
|
+
if cached?
|
23
|
+
result = redis_store.get("#{path}#{options}")
|
24
|
+
return JSON.parse(result) if result
|
25
|
+
end
|
26
|
+
response = perform_uncached_request(:get, path, options)
|
27
|
+
redis_store.setex "#{path}#{options}", ttl, response.to_json if cached?
|
28
|
+
response
|
15
29
|
end
|
16
30
|
|
31
|
+
def perform_uncached_request(verb, path, params = {})
|
32
|
+
options = {}
|
33
|
+
headers = {
|
34
|
+
'Ocp-Apim-Subscription-Key' => @api_key,
|
35
|
+
'Accept-Language' => @region
|
36
|
+
}
|
37
|
+
|
38
|
+
options[:headers] = headers unless headers.empty?
|
39
|
+
options[:query] = params unless params.empty?
|
40
|
+
|
41
|
+
response = self.class.send(verb, Addressable::URI.encode(path), options)
|
42
|
+
|
43
|
+
response.respond_to?(:parsed_response) ? response.parsed_response : response
|
44
|
+
end
|
45
|
+
|
46
|
+
# @return [Redis] returns the cache store
|
47
|
+
def redis_store
|
48
|
+
@cache_store[:redis]
|
49
|
+
end
|
50
|
+
|
51
|
+
# @return [Boolean] true if the request should be cached
|
52
|
+
def cached?
|
53
|
+
@cache_store[:cached]
|
54
|
+
end
|
55
|
+
|
56
|
+
# @return [Integer] the ttl to apply to cached keys
|
57
|
+
def ttl
|
58
|
+
@cache_store[:ttl]
|
59
|
+
end
|
17
60
|
end
|
18
61
|
end
|
data/lib/halo-api/client.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'halo-api/version'
|
2
2
|
require 'halo-api/api_response'
|
3
|
-
require '
|
4
|
-
|
3
|
+
require 'halo-api/configuration'
|
4
|
+
|
5
|
+
require 'redis'
|
5
6
|
|
6
7
|
|
7
8
|
module Halo
|
@@ -11,34 +12,35 @@ module Halo
|
|
11
12
|
attr_accessor(*Configuration::OPTIONS)
|
12
13
|
|
13
14
|
def initialize(options = {})
|
14
|
-
@api_key = options[:api_key]
|
15
|
-
@region = options[:region]
|
16
|
-
@endpoint = options[:endpoint]
|
17
|
-
|
18
15
|
options = Halo.options.merge(options)
|
19
16
|
|
20
17
|
Halo::Configuration::OPTIONS.each do |key|
|
21
18
|
send("#{key}=", options[key])
|
22
19
|
end
|
23
20
|
|
24
|
-
|
21
|
+
set_up_cache(options.delete(:redis), options.delete(:ttl))
|
25
22
|
end
|
26
23
|
|
27
|
-
def
|
28
|
-
|
24
|
+
def set_up_cache(redis_url, ttl)
|
25
|
+
return @cached = false unless redis_url
|
26
|
+
@ttl = ttl || Halo::Configuration::DEFAULT_TTL
|
27
|
+
@cached = true
|
28
|
+
@redis = Redis.new url: redis_url
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
# Returns an options hash with cache keys
|
32
|
+
# @return [Hash]
|
33
|
+
def cache_store
|
34
|
+
{
|
35
|
+
redis: @redis,
|
36
|
+
ttl: @ttl,
|
37
|
+
cached: @cached
|
36
38
|
}
|
39
|
+
end
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
self.class.send(verb, Addressable::URI.encode(path), options)
|
41
|
+
# @return [Boolean] true if the request should be cached
|
42
|
+
def cached?
|
43
|
+
cache_store[:cached]
|
42
44
|
end
|
43
45
|
|
44
46
|
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module Halo
|
2
2
|
module Configuration
|
3
3
|
|
4
|
-
OPTIONS = %i[api_key region].freeze
|
4
|
+
OPTIONS = %i[api_key region redis ttl].freeze
|
5
5
|
|
6
6
|
DEFAULT_API_KEY = nil
|
7
|
-
DEFAULT_REGION =
|
7
|
+
DEFAULT_REGION = 'en'
|
8
|
+
DEFAULT_REDIS = 'redis://localhost:6379'
|
9
|
+
DEFAULT_TTL = 1800
|
8
10
|
|
9
11
|
attr_accessor(*OPTIONS)
|
10
12
|
|
data/lib/halo-api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: halo-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carlos Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -44,6 +44,20 @@ dependencies:
|
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: 2.5.2
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: redis
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '4.0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '4.0'
|
47
61
|
- !ruby/object:Gem::Dependency
|
48
62
|
name: bundler
|
49
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,28 +78,28 @@ dependencies:
|
|
64
78
|
requirements:
|
65
79
|
- - "~>"
|
66
80
|
- !ruby/object:Gem::Version
|
67
|
-
version: '12.
|
81
|
+
version: '12.1'
|
68
82
|
type: :development
|
69
83
|
prerelease: false
|
70
84
|
version_requirements: !ruby/object:Gem::Requirement
|
71
85
|
requirements:
|
72
86
|
- - "~>"
|
73
87
|
- !ruby/object:Gem::Version
|
74
|
-
version: '12.
|
88
|
+
version: '12.1'
|
75
89
|
- !ruby/object:Gem::Dependency
|
76
90
|
name: rspec
|
77
91
|
requirement: !ruby/object:Gem::Requirement
|
78
92
|
requirements:
|
79
93
|
- - "~>"
|
80
94
|
- !ruby/object:Gem::Version
|
81
|
-
version: '3.
|
95
|
+
version: '3.6'
|
82
96
|
type: :development
|
83
97
|
prerelease: false
|
84
98
|
version_requirements: !ruby/object:Gem::Requirement
|
85
99
|
requirements:
|
86
100
|
- - "~>"
|
87
101
|
- !ruby/object:Gem::Version
|
88
|
-
version: '3.
|
102
|
+
version: '3.6'
|
89
103
|
- !ruby/object:Gem::Dependency
|
90
104
|
name: dotenv
|
91
105
|
requirement: !ruby/object:Gem::Requirement
|
@@ -112,14 +126,14 @@ dependencies:
|
|
112
126
|
requirements:
|
113
127
|
- - "~>"
|
114
128
|
- !ruby/object:Gem::Version
|
115
|
-
version: 0.
|
129
|
+
version: '0.50'
|
116
130
|
type: :development
|
117
131
|
prerelease: false
|
118
132
|
version_requirements: !ruby/object:Gem::Requirement
|
119
133
|
requirements:
|
120
134
|
- - "~>"
|
121
135
|
- !ruby/object:Gem::Version
|
122
|
-
version: 0.
|
136
|
+
version: '0.50'
|
123
137
|
- !ruby/object:Gem::Dependency
|
124
138
|
name: coveralls
|
125
139
|
requirement: !ruby/object:Gem::Requirement
|
@@ -140,14 +154,14 @@ dependencies:
|
|
140
154
|
requirements:
|
141
155
|
- - "~>"
|
142
156
|
- !ruby/object:Gem::Version
|
143
|
-
version: 0.
|
157
|
+
version: '0.11'
|
144
158
|
type: :development
|
145
159
|
prerelease: false
|
146
160
|
version_requirements: !ruby/object:Gem::Requirement
|
147
161
|
requirements:
|
148
162
|
- - "~>"
|
149
163
|
- !ruby/object:Gem::Version
|
150
|
-
version: 0.
|
164
|
+
version: '0.11'
|
151
165
|
- !ruby/object:Gem::Dependency
|
152
166
|
name: wwtd
|
153
167
|
requirement: !ruby/object:Gem::Requirement
|
@@ -171,8 +185,6 @@ extra_rdoc_files: []
|
|
171
185
|
files:
|
172
186
|
- ".gitignore"
|
173
187
|
- ".rspec"
|
174
|
-
- ".ruby-gemset"
|
175
|
-
- ".ruby-version"
|
176
188
|
- ".travis.yml"
|
177
189
|
- Gemfile
|
178
190
|
- LICENSE.txt
|
@@ -206,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
206
218
|
requirements:
|
207
219
|
- - ">="
|
208
220
|
- !ruby/object:Gem::Version
|
209
|
-
version: 2.
|
221
|
+
version: 2.2.2
|
210
222
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
211
223
|
requirements:
|
212
224
|
- - ">="
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
halo-api
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ruby-2.4.1
|