fragment_client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3a35f593d6abe9192c15b031170a641bec01aa8
4
+ data.tar.gz: 42a3637c7f04786e2e2e26c8e9bf84ecca9eb3f9
5
+ SHA512:
6
+ metadata.gz: 9a1e0322f94064a74b9e6ea7c01ccac85d72ad9377d0e7473a3b41b442d398d77f87357836f7c56e6bd7062af25e85f98abecbd621da1c9d57bf14edfaa44c2c
7
+ data.tar.gz: 7bbdb7cbb840d75c29fc8d946e69d8c0232167ba09449c33db22c293e48ead1f9dadbfde99b7afe79474b46f18ca9d50eaf20ddc0257bb1650a7b37130f709d9
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ #Fragment client
2
+ A Ruby gem that provides an interface to the fragment_server for easy use in web apps. The client's main responsibility is to construct requests and handle caching of response strings.
3
+
4
+ ##Usage
5
+ The fragment client provides an easy method to fetch snippets of markup from the fragment server. E.G.
6
+
7
+ ```ruby
8
+ client = FragmentClient.new
9
+ client.fetch('footer')
10
+ client.fetch('navigation', options)
11
+ ```
12
+
13
+ `options` is a hash of values used to construct the API query. The default values are listed below:
14
+
15
+ ```ruby
16
+ {
17
+ has_log_in: true, # true || false
18
+ logged_in: false, # true || false
19
+ logo: 'red' # 'red' || 'black'
20
+ }
21
+ ```
22
+
23
+ <sup>*currently, only the navigation method has options</sup>
24
+
25
+ ##Caching
26
+
27
+ Caching is achieved by The fragment client attempts to cache responses wherever possible.
@@ -0,0 +1,34 @@
1
+ require 'json'
2
+
3
+ class FragmentClient
4
+ attr_accessor :fragment_server_url, :option_params, :cache
5
+ DEFAULT_URL = 'http://test-eva-fragment-server.herokuapp.com/fragments/full.json?'
6
+
7
+ def initialize(fragment_server_url=DEFAULT_URL, cache=MemoryCache.new)
8
+ self.fragment_server_url = fragment_server_url
9
+ self.cache = cache
10
+ end
11
+
12
+ def fetch(section, options={})
13
+ set_params(options)
14
+ cache.fetch(section + option_params) { request[section] }
15
+ end
16
+
17
+ private
18
+
19
+ def set_params(options={})
20
+ self.option_params = URI.encode_www_form(default_options.merge(options).sort)
21
+ end
22
+
23
+ def default_options
24
+ { has_login: true, logged_in: false, logo: 'red'}
25
+ end
26
+
27
+ def request
28
+ JSON.parse(Net::HTTP.get(uri))
29
+ end
30
+
31
+ def uri
32
+ URI(fragment_server_url + option_params)
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ class MemoryCache
2
+ attr_accessor :cache
3
+
4
+ def initialize
5
+ self.cache = {}
6
+ end
7
+
8
+ def fetch(key, &block)
9
+ validate?(key) ? read(key) : write(key, block.call)['entry']
10
+ end
11
+
12
+ def read(key)
13
+ cache[key]['entry']
14
+ end
15
+
16
+ def write(key, value)
17
+ self.cache[key] = { 'created_at' => Time.now, 'entry' => value } unless value.blank?
18
+ end
19
+
20
+ def validate?(key)
21
+ cache[key] && (Time.now - cache[key]['created_at']) < 1.hours
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fragment_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Atkinson
8
+ - Joe Love
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails-timeago
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: A Ruby gem that provides an interface to the fragment_server for easy
43
+ use in web apps. This works for any ruby app.
44
+ email:
45
+ - anthony.atkinson@which.co.uk
46
+ - joe.love@which.co.uk
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - README.md
52
+ - lib/fragment_client/fragment_client.rb
53
+ - lib/fragment_client/memory_cache.rb
54
+ homepage: https://github.com/whichdigital/fragment_client
55
+ licenses: []
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.2.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: A Ruby gem that provides an interface to the fragment_server for easy use
77
+ in web apps.
78
+ test_files: []