buckybox-api 1.3.1 → 1.4.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
  SHA1:
3
- metadata.gz: bda861844c9c576cfc55001b0b30854d8dbca2a3
4
- data.tar.gz: 84196ac27e6a4e9b3bf5f019e2b4dcb21b25f53a
3
+ metadata.gz: 330e89a7ff831ac42be4151bd4d7cf8b23e9a53b
4
+ data.tar.gz: 210061a10df2a9afec3443f4ff5928def4f360b0
5
5
  SHA512:
6
- metadata.gz: f5b20f2d07326ccf416926119a8db7f4b967ac0daf3e01fe1e816fe882d641f98756eb8392a75e336d33905d0e8ef0ea10ac422e3dfca5c959c33c3bcd5ed016
7
- data.tar.gz: 8aade241308b8f455e6e60431fcd32497f2f6b707c342c61d8dfd493c5171ef53166ee6e860430111ed5a1f90a86b787b5131391c28a2cefa22b1ab48bd301e4
6
+ metadata.gz: d1168bf5441e6c7a38f4de1b3701da726fb3dffe7b344eab4d8d29fe6b01fa02b8049e274fc11fcd749d8b81613bc146b8c66ca04fed58f8f62e1da6fd76e840
7
+ data.tar.gz: c8106a1dec1774e6914ec7f03fcb42c681ae36e10209027b22c9f93b89842f49e6bf7e714181430a2f18352256a8c4505461c22c70443cecb6fac612e4a4ec8a
data/buckybox-api.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "buckybox-api"
7
- spec.version = "1.3.1"
7
+ spec.version = "1.4.0"
8
8
  spec.authors = ["Cédric Félizard"]
9
9
  spec.email = ["cedric@felizard.fr"]
10
10
  spec.summary = %q{RubyGem wrapper for the Bucky Box API}
data/lib/buckybox/api.rb CHANGED
@@ -8,6 +8,22 @@ module BuckyBox
8
8
  ResponseError = Class.new(Exception) # generic error
9
9
  NotFoundError = Class.new(Exception)
10
10
 
11
+ class CachedResponse
12
+ attr_reader :response, :cached_at
13
+
14
+ def initialize(response)
15
+ @response, @cached_at = response, epoch
16
+ end
17
+
18
+ def expired?
19
+ epoch - cached_at > 60 # NOTE: cache responses for 60 seconds
20
+ end
21
+
22
+ private def epoch
23
+ Time.now.utc.to_i
24
+ end
25
+ end
26
+
11
27
  include HTTParty
12
28
  format :json
13
29
  base_uri "https://api.buckybox.com/v1"
@@ -107,10 +123,17 @@ module BuckyBox
107
123
  }
108
124
 
109
125
  if type == :get # NOTE: only cache GET method
110
- cache_key = [self.class.headers.hash, uri, to_query(params)].join
111
-
112
126
  @cache ||= {}
113
- @cache[cache_key] ||= query_fresh.call
127
+ cache_key = [self.class.headers.hash, uri, to_query(params)].join
128
+ cached_response = @cache[cache_key]
129
+
130
+ if cached_response && !cached_response.expired?
131
+ cached_response.response
132
+ else
133
+ response = query_fresh.call
134
+ @cache[cache_key] = CachedResponse.new(response)
135
+ response
136
+ end
114
137
  else
115
138
  query_fresh.call
116
139
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buckybox-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric Félizard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-06 00:00:00.000000000 Z
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty