json_api_resource_connections 0.2.3

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: b177657fab9e94a6d99342a765b5a0dd66851e40
4
+ data.tar.gz: c4197d9d7d4adc9394fb5bb8da5da22c9ae13448
5
+ SHA512:
6
+ metadata.gz: a133ecb64dfd9e76246473f4f51e5dee3890c4c931d7d15555cc52eeb1cf624c8dd5d07d8f80cef2a3869750297bc5d9ffc3a110006733790b190df89b0efb1e
7
+ data.tar.gz: bbfab1ee3dae9871c7e647b9255b78b943c4d02841ae44939a1bf10a96645d7b322a0e37dca4ba543a28c38936d45580b2cf87bb960998d11037cf84cd1eadaf
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_api_resource_connections.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
7
+
8
+ gem 'json_api_resource', path: "../json_api_resource"
9
+
10
+ group :test do
11
+ gem 'simplecov'
12
+ gem 'minitest-stub_any_instance'
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Greg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # JsonApiResourceConnections
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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'json_api_resource_connections'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install json_api_resource_connections
22
+
23
+ And it should auto magically inject itself into `JsonApiResource::Resource`
24
+
25
+ ## Usage
26
+
27
+ ### CacheProcessor
28
+
29
+ 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
+ ``` ruby
31
+ 'snack/search/q=>"cheezbergher"' => `[1, 2, 3, 4]`
32
+
33
+ # and
34
+
35
+ "snack/search/1" => {id: 1, ... }
36
+ "snack/search/2" => {id: 2, ... }
37
+ ...
38
+ ```
39
+
40
+ When no id is present in the response, the full response will be cached.
41
+
42
+ #### Setup
43
+
44
+ In your `config/json_api_resource.rb` you will need to set up the cache layer for the `CacheProcessor`
45
+
46
+ ```ruby
47
+ module JsonApiResource
48
+
49
+ # set up the cache lawyer for the cache processor
50
+ module CacheProcessor
51
+ CompressedCacheProcessor.cache = Rails.cache # or whatever
52
+ end
53
+
54
+ # set up the processor for the connections
55
+ module Connections
56
+ CachedCircuitbreakerServerConnection.cache_processor = JsonApiResource::CacheProcessor::CompressedCacheProcessor
57
+ CacheConnection.cache_processor = JsonApiResource::CacheConnection::CompressedCacheProcessor
58
+ end
59
+ end
60
+ ```
61
+
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
+ ## Development
67
+
68
+ 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
+
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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_api_resource_connections.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ Bundler::GemHelper.install_tasks
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_api_resource_connections"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_api_resource_connections/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_api_resource_connections"
8
+ spec.version = JsonApiResourceConnections::VERSION
9
+ spec.authors = ["Greg"]
10
+ spec.email = ["greg@avvo.com"]
11
+
12
+ spec.summary = "circuit breaker and cache fallback connections for json api resource v2"
13
+ spec.description = "circuit breaker and cache fallback connections for json api resource v2"
14
+ spec.homepage = "https://github.com/avvo/json_api_resource_connections"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activesupport"
23
+ spec.add_dependency "json_api_resource"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
@@ -0,0 +1,16 @@
1
+ module JsonApiResource
2
+ module CacheProcessor
3
+ class Base
4
+
5
+ class << self
6
+ def write( result, client, action, *args )
7
+ result
8
+ end
9
+
10
+ def fetch( client, action, *args )
11
+ []
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,88 @@
1
+ module JsonApiResource
2
+ module CacheProcessor
3
+ class CompressedCacheProcessor < Base
4
+
5
+ class_attribute :cache
6
+
7
+ class << self
8
+
9
+ def write(result, client, action, *args)
10
+ result_set = Array(result)
11
+
12
+ key = cache_key(client, action, *args)
13
+
14
+ # result set has ids and we can break the set down into an array of ids and the objects
15
+ if splitable?(result_set)
16
+
17
+ write_ids(key, result_set)
18
+
19
+ write_objects(client, action, result_set)
20
+
21
+ else
22
+ cache.write key, result_set
23
+ end
24
+
25
+ result
26
+ end
27
+
28
+ def fetch(client, action, *args)
29
+ key = cache_key(client, action, *args)
30
+ set = cache.fetch key
31
+
32
+ raise KeyError.new("#{key} not found") if set.blank?
33
+
34
+ # set can be an array of blobs or an array of ids
35
+ set.map! do |item|
36
+ # if the results are ids
37
+ if item.is_a? Integer
38
+ # grab the actual object from cache
39
+ key = item_cache_key(client, action, item)
40
+ cache.fetch key
41
+ # if they are not ids
42
+ else
43
+ # they have to be the full objects. return them
44
+ item
45
+ end
46
+ end
47
+ JsonApiClient::ResultSet.new(Array(set))
48
+ end
49
+
50
+ private
51
+
52
+ def cache_key(client, action, *args)
53
+ # this can come in as a class or as an instance
54
+ # class | instance
55
+ class_string = client.is_a?(Class) ? client.to_s : client.class.to_s
56
+ class_string = class_string.underscore
57
+ formatted_args = args.present? ? ordered_args(*args) : nil
58
+ "#{class_string}/#{action}/#{formatted_args}"
59
+ end
60
+
61
+ def item_cache_key(client, action, id)
62
+ "#{cache_key(client, action)}id:#{id}"
63
+ end
64
+
65
+ def ordered_args(*args)
66
+ args.map do |arg|
67
+ arg.is_a?(Hash) ? arg.sort.to_h : arg
68
+ end
69
+ end
70
+
71
+ def splitable?(result_set)
72
+ result_set.select{|_| _["id"]}.present?
73
+ end
74
+
75
+ def write_ids(key, result_set)
76
+ cache.write key, result_set.map{|_| _["id"]}
77
+ end
78
+
79
+ def write_objects(client, action, result_set)
80
+ result_set.each do |item|
81
+ key = item_cache_key client, action, item["id"]
82
+ cache.write key, item
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,6 @@
1
+ module JsonApiResource
2
+ module CacheProcessor
3
+ autoload :Base, 'json_api_resource/cache_processor/base'
4
+ autoload :CompressedCacheProcessor, 'json_api_resource/cache_processor/compressed_cache_processor'
5
+ end
6
+ end
@@ -0,0 +1,23 @@
1
+ module JsonApiResource
2
+ module Connections
3
+ class CacheConnection < Multiconnect::Connection::Base
4
+
5
+ class_attribute :cache_processor
6
+ class_attribute :error_notifier
7
+
8
+ self.cache_processor = ::JsonApiResource::CacheProcessor::Base
9
+
10
+ class << self
11
+ attr_accessor :cache
12
+ end
13
+
14
+ def report_error( e )
15
+ error_notifier.notify( self, e ) if error_notifier.present?
16
+ end
17
+
18
+ def request( action, *args )
19
+ cache_processor.fetch client, action, *args
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,83 @@
1
+ module JsonApiResource
2
+ module Connections
3
+ class CachedCircuitbreakerServerConnection < Multiconnect::Connection::Base
4
+
5
+ class_attribute :cache_processor
6
+ class_attribute :error_notifier
7
+
8
+ self.cache_processor = ::JsonApiResource::CacheProcessor::Base
9
+
10
+ def initialize(options)
11
+ super options
12
+ @caching = options.fetch :caching, true
13
+ @timeout = Time.now
14
+ end
15
+
16
+ def report_error( e )
17
+ unless e.is_a? ServerNotReadyError
18
+ error_notifier.notify( self, e ) if error_notifier.present?
19
+ end
20
+ end
21
+
22
+ def request( action, *args )
23
+ if ready_for_request?
24
+
25
+ client_args = args.deep_dup
26
+ result = client_request(action, *client_args)
27
+
28
+ cache_processor.write(result, client, action, *args) if cache?
29
+
30
+ result
31
+
32
+ else
33
+ raise ServerNotReadyError
34
+ end
35
+
36
+ rescue JsonApiClient::Errors::NotFound => e
37
+ empty_set_with_errors e
38
+ rescue => e
39
+ @timeout = timeout
40
+
41
+ # propagate the error up to be handled by Connection::Base
42
+ raise e
43
+ end
44
+
45
+ def empty_set_with_errors( e )
46
+ result = JsonApiClient::ResultSet.new
47
+
48
+ result.meta = {status: 404}
49
+
50
+ result.errors = ActiveModel::Errors.new(result)
51
+ result.errors.add("RecordNotFound", e.message)
52
+
53
+ result
54
+ end
55
+
56
+ private
57
+
58
+ def timeout
59
+ # default circuit broken for 30 seconds.
60
+ # This should probably be 1 - 2 - 5 - 15 - 30 - 1 min *
61
+ Time.now + 30.seconds
62
+ end
63
+
64
+ def ready_for_request?
65
+ Time.now > @timeout
66
+ end
67
+
68
+ def cache?
69
+ @caching
70
+ end
71
+
72
+ def client_request(action, *args)
73
+ result = self.client.send action, *args
74
+
75
+ if result.is_a? JsonApiClient::Scope
76
+ result = result.all
77
+ end
78
+
79
+ result
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ module JsonApiResource
2
+ module Connections
3
+ class ServerNotReadyError < StandardError
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module JsonApiResourceConnections
2
+ VERSION = "0.2.3"
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support'
2
+ require 'active_support/callbacks'
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/module'
5
+ require 'active_support/core_ext/class/attribute'
6
+ require 'active_support/core_ext/object/deep_dup'
7
+ require 'json_api_resource'
8
+ require "json_api_resource_connections/version"
9
+
10
+ require 'multiconnect'
11
+
12
+ require 'json_api_resource/cache_processor'
13
+
14
+ require 'json_api_resource/connections/cache_connection'
15
+ require 'json_api_resource/connections/cached_circuitbreaker_server_connection'
16
+ require 'json_api_resource/connections/server_not_ready_error'
17
+
18
+
19
+ module JsonApiResourceConnections
20
+
21
+ extend ActiveSupport::Concern
22
+
23
+ included do
24
+
25
+ include Multiconnect::Connectable
26
+
27
+ class << self
28
+ class_attribute :_fallbacks
29
+ self._fallbacks = []
30
+
31
+ def cache_fallback(*actions)
32
+ self._fallbacks = _fallbacks + Array(actions)
33
+ add_connection JsonApiResource::Connections::CacheConnection, client: self.client_class, only: self._fallbacks
34
+ end
35
+
36
+ def wraps(client)
37
+ self.client_class = client
38
+
39
+ # now that we know where to connect to, let's do it
40
+ add_connection JsonApiResource::Connections::CachedCircuitbreakerServerConnection, client: self.client_class
41
+ end
42
+ end
43
+
44
+ def connection
45
+ @connection ||= JsonApiResource::Connections::CachedCircuitbreakerServerConnection.new( client: self.client, caching: false )
46
+ end
47
+ end
48
+ end
49
+
50
+ JsonApiResource::Resource.send :include, JsonApiResourceConnections
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_api_resource_connections
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Greg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json_api_resource
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: circuit breaker and cache fallback connections for json api resource
84
+ v2
85
+ email:
86
+ - greg@avvo.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - json_api_resource_connections.gemspec
100
+ - lib/json_api_resource/cache_processor.rb
101
+ - lib/json_api_resource/cache_processor/base.rb
102
+ - lib/json_api_resource/cache_processor/compressed_cache_processor.rb
103
+ - lib/json_api_resource/connections/cache_connection.rb
104
+ - lib/json_api_resource/connections/cached_circuitbreaker_server_connection.rb
105
+ - lib/json_api_resource/connections/server_not_ready_error.rb
106
+ - lib/json_api_resource_connections.rb
107
+ - lib/json_api_resource_connections/version.rb
108
+ homepage: https://github.com/avvo/json_api_resource_connections
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.6
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: circuit breaker and cache fallback connections for json api resource v2
132
+ test_files: []