multiconnect 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 991ba66412af2a541aed7a547312cd01b4a59b73
4
+ data.tar.gz: eff776e9c9f55a27e1ba87ca13430d2bf077c51a
5
+ SHA512:
6
+ metadata.gz: e1db9b9c47e7551fd3a713641bd08303768e667a80057846ac68a523bb93b5dbbe86f0a3875a0d8e8172a31a0197120e67a34ca03f2f8cd458763af7491fb5c5
7
+ data.tar.gz: 2778d5152b747fa0378e45decb9bc5855ddf85ff930f272f42e8bfb6101b45bd907302922c71938394e887fecd94895a26158b64dac596eb9d4c3885fcbe3875
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,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multiconnect.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Greg Orlov
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,73 @@
1
+ # Multiconnect
2
+
3
+ Multiconnect is a way to manage your server conenction fallbacks.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'multiconnect'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install multiconnect
20
+
21
+ ## Usage
22
+
23
+ Let's say you have an object that wraps [JsonApiClient](http://github.com/chingor13/json_api_client) and you want to make sure that your client server has some level of failure tolerance.
24
+
25
+ You want your client to make a request to the server, but if it fails you want to try to pull from cache. Here's a simple way to do this:
26
+
27
+ ```ruby
28
+ module Zappos
29
+ module Client
30
+ class Shoe
31
+ self.site = "http://zappos.com/api"
32
+ end
33
+ end
34
+ end
35
+
36
+ class Shoe
37
+ self.client_class = Zappos::Client::Shoe
38
+
39
+ include Multiconnect::Connectable
40
+
41
+ # block of connections
42
+
43
+ # try to hit the server directly. If the server fails, disallow calls for 30 seconds
44
+ add_connection :server, client: client_class, class: CircuitBreakerServerConnection
45
+
46
+ # If the server call failed, try to fetch the value from cache. This assumes you cache that somewhere
47
+ add_connection :cache_fallback
48
+
49
+
50
+ # whatever your client calling code is
51
+ def self.call_client(action, *args)
52
+ request(action, *args)
53
+ end
54
+ end
55
+ ```
56
+
57
+ You can find all these connections in the [examples file](/example_connections.rb)
58
+
59
+
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/gaorlov/multiconnect/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rake/testtask'
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'lib'
9
+ t.libs << 'test'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = false
12
+ end
13
+
14
+
15
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "multiconnect"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,187 @@
1
+
2
+
3
+ # crude implementation without resource helpers
4
+ class Object << JsonApiResource::Resource
5
+ add_connection CacheFirstToCircuitBreakerServerConnection, client: client_class # no autmatic fallthrough to the server IN the connection. No client_class because it'll use Rails.cache
6
+ # Solicitor::Client::Lawyer
7
+ add_connection CircuitBreakerServerConnection, client: client_class
8
+ add_connection CacheFallbackConnection # no use trying to hit the client now. We laready know the server is unresponsive
9
+ add_connection DefaultValueConnection, client: self
10
+ end
11
+
12
+
13
+ module JsonApiResource
14
+ module Connection
15
+ class ServerConnection < Multiconnect::Connection::Base
16
+
17
+ def report_error(e)
18
+ Honeybadger.notify(e)
19
+ end
20
+
21
+ def request(action, *args)
22
+ client.send action, *args
23
+
24
+ rescue JsonApiClient::Errors::NotFound => e
25
+ result = JsonApiClient::ResultSet.new
26
+
27
+ result.meta = {status: status}
28
+
29
+ result.errors = ActiveModel::Errors.new(result)
30
+ result.errors.add("NofFound", e.message)
31
+
32
+ result
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ module JsonApiResource
40
+ module Connection
41
+ class CacheFallbackConnection < Multiconnect::Connection::Base
42
+
43
+ def report_error(e)
44
+ end
45
+
46
+ def request(action, *args)
47
+ key = sorted_args(args)
48
+ Rails.cache.fetch key
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+
55
+ module JsonApiResource
56
+ module Connection
57
+ class CacheFirstToCircuitBreakerServerConnection < CircuitBreakerServerConnection
58
+
59
+ def report_error(e)
60
+ Honeybadger.notify(e)
61
+ InfluxDB.notify(e)
62
+ end
63
+
64
+ def request(action, *args)
65
+ key = key_from_args(args)
66
+ Rails.cache.fetch key do
67
+ super
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def key_from_args(args)
74
+ # downcase everything and sort the keys alpabetically so that {q: "dui", loc: "seattle"} is the same as {loc:"seattle", q: "dui"}
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ module JsonApiResource
81
+ module Connection
82
+ class CircuitBreakerServerConnection < Multiconnect::Connection::Base
83
+
84
+ def report_error(e)
85
+ Honeybadger.notify(e)
86
+ InfluxDB.notify(e)
87
+ end
88
+
89
+ def request(action, *args)
90
+ if ready_for_request?
91
+ client.send :find, args
92
+ else
93
+ raise "fall through!"
94
+ end
95
+
96
+ rescue JsonApiClient::Errors::NotFound => e
97
+ result = JsonApiClient::ResultSet.new
98
+
99
+ result.meta = {status: status}
100
+
101
+ result.errors = ActiveModel::Errors.new(result)
102
+ result.errors.add( "FileNotFound", e.message )
103
+
104
+ result
105
+ rescue => e
106
+ @responding = false
107
+ # default circuit broken for 30 seconds. This should probably be 1 - 2 - 5 - 15 - 30 - 1 min - 2 min*
108
+ @timeout = 30.seconds.from.
109
+
110
+ # propagate the error up to be handled by Connection::Base
111
+ raise e
112
+ end
113
+
114
+ private
115
+
116
+ def ready_for_request?
117
+ @responding || Time.now > @timeout
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+
124
+ class Shoe
125
+ self.client_class = Faraday
126
+
127
+ include Multiconnect::Connectable
128
+
129
+ # block of connections
130
+
131
+ # try to hit the server directly. If the server fails, disallow calls for 30 seconds
132
+ add_connection :server, client: Solicitor::Client::Lawyer, class: CircuitBreakerServerConnection
133
+
134
+ # If the server call failed, try to fetch the value from cache. This assumes you cache that somewhere
135
+ add_connection :cache_fallback
136
+
137
+
138
+ # whatever your client calling code is
139
+ def self.where(opts)
140
+ request opts do
141
+ client.where
142
+ end
143
+
144
+ def self.find(opts)
145
+ request(:find, opts).data
146
+ end
147
+ end
148
+
149
+
150
+ Shoe.request(key: 'asdf') do |connection_client|
151
+ connection_client.where(foo: 'bar').first
152
+ end
153
+
154
+
155
+ class Lawyer < JsonApiResource::Resource
156
+ wraps Solicitor::Client::Lawyer
157
+
158
+ self.legal_zoom_client = LZ::Client::Lawyer
159
+
160
+ add_connection :avvo_api, client: Faraday
161
+ add_connection :legal_zoom, client: legal_zoom_client
162
+
163
+ def self.find(*args)
164
+ request = request( :find, args )
165
+ connection_status = request.connection
166
+ request.data
167
+ end
168
+
169
+ end
170
+
171
+ # controler
172
+
173
+ def show
174
+ @lawyer = Lawyer.find(params[:id])
175
+ render_500 if @lawyer.using_fallback(:cache_fallback)?
176
+ end
177
+
178
+ def search
179
+ @lawyers = Lawyer.search(params)
180
+
181
+ @lawyers.using_fallback(:cache_fallback)?
182
+
183
+ end
184
+
185
+ def index
186
+ @lawyers = Lawyer.where(params[:whatever], per_page: 4, order: :created_at)
187
+ end
@@ -0,0 +1,9 @@
1
+ require "multiconnect/version"
2
+ require 'active_support'
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/class/attribute'
5
+
6
+ module Multiconnect
7
+ autoload :Connection, 'multiconnect/connection'
8
+ autoload :Connectable, 'multiconnect/connectable'
9
+ end
@@ -0,0 +1,26 @@
1
+ module Multiconnect
2
+ module Connectable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class << self
7
+ class_attribute :_connections
8
+ self._connections = []
9
+
10
+ def add_connection(connection_class, options = {})
11
+
12
+ self._connections = _connections + [connection_class.new(options)]
13
+
14
+ end
15
+
16
+ def request(action, *args)
17
+ self._connections.each do |connection|
18
+ result = connection.execute(action, *args)
19
+ return result if result.success?
20
+ end
21
+ raise "Multiconnect Error: #{self} has failed to request #{action}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ module Multiconnect
2
+ module Connection
3
+ autoload :Base, 'multiconnect/connection/base'
4
+ autoload :Result, 'multiconnect/connection/result'
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ module Multiconnect
2
+ module Connection
3
+ class Base
4
+
5
+ attr_accessor :client
6
+
7
+ def initialize(options = {})
8
+ self.client = options.fetch :client, nil
9
+ @except = Array(options.fetch :except, [])
10
+ end
11
+
12
+ def request(action, *args)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ def report_error(e)
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def execute(action, *args)
21
+ if @except.include? action
22
+ Result.new
23
+ else
24
+ Result.new status: Result::SUCCESS, data: request(action, *args), connection: self.class
25
+ end
26
+
27
+ rescue => e
28
+ report_error(e)
29
+ Result.new
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ module Multiconnect
2
+ module Connection
3
+ class Result
4
+ SUCCESS = :success
5
+ FAILURE = :failure
6
+
7
+ def initialize(opts = {})
8
+ @status = opts.fetch :status, FAILURE
9
+ @data = opts.fetch :data, nil
10
+ @connection = opts.fetch :connection, nil
11
+ end
12
+
13
+ def success?
14
+ @status == SUCCESS
15
+ end
16
+
17
+ def using_fallback?(connection)
18
+ @connection == connection
19
+ end
20
+
21
+ def method_missing(method, *args, &block)
22
+ @data.send method, *args, &block
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Multiconnect
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multiconnect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multiconnect"
8
+ spec.version = Multiconnect::VERSION
9
+ spec.authors = ["Greg Orlov"]
10
+ spec.email = ["gaorlov@gmail.com"]
11
+
12
+ spec.summary = %q{Allows a client to try several methods of fetching data with fallbacks}
13
+ spec.description = %q{Allows a client to try several methods of fetching data with fallbacks}
14
+ spec.homepage = "https://github.com/gaorlov/multiconnect"
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
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest"
27
+ spec.add_development_dependency "m"
28
+ spec.add_development_dependency "simplecov"
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multiconnect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Greg Orlov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: m
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Allows a client to try several methods of fetching data with fallbacks
98
+ email:
99
+ - gaorlov@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - example_connections.rb
113
+ - lib/multiconnect.rb
114
+ - lib/multiconnect/connectable.rb
115
+ - lib/multiconnect/connection.rb
116
+ - lib/multiconnect/connection/base.rb
117
+ - lib/multiconnect/connection/result.rb
118
+ - lib/multiconnect/version.rb
119
+ - multiconnect.gemspec
120
+ homepage: https://github.com/gaorlov/multiconnect
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Allows a client to try several methods of fetching data with fallbacks
144
+ test_files: []