faraday-manual-cache 0.0.1

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: 72ed2d3a4eb4bacc4e2f682d7d2f0621aecfc2ae
4
+ data.tar.gz: 1ff4fcf690ae465b312fa324152d40bd4bc1910a
5
+ SHA512:
6
+ metadata.gz: 781465c3431f1167f6dea2df07b617b1a4b8de965dbfd890e6a018ea50792a2b84d7a630760d67bafa611d82c061fc68bc6b6393ef4849225670f42719469d24
7
+ data.tar.gz: 5dd8050ea269687ce3ccf5137d42ae20001382f11763ddf5cb4088deeb43be7575e4e6513d9e5a7c2f9d89e43c8f10fb744ccb8332aa8c37af7d98e064fcbce0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ FileName:
2
+ Description: 'Use snake_case for source file names.'
3
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday-manual-cache.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Daniel O'Brien
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Faraday::ManualCache
2
+
3
+ A super simple Faraday cache implementation.
4
+
5
+ Unlike [`faraday-http-cache`](https://github.com/plataformatec/faraday-http-cache), `faraday-manual-cache` ignores cache headers in favor of a manually-specified `expires_in`.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'faraday-manual-cache'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install faraday-manual-cache
20
+
21
+ ## Usage
22
+
23
+ Super simple example that caches using the default store (`MemoryStore`) for the default expires_in (30 seconds):
24
+
25
+ ```ruby
26
+ require 'faraday'
27
+ require 'faraday-manual-cache'
28
+
29
+ connection = Faraday.new(url: 'http://example.com') do |builder|
30
+ builder.use :manual_cache
31
+ builder.adapter Faraday.default_adapter
32
+ end
33
+ ```
34
+ The middleware currently takes several options:
35
+
36
+ * `expires_in`: Cache expiry, in seconds (default 30).
37
+ * `store`: The `ActiveSupport::Cache`-compatible store to use (default `ActiveSupport::Cache::MemoryStore`).
38
+ * `store_options`: Options passed to the store if created by lookup (e.g. when specifying `:memory_store`, `:redis_store`, etc).
39
+
40
+ So a more complicated example would be:
41
+
42
+ ```ruby
43
+ require 'faraday'
44
+ require 'faraday-manual-cache'
45
+ require 'redis-rails'
46
+
47
+ connection = Faraday.new(url: 'http://example.com') do |builder|
48
+ builder.use :manual_cache, expires_in: 10, store: :redis_store, store_options: { host: 'my-redis-server', port: '1234' }
49
+ builder.adapter Faraday.default_adapter
50
+ end
51
+ ```
52
+
53
+ As with `faraday-http-cache` it's recommended that `faraday-manual-cache` be fairly low in the middleware stack.
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( http://github.com/dobs/faraday-manual-cache/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
62
+
63
+ ## Attribution
64
+
65
+ Some implementation details taken from [`faraday-http-cache`](https://github.com/plataformatec/faraday-http-cache).
66
+
67
+ ## Contributors
68
+
69
+ * Maintainer: [Daniel O'Brien](http://github.com/dobs)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'faraday-manual-cache'
4
+ spec.version = '0.0.1'
5
+ spec.authors = ['Daniel O\'Brien']
6
+ spec.email = ['dan@dobs.org']
7
+ spec.summary = %q(A super simple Faraday cache implementation.)
8
+ spec.description = %q(A super simple Faraday cache implementation.)
9
+ spec.homepage = 'https://github.com/dobs/faraday-manual-cache'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files -z`.split("\x0")
13
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'activesupport', '>= 3.0.0'
18
+ spec.add_dependency 'faraday', '~> 0.9'
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.5'
21
+ spec.add_development_dependency 'rake'
22
+ end
@@ -0,0 +1,74 @@
1
+ require 'faraday'
2
+
3
+ module Faraday
4
+ # Middleware for caching Faraday responses based on a specified expiry.
5
+ #
6
+ # As with faraday-http-cache, it's recommended that this middleware be added
7
+ # fairly low in the middleware stack.
8
+ #
9
+ # Currently accepts four arguments:
10
+ #
11
+ # :expires_in - Cache expiry, in seconds (default: 30).
12
+ # :store - An object (or lookup symbol) for an
13
+ # ActiveSupport::Cache::Store instance. (default:
14
+ # MemoryStore).
15
+ # :store_options - Options to pass to the store when generated based on a
16
+ # lookup symvol (default: {}).
17
+ class ManualCache < Faraday::Middleware
18
+ def initialize(app, *args)
19
+ super(app)
20
+ options = args.first || {}
21
+ @expires_in = options.fetch(:expires_in, 30)
22
+ @store = options.fetch(:store, :memory_store)
23
+ @store_options = options.fetch(:store_options, {})
24
+
25
+ initialize_store
26
+ end
27
+
28
+ def call(env)
29
+ dup.call!(env)
30
+ end
31
+
32
+ protected
33
+
34
+ def call!(env)
35
+ return to_response(cached_response(env)) if cacheable?(env) &&
36
+ cached_response(env)
37
+
38
+ @app.call(env).on_complete do |response_env|
39
+ cache_response(response_env) if cacheable?(env)
40
+ end
41
+ end
42
+
43
+ # Cache the env to the store.
44
+ def cache_response(env)
45
+ @store.write(env.url, env, expires_in: @expires_in)
46
+ end
47
+
48
+ # Whether or not the env is cacheable.
49
+ def cacheable?(env)
50
+ env.method == :get || env.method == :head
51
+ end
52
+
53
+ # Retrieve (and memoize) cached response matching current env.
54
+ def cached_response(env)
55
+ @cached_response ||= @store.fetch(env.url)
56
+ end
57
+
58
+ # Checks whether the specified store is a symbol, and if so attempts to
59
+ # do a lookup against ActiveSupport::Cache.
60
+ def initialize_store
61
+ return unless @store.is_a? Symbol
62
+
63
+ require 'active_support/cache'
64
+ @store = ActiveSupport::Cache.lookup_store(@store, @store_options)
65
+ end
66
+
67
+ # Massage env into a Response object.
68
+ def to_response(env)
69
+ response = Response.new
70
+ response.finish(env) unless env.parallel?
71
+ env.response = response
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,7 @@
1
+ require 'faraday/manual_cache'
2
+
3
+ if Faraday.respond_to?(:register_middleware)
4
+ Faraday.register_middleware manual_cache: Faraday::ManualCache
5
+ elsif Faraday::Middleware.respond_to?(:register_middleware)
6
+ Faraday::Middleware.register_middleware manual_cache: Faraday::ManualCache
7
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-manual-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel O'Brien
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-22 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: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
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.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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
+ description: A super simple Faraday cache implementation.
70
+ email:
71
+ - dan@dobs.org
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rubocop.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - faraday-manual-cache.gemspec
83
+ - lib/faraday-manual-cache.rb
84
+ - lib/faraday/manual_cache.rb
85
+ homepage: https://github.com/dobs/faraday-manual-cache
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: A super simple Faraday cache implementation.
109
+ test_files: []
110
+ has_rdoc: