fragment_client 1.1.0 → 1.1.1

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: 47ec5a52e5b81e40693ac35fd5e34c1dbd1975ee
4
- data.tar.gz: 60dfeb981985a908b134f452ae2d29d38f9d6518
3
+ metadata.gz: f1c1d458d625afad36e7931d2bda171aa6064d3d
4
+ data.tar.gz: 81c9be211e144ddb2a07d520de834a9bac7964d1
5
5
  SHA512:
6
- metadata.gz: 02b2c42311ead392b53abbefc0d3528a6da8bdb1cb53c167683a8bd537b9cc55c6bba9a24e0bde80b724c0c31e74db60376edb42605350958ef5c77bd162d416
7
- data.tar.gz: 3ecb2fad3cdced22a5060d1cd9baa7d8cab21f005f3f0b3fd5d38ca8b8863a421bcbf6a633ff47eecd586d55808cac71e985cc523048d8b67a0e60a40ee2d837
6
+ metadata.gz: 64835b4471eeaf7c32871b8ee4ab40801114f7215fd2c707cc18fe54754c3a2e2c0e16da0f6b68627f884c5dab01264ee99df7d97f8cd4f14d2c6aa6f17a596c
7
+ data.tar.gz: 8cdb8051c830a46a872c19fef5ade1d72b22d00acfe1b63ec458cf2f5ee49c3c65331b9c5de253f4da0d09ccab90c52cc6228e5db3e29e977dbe597651e7d799
@@ -1,21 +1,36 @@
1
1
  require 'json'
2
2
  require 'memory_cache'
3
+ require 'net/http'
3
4
 
4
5
  class FragmentClient
5
- attr_accessor :fragment_server_url, :option_params, :cache
6
+ attr_accessor :fragment_server_url, :option_params, :cache, :logger
6
7
  DEFAULT_URL = 'http://test-eva-fragment-server.herokuapp.com/fragments/full.json?'
7
8
 
8
- def initialize(fragment_server_url=DEFAULT_URL, cache=MemoryCache.new)
9
+ def initialize(fragment_server_url=DEFAULT_URL, cache=MemoryCache.new, logger=default_logger)
9
10
  self.fragment_server_url = fragment_server_url
10
11
  self.cache = cache
12
+ self.logger = logger
11
13
  end
12
14
 
13
15
  def fetch(section, options={})
14
16
  set_params(options)
15
- cache.fetch(section + option_params, expires_in: ENV["FRAGMENT_CLIENT_CACHE_TIME"] || 1.hour) { request[section] }
17
+ log(section)
18
+
19
+ cache.fetch(section.to_s + option_params, expires_in: ENV["FRAGMENT_CLIENT_CACHE_TIME"].to_i || 1.hour) do
20
+ request[section]
21
+ end
16
22
  end
17
23
 
18
24
  private
25
+ def default_logger
26
+ Rails.try(:logger) || Logger.new(STDOUT)
27
+ end
28
+
29
+ def log(section)
30
+ cache_hit = cache.read(section).present? ? 'CacheHit' : 'CacheMiss'
31
+ logger.info "FragmentClient:{Cache:#{cache.class}, Section:#{section}, #{cache_hit}}"
32
+ logger.info "FragmentClient:{requesting:#{uri}}" if cache_hit == 'CacheMiss'
33
+ end
19
34
 
20
35
  def set_params(options={})
21
36
  self.option_params = URI.encode_www_form(default_options.merge(options).sort)
data/lib/memory_cache.rb CHANGED
@@ -10,6 +10,7 @@ class MemoryCache
10
10
  end
11
11
 
12
12
  def read(key)
13
+ return nil unless cache[key]
13
14
  cache[key]['entry']
14
15
  end
15
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fragment_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Atkinson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-11 00:00:00.000000000 Z
12
+ date: 2015-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails-timeago