faraday-lazyable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +1 -0
- data/faraday-lazyable.gemspec +23 -0
- data/lib/faraday-lazyable.rb +1 -0
- data/lib/faraday/lazyable.rb +15 -0
- data/lib/faraday/lazyable/dummy_response.rb +27 -0
- data/lib/faraday/lazyable/version.rb +5 -0
- data/spec/faraday/lazyable_spec.rb +80 -0
- data/spec/spec_helper.rb +8 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6fac454b5e6dfbf8d9a1dcb28a8b8e4d81a561d2
|
4
|
+
data.tar.gz: 979f4f44623c038cf2b3b2df72224d9919c99b18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c6665e619166c5ded3db1d15d556c8eb85f90f7db483ed199938344d2184e0158d27df6da8f81501702c9f57504849a82cd996203db65cc22870662f15bf26c
|
7
|
+
data.tar.gz: 5d9700cade367146152ac4d1653e950381fa2a7b995443fba3b765b3ed909654bc2e48a2c54245e43bc6b3f6eba1d4a102bf530fb1d1b6b97fb1598aefee3144
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ryo Nakamura
|
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,73 @@
|
|
1
|
+
# Faraday::Lazyable
|
2
|
+
A request is never sent till a response have need.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
```
|
6
|
+
gem install faraday-lazyable
|
7
|
+
```
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
```ruby
|
11
|
+
require "faraday-lazyable"
|
12
|
+
|
13
|
+
connection = Faraday.new do |connection|
|
14
|
+
connection.use Faraday::Lazyable
|
15
|
+
connection.adapter :net_http
|
16
|
+
connection.response :logger
|
17
|
+
end
|
18
|
+
|
19
|
+
# A dummy response is returned.
|
20
|
+
response = connection.get("http://example.com")
|
21
|
+
|
22
|
+
# The HTTP request is sent when any method call happened to the response.
|
23
|
+
response.status #=> 200
|
24
|
+
```
|
25
|
+
|
26
|
+
## Example
|
27
|
+
Suppose that you are building a client web application for your API server.
|
28
|
+
Your controller sends HTTP request to the server.
|
29
|
+
You're trying to cache the HTTP request
|
30
|
+
by using [Fragment Cache](http://guides.rubyonrails.org/caching_with_rails.html) in the view layer
|
31
|
+
to improve performance.
|
32
|
+
|
33
|
+
### Controller
|
34
|
+
```ruby
|
35
|
+
class RecipesController < ApplicationController
|
36
|
+
def show
|
37
|
+
@recipe = client.get("http://example.com/recipes/#{params[:id]}")
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def client
|
43
|
+
Faraday.new
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
### View
|
49
|
+
```erb
|
50
|
+
<% cache do %>
|
51
|
+
<article class="recipe">
|
52
|
+
<h1><%= @recipe.title %></h1>
|
53
|
+
<p><%= @recipe.description %></p>
|
54
|
+
</article>
|
55
|
+
<% end %>
|
56
|
+
```
|
57
|
+
|
58
|
+
### Problem
|
59
|
+
But you found that you cannot cache the HTTP request this way
|
60
|
+
because the HTTP request is already sent at the controller.
|
61
|
+
|
62
|
+
### Solution
|
63
|
+
Faraday::Lazyable fits this type of problem.
|
64
|
+
The response (`@recipe`) will become lazy-evaluated
|
65
|
+
and the HTTP request will never be sent till `@recipe.title` is called at the view.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
def client
|
69
|
+
Faraday.new do |connection|
|
70
|
+
connection.use Faraday::Lazyable
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'faraday/lazyable/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "faraday-lazyable"
|
7
|
+
spec.version = Faraday::Lazyable::VERSION
|
8
|
+
spec.authors = ["Ryo Nakamura"]
|
9
|
+
spec.email = ["r7kamura@gmail.com"]
|
10
|
+
spec.summary = "A request is never sent till a response have need."
|
11
|
+
spec.homepage = "https://github.com/r7kamura/faraday-lazyable"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "faraday"
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec", "2.14.1"
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "faraday/lazyable"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "faraday/lazyable/dummy_response"
|
3
|
+
require "faraday/lazyable/version"
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Lazyable
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
DummyResponse.new { @app.call(env) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Faraday
|
2
|
+
class Lazyable
|
3
|
+
class DummyResponse < BasicObject
|
4
|
+
instance_methods.each do |method_name|
|
5
|
+
undef_method(method_name) unless method_name == :__send__
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(&block)
|
9
|
+
@block = block
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to?(method_name)
|
13
|
+
__response__.respond_to?(method_name)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def method_missing(method_name, *args, &block)
|
19
|
+
__response__.__send__(method_name, *args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def __response__
|
23
|
+
@response ||= @block.call
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Faraday::Lazyable do
|
4
|
+
let(:client) do
|
5
|
+
Faraday.new do |connection|
|
6
|
+
connection.use Faraday::Lazyable
|
7
|
+
connection.adapter :test, stubs
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:stubs) do
|
12
|
+
Faraday::Adapter::Test::Stubs.new do |stub|
|
13
|
+
stub.get(url) do
|
14
|
+
history << true
|
15
|
+
[response_status, response_headers, response_body]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:url) do
|
21
|
+
"/"
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:response_status) do
|
25
|
+
200
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:response_headers) do
|
29
|
+
{}
|
30
|
+
end
|
31
|
+
|
32
|
+
let(:response_body) do
|
33
|
+
"OK"
|
34
|
+
end
|
35
|
+
|
36
|
+
let(:history) do
|
37
|
+
[]
|
38
|
+
end
|
39
|
+
|
40
|
+
context "till any method is called to the response" do
|
41
|
+
it "does not send any request" do
|
42
|
+
response = client.get(url)
|
43
|
+
history.should be_empty
|
44
|
+
response.body
|
45
|
+
history.should_not be_empty
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "after any method is called to the response" do
|
50
|
+
it "behaves like a real response" do
|
51
|
+
response = client.get(url)
|
52
|
+
response.status.should == response_status
|
53
|
+
response.headers.should == response_headers
|
54
|
+
response.body.should == response_body
|
55
|
+
response.should be_a Faraday::Response
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "even if 2 methods are called to the response" do
|
60
|
+
it "sends only 1 request" do
|
61
|
+
response = client.get(url)
|
62
|
+
response.headers
|
63
|
+
response.body
|
64
|
+
history.size.should == 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "when undefined method is called to the response" do
|
69
|
+
it "raises NoMethodError" do
|
70
|
+
response = client.get(url)
|
71
|
+
expect { response.undefined_method }.to raise_error(NoMethodError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "sends a request" do
|
75
|
+
response = client.get(url)
|
76
|
+
response.undefined_method rescue nil
|
77
|
+
history.size.should == 1
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday-lazyable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryo Nakamura
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
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.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.1
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- r7kamura@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- faraday-lazyable.gemspec
|
82
|
+
- lib/faraday-lazyable.rb
|
83
|
+
- lib/faraday/lazyable.rb
|
84
|
+
- lib/faraday/lazyable/dummy_response.rb
|
85
|
+
- lib/faraday/lazyable/version.rb
|
86
|
+
- spec/faraday/lazyable_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: https://github.com/r7kamura/faraday-lazyable
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.0.3
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A request is never sent till a response have need.
|
112
|
+
test_files:
|
113
|
+
- spec/faraday/lazyable_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
has_rdoc:
|