json_api_resource_connections 0.3.0 → 1.0.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: 5464f3db319f337b50a49b11d643c6b667b25f24
4
- data.tar.gz: 849c3f3dc0eebc242e7fa0b24a29bd5ca1b0a1db
3
+ metadata.gz: e7b6b129c190c2f8b7c19ca6d5a7fa8aac51fd2b
4
+ data.tar.gz: 140f813cb5c8e8ce1f6f9366e009b2431a766b55
5
5
  SHA512:
6
- metadata.gz: 58861256f912031c5364396f1d7d1d2dbab728c77a095809176683f597706cd1ac4b3a428e3dd02e9a290b50848f8dfc47379cf97d50bf0c7e3a04ac5fd3aa83
7
- data.tar.gz: bbc346515f3454d659f8c3edb5e144e73629b2a9e98f614566bc54a4a1c25ca037423698d50d4333252ab315366bbaf869c1e40aa646309013ff0d1ae0da2258
6
+ metadata.gz: c38fdb33bac91aba9d5f0ab10d091ee51dcedfdcfcbd375baaea7e0414cdb7963ddf52f04321b631aaff1cb864da40d7799533c63872e9880c669a1633087322
7
+ data.tar.gz: 63d512bde9bd0599b00c7ef3aa47a089fe7b0c164bd3fb9ecc0e6a193c1b39b17df90a83aa527da08f9f1e084b8a44db25bc6f79f69498dec96f3d45fb8b149c
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ gemspec
5
5
 
6
6
  gem 'rake'
7
7
 
8
- gem 'json_api_resource', path: "../json_api_resource"
8
+ gem 'json_api_resource'
9
9
 
10
10
  group :test do
11
11
  gem 'simplecov'
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # JsonApiResourceConnections
2
2
 
3
- Complex connection behaviour to sit on top of [JsonApiResource](http://github.com/avvo/json_api_resource) v2.0. This makes circuitbreaker connections default and enables cache flallbacks to when the server replies with anything other than a 404
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Complex connection behaviour to sit on top of [JsonApiResource](http://github.com/avvo/json_api_resource) v2.0. This makes circuitbreaker connections default and enables cache fallbacks to when the server replies with anything other than a 404
6
4
 
7
5
  ## Installation
8
6
 
@@ -24,7 +22,25 @@ And it should auto magically inject itself into `JsonApiResource::Resource`
24
22
 
25
23
  ## Usage
26
24
 
27
- ### CacheProcessor
25
+ Once the gem is included, it automatically injects its code into all `JsonApiResource`s, giving them default circuitbreaker connections. There are 3 major components and a couple handy helpers.
26
+
27
+ ### Helpers
28
+
29
+ #### cache_fallback( *actions )
30
+
31
+ Enables retreival of data from cache if the server connection fails. Passing no actions will enable cache fallbacks for everything.
32
+
33
+ Requires caching to be enabled as described in [setup](#setup).
34
+
35
+ #### try_cache_first( *actions )
36
+
37
+ Will force the resource to try to fetch the data requested from the cache first, before it tries to hit the server. Passing no actions will force trying caching for everything.
38
+
39
+ Requires caching to be enabled as described in [setup](#setup).
40
+
41
+ ### Components
42
+
43
+ #### CacheProcessor
28
44
 
29
45
  Cache Processor is the component that handles caching. `CompressedCacheProcessor` caches results in two pieces: the actual object and the ids for the action. So your `Snack.search(q: "cheezbergher")` call will cache as
30
46
  ``` ruby
@@ -32,14 +48,28 @@ Cache Processor is the component that handles caching. `CompressedCacheProcessor
32
48
 
33
49
  # and
34
50
 
35
- "snack/search/1" => {id: 1, ... }
36
- "snack/search/2" => {id: 2, ... }
51
+ "snack/search/id:1" => {id: 1, ... }
52
+ "snack/search/id:2" => {id: 2, ... }
37
53
  ...
38
54
  ```
39
55
 
40
56
  When no id is present in the response, the full response will be cached.
41
57
 
42
- #### Setup
58
+ #### CachedCircuitbreakerServerConnection
59
+
60
+ New default connection. Calls the server, but if the request fails, it will drop all subsequent calls for 30 seconds. Failure is defined as any non-404 error; a 500 server error or any exception in the code will trip the circuitbreaker.
61
+
62
+ If the request succeeds, it will try to cache the result using its cache_processor. If none is set up, no caching will occur.
63
+
64
+ See [setup](#setup) for enabling caching.
65
+
66
+ #### CacheConnection
67
+
68
+ Given an action, will try to fetch results from the cache via cache_processor. Is used as both fallback and cache first connection.
69
+
70
+ Requires caching to be enabled as described in [setup](#setup).
71
+
72
+ ## Setup
43
73
 
44
74
  In your `config/json_api_resource.rb` you will need to set up the cache layer for the `CacheProcessor`
45
75
 
@@ -59,19 +89,15 @@ module JsonApiResource
59
89
  end
60
90
  ```
61
91
 
62
- ### Connections
63
-
64
- The default connection is the `CachedCircuitbreakerServerConnection`. It will prevent any more calls to the server if any non-404 error is returned for 30 seconds. If you assign cache_processor, the cache part will kick in and cache the results returnrd from the server.
65
-
66
92
  ## Development
67
93
 
68
94
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
95
 
70
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
96
+ To install this gem onto your local machine, run `bundle exec rake install`.
71
97
 
72
98
  ## Contributing
73
99
 
74
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_api_resource_connections.
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/avvo]/json_api_resource_connections.
75
101
 
76
102
 
77
103
  ## License
@@ -19,7 +19,7 @@ module JsonApiResource
19
19
  write_objects(client, action, result_set)
20
20
 
21
21
  else
22
- cache.write key, result_set
22
+ write_blob(key, result_set)
23
23
  end
24
24
 
25
25
  result
@@ -37,11 +37,12 @@ module JsonApiResource
37
37
  if item.is_a? Integer
38
38
  # grab the actual object from cache
39
39
  key = item_cache_key(client, action, item)
40
- cache.fetch key
40
+ attrs = cache.fetch key
41
+ client.new attrs
41
42
  # if they are not ids
42
43
  else
43
44
  # they have to be the full objects. return them
44
- item
45
+ client.new item
45
46
  end
46
47
  end
47
48
  JsonApiClient::ResultSet.new(Array(set))
@@ -55,7 +56,7 @@ module JsonApiResource
55
56
  class_string = client.is_a?(Class) ? client.to_s : client.class.to_s
56
57
  class_string = class_string.underscore
57
58
  formatted_args = args.present? ? ordered_args(*args) : nil
58
- "#{class_string}/#{action}/#{formatted_args}"
59
+ "connection::#{JsonApiResourceConnections::VERSION}/#{class_string}/#{action}/#{formatted_args}"
59
60
  end
60
61
 
61
62
  def item_cache_key(client, action, id)
@@ -79,9 +80,13 @@ module JsonApiResource
79
80
  def write_objects(client, action, result_set)
80
81
  result_set.each do |item|
81
82
  key = item_cache_key client, action, item["id"]
82
- cache.write key, item
83
+ cache.write key, item.attributes
83
84
  end
84
85
  end
86
+
87
+ def write_blob(key, result_set)
88
+ cache.write key, result_set.map(&:attributes)
89
+ end
85
90
  end
86
91
  end
87
92
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiResourceConnections
2
- VERSION = "0.3.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_resource_connections
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-21 00:00:00.000000000 Z
11
+ date: 2016-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport