warframe 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/warframe/cache.rb +37 -0
- data/lib/warframe/client_wrapper.rb +6 -4
- data/lib/warframe/rest/request.rb +7 -1
- data/lib/warframe/rest/utils.rb +5 -1
- data/lib/warframe/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7131e27d764e61e4219caef65a2b3485090e6f5c2076ea9bbb15a53e3c9ff0f2
|
4
|
+
data.tar.gz: 9b1627bfa5cefc5665e0a51a0fcc697c3ef57a9a2a4c633554e6f188ca5717cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad1132a20ca44e9991439d279e78582fff13779426be2edac6ef980b5acf5d1dceeede32f3b47122e388e831165a6c50d59473be16adb6ac96f549de694f7647
|
7
|
+
data.tar.gz: 7211fc972c45fdeef66b287a26e6a8ff6bd2eda0e8a0ff2579a8b9476f1e44ad09c573f0dbdb29a758e5414cc4199f5291279bbd50f0626b65da402cb0d92953
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Warframe
|
4
|
+
# @private
|
5
|
+
class Cache
|
6
|
+
# Expiration time in seconds
|
7
|
+
EXPIRATION_TIME = 5 * 60
|
8
|
+
attr_accessor :cache
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@cache = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_from_cache(key)
|
15
|
+
cache[key][:result]
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_in_cache(key)
|
19
|
+
cache[key][:result] if exist?(key) && !expired?(key)
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_to_cache(key, val, time = EXPIRATION_TIME)
|
23
|
+
cache[key] = { time: Time.now.to_i + time, result: val }
|
24
|
+
val
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def exist?(key)
|
30
|
+
!cache[key].nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
def expired?(key)
|
34
|
+
cache[key][:time] - Time.now.to_i <= 0
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,16 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'cache'
|
4
|
+
|
3
5
|
module Warframe
|
4
6
|
# @abstract
|
5
7
|
# @private
|
6
8
|
# Wraps the {Warframe::REST::Client REST::Client} with necessary variables and methods.
|
7
9
|
#
|
8
10
|
# This class should < not be used > for interacting with the API.
|
9
|
-
class ClientWrapper
|
11
|
+
class ClientWrapper < Warframe::Cache
|
10
12
|
# The base Warframe Stat API link
|
11
13
|
BASE_URL = 'https://api.warframestat.us/'
|
12
14
|
# Default attributes
|
13
15
|
DEFAULT_OPTIONS = { platform: 'pc', language: 'en' }.freeze
|
16
|
+
|
17
|
+
# The Client Cache
|
14
18
|
attr_accessor :platform, :language
|
15
19
|
|
16
20
|
# Initialize the Wrapper for {Warframe::REST::Client REST::Client}
|
@@ -22,8 +26,8 @@ module Warframe
|
|
22
26
|
# This class is ABSTRACT and should not be instantiated outside of {Warframe::REST::Client REST::Client}.
|
23
27
|
# @return [Warframe::ClientWrapper]
|
24
28
|
def initialize(options = {})
|
29
|
+
super()
|
25
30
|
DEFAULT_OPTIONS.merge(options).each { |k, v| instance_variable_set "@#{k}", v }
|
26
|
-
|
27
31
|
yield self if block_given?
|
28
32
|
end
|
29
33
|
|
@@ -31,7 +35,5 @@ module Warframe
|
|
31
35
|
def base_url
|
32
36
|
BASE_URL + platform
|
33
37
|
end
|
34
|
-
|
35
|
-
# might need to manipulate user_agent
|
36
38
|
end
|
37
39
|
end
|
@@ -21,6 +21,7 @@ module Warframe
|
|
21
21
|
# @return [Warframe:REST:Request]
|
22
22
|
def initialize(client, path, klass)
|
23
23
|
@client = client
|
24
|
+
@route = path
|
24
25
|
@path = client.base_url + path + "?language=#{@client.language}"
|
25
26
|
@klass = klass
|
26
27
|
end
|
@@ -31,7 +32,8 @@ module Warframe
|
|
31
32
|
def send
|
32
33
|
uri = URI(path)
|
33
34
|
req = Net::HTTP::Get.new(uri)
|
34
|
-
|
35
|
+
resp = get_response uri, req
|
36
|
+
return_parsed resp
|
35
37
|
end
|
36
38
|
|
37
39
|
private
|
@@ -41,6 +43,10 @@ module Warframe
|
|
41
43
|
# @return [Warframe::Models, Array<[Warframe::Models]>]
|
42
44
|
def return_parsed(resp)
|
43
45
|
parsed = JSON.parse(resp)
|
46
|
+
|
47
|
+
# Return Empty array if no data found.
|
48
|
+
return [] if parsed.is_a?(Array) && parsed.empty?
|
49
|
+
|
44
50
|
@klass.new parsed
|
45
51
|
end
|
46
52
|
|
data/lib/warframe/rest/utils.rb
CHANGED
@@ -12,7 +12,11 @@ module Warframe
|
|
12
12
|
# @param path [String]
|
13
13
|
# @param klass [Warframe::Models]
|
14
14
|
def get(path, klass)
|
15
|
-
|
15
|
+
inst = @client || self
|
16
|
+
return inst.get_from_cache(path) if inst.find_in_cache(path)
|
17
|
+
|
18
|
+
result = Warframe::REST::Request.new(inst, path, klass).send
|
19
|
+
inst.add_to_cache(path, result)
|
16
20
|
end
|
17
21
|
end
|
18
22
|
end
|
data/lib/warframe/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: warframe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- A.J. Romaniello
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fast_underscore
|
@@ -176,6 +176,7 @@ files:
|
|
176
176
|
- LICENSE.txt
|
177
177
|
- README.md
|
178
178
|
- lib/warframe.rb
|
179
|
+
- lib/warframe/cache.rb
|
179
180
|
- lib/warframe/client_wrapper.rb
|
180
181
|
- lib/warframe/models/alert.rb
|
181
182
|
- lib/warframe/models/attributes/active.rb
|