json_api_resource_connecitons 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3f3f54916a89e9f3f079b32b656bd8a72a9de26
4
+ data.tar.gz: edc2bf3aa050e7a3324b81ba5e6697d6e16e68f1
5
+ SHA512:
6
+ metadata.gz: 87bdc3c17a3811926b17c5a427b6b3096aa163622e6335d18144009cf0bbb6ba070533c1b08357f036afa4acd7420344448aec8cc11b2275e1a55357f3bb6afc
7
+ data.tar.gz: f4ab1b7b605044ba6bca452e4beb9bf0b9a3a3b4e60db36446f2c1658bfd9820b4abfbd47b2cf6f24d8d7ac9fca3293a594b70e4679b24b32a0a3773d5e1dfcb
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -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_connecitons.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
@@ -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.
@@ -0,0 +1,80 @@
1
+ # JsonApiResourceConnecitons
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_connecitons'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install json_api_resource_connecitons
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_connecitons.
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
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_api_resource_connecitons"
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
@@ -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_connecitons/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_api_resource_connecitons"
8
+ spec.version = JsonApiResourceConnecitons::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,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,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 read( client, action, *args )
11
+ []
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,85 @@
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 read(client, action, *args)
29
+ key = cache_key(client, action, *args)
30
+ set = cache.read key
31
+
32
+ # set can be an array of blobs or an array of ids
33
+ set.map do |item|
34
+ # if the results are ids
35
+ if item.is_a? Integer
36
+ # grab the actual object from cache
37
+ key = item_cache_key(client, action, item)
38
+ cache.read key
39
+ # if they are not ids
40
+ else
41
+ # they have to be the full objects. return them
42
+ item
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def cache_key(client, action, *args)
50
+ # this can come in as a class or as an instance
51
+ # class | instance
52
+ class_string = client.is_a?(Class) ? client.to_s : client.class.to_s
53
+ class_string = class_string.underscore
54
+ args = args.present? ? ordered_args(*args) : nil
55
+ "#{class_string}/#{action}/#{args}"
56
+ end
57
+
58
+ def item_cache_key(client, action, id)
59
+ "#{cache_key(client, action)}id:#{id}"
60
+ end
61
+
62
+ def ordered_args(*args)
63
+ args.map do |arg|
64
+ arg.is_a?(Hash) ? arg.sort.to_h : arg
65
+ end.sort
66
+ end
67
+
68
+ def splitable?(result_set)
69
+ result_set.select{|_| _["id"]}.present?
70
+ end
71
+
72
+ def write_ids(key, result_set)
73
+ cache.write key, result_set.map{|_| _["id"]}
74
+ end
75
+
76
+ def write_objects(client, action, result_set)
77
+ result_set.each do |item|
78
+ key = item_cache_key client, action, item["id"]
79
+ cache.write key, item
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,7 @@
1
+ module JsonApiResource
2
+ module Connections
3
+ autoload :CacheConnection, 'json_api_resource/connections/cache_connection'
4
+ autoload :CachedCircuitbreakerServerConnection, 'json_api_resource/connections/cached_circuitbreaker_server_connection'
5
+ autoload :ServerNotReadyError, 'json_api_resource/connections/server_not_ready_error'
6
+ end
7
+ 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.read client, action, *args
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,82 @@
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
+ result = client_request(action, *args)
26
+
27
+ cache_processor.write(result, client, action, *args) if cache?
28
+
29
+ result
30
+
31
+ else
32
+ raise ServerNotReadyError
33
+ end
34
+
35
+ rescue JsonApiClient::Errors::NotFound => e
36
+ empty_set_with_errors e
37
+ rescue => e
38
+ @timeout = timeout
39
+
40
+ # propagate the error up to be handled by Connection::Base
41
+ raise e
42
+ end
43
+
44
+ def empty_set_with_errors( e )
45
+ result = JsonApiClient::ResultSet.new
46
+
47
+ result.meta = {status: 404}
48
+
49
+ result.errors = ActiveModel::Errors.new(result)
50
+ result.errors.add("RecordNotFound", e.message)
51
+
52
+ result
53
+ end
54
+
55
+ private
56
+
57
+ def timeout
58
+ # default circuit broken for 30 seconds.
59
+ # This should probably be 1 - 2 - 5 - 15 - 30 - 1 min *
60
+ Time.now + 30.seconds
61
+ end
62
+
63
+ def ready_for_request?
64
+ Time.now > @timeout
65
+ end
66
+
67
+ def cache?
68
+ @caching
69
+ end
70
+
71
+ def client_request(action, *args)
72
+ result = self.client.send action, *args
73
+
74
+ if result.is_a? JsonApiClient::Scope
75
+ result = result.all
76
+ end
77
+
78
+ result
79
+ end
80
+ end
81
+ end
82
+ 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,42 @@
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 'json_api_resource'
7
+ require "json_api_resource_connecitons/version"
8
+
9
+ module JsonApiResourceConnections
10
+ require 'json_api_resource/connections'
11
+ require 'json_api_resource/cache_processor'
12
+
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+
17
+ include Multiconnect::Connectable
18
+
19
+ class << self
20
+ class_attribute :_fallbacks
21
+ self._fallbacks = []
22
+
23
+ def cache_fallback(*actions)
24
+ self._fallbacks = _fallbacks + Array(actions)
25
+ add_connection JsonApiResource::Connections::CacheConnection, client: self.client_class, only: self._fallbacks
26
+ end
27
+
28
+ def wraps(client)
29
+ self.client_class = client
30
+
31
+ # now that we know where to connect to, let's do it
32
+ add_connection JsonApiResource::Connections::CachedCircuitbreakerServerConnection, client: self.client_class
33
+ end
34
+ end
35
+
36
+ def connection
37
+ @connection ||= JsonApiResource::Connections::CachedCircuitbreakerServerConnection.new( client: self.client, caching: false )
38
+ end
39
+ end
40
+ end
41
+
42
+ JsonApiResource::Resource.send :include, JsonApiResourceConnections
@@ -0,0 +1,3 @@
1
+ module JsonApiResourceConnecitons
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_api_resource_connecitons
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Greg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-15 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_connecitons.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.rb
104
+ - lib/json_api_resource/connections/cache_connection.rb
105
+ - lib/json_api_resource/connections/cached_circuitbreaker_server_connection.rb
106
+ - lib/json_api_resource/connections/server_not_ready_error.rb
107
+ - lib/json_api_resource_connecitons.rb
108
+ - lib/json_api_resource_connecitons/version.rb
109
+ homepage: https://github.com/avvo/json_api_resource_connections
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.4.6
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: circuit breaker and cache fallback connections for json api resource v2
133
+ test_files: []