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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 064d0649ace9ff7b79f1beaa548ac113c1e8b4b1067d102c628f18b20d7b868a
4
- data.tar.gz: 1f003893b70c4649472ca7643fb596bd112492641b30d96bfc22cc4b86db48cc
3
+ metadata.gz: 7131e27d764e61e4219caef65a2b3485090e6f5c2076ea9bbb15a53e3c9ff0f2
4
+ data.tar.gz: 9b1627bfa5cefc5665e0a51a0fcc697c3ef57a9a2a4c633554e6f188ca5717cd
5
5
  SHA512:
6
- metadata.gz: 24e0ff2c60f49fbd6e6234c517a75c2c005253f1c460e7fef99b0e488680d265f641009afe52cd5503551df99e1f0cacc4009d72e79aafb0726a7120f5d73b79
7
- data.tar.gz: 20168043a6f49861b071e03305b416e87d1752d305827532ff8080477116bf8b8ec5400bc463a515fc40681a2a836151b199d4fe3e8d3c789c45eeee4a537563
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
- return_parsed get_response uri, req
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
 
@@ -12,7 +12,11 @@ module Warframe
12
12
  # @param path [String]
13
13
  # @param klass [Warframe::Models]
14
14
  def get(path, klass)
15
- Warframe::REST::Request.new(@client || self, path, klass).send
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Warframe
4
4
  # The current version of this gem.
5
- VERSION = '0.2.1'
5
+ VERSION = '0.3.0'
6
6
  end
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.2.1
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-29 00:00:00.000000000 Z
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