serial_fetcher 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/serial_fetcher.rb +38 -0
- data/lib/serial_fetcher/configuration.rb +13 -0
- data/lib/serial_fetcher/fetchers/active_record_fetcher.rb +18 -0
- data/lib/serial_fetcher/version.rb +3 -0
- data/serial_fetcher.gemspec +24 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9473c55de75f60712d7fc3414f8ddfc1432c1d5a
|
4
|
+
data.tar.gz: 46459962b81d3f6fa43476bf2de61b12d192e1a3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c4c6852aac20e727fc5aa4d639b3cd915b594049edc1b3c909c9941cfbf9dd97fd9796c164b2874d627b29601bf6348b225a05bbf40af54b009acb4424d3333a
|
7
|
+
data.tar.gz: 56f8483cdb10b09d7498a40d998120adfaef7089b74df437518aa91ef786e72e24a16106f8d9dac9f7a1cafd5004f3026cd89c170c55170f847ead5a9ca34a67
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
serial_fetcher
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Olivier Berho
|
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,128 @@
|
|
1
|
+
# SerialFetcher
|
2
|
+
|
3
|
+
Serial Fetcher provides a way to automatically fetch a list of resources from
|
4
|
+
the params hash.
|
5
|
+
|
6
|
+
The params must be passed to the `fetch` method with a `schema` describing the
|
7
|
+
associations between params and models.
|
8
|
+
|
9
|
+
A fetcher must be provided to the SerialFetcher through configuration.
|
10
|
+
By default there is an ActiveRecord fetcher that call `find` to the constantized
|
11
|
+
class name.
|
12
|
+
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'serial_fetcher'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install serial_fetcher
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Suppose you have an url like :
|
33
|
+
|
34
|
+
http://localhost:3000/users/1/blogs/5/posts/35/comments/3
|
35
|
+
|
36
|
+
The corresponding params are :
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
params: {
|
40
|
+
user_id: "1",
|
41
|
+
blog_id: "5",
|
42
|
+
post_id: "35",
|
43
|
+
id: "3"
|
44
|
+
}
|
45
|
+
```
|
46
|
+
|
47
|
+
Now in your comments_controller.rb, you probably have something like :
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
@user = User.find params[:user_id]
|
51
|
+
@blog = Blog.find params[:blog_id]
|
52
|
+
@post = Post.find params[:post_id]
|
53
|
+
@comment = User.find params[:id]
|
54
|
+
```
|
55
|
+
|
56
|
+
With SerialFetcher, you could just write :
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
@store = SerialFetcher.fetch(params, id: :comment)
|
60
|
+
```
|
61
|
+
|
62
|
+
And then @store will be created with :
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
@store = {
|
66
|
+
user: User.find params[:user_id],
|
67
|
+
blog: Blog.find params[:blog_id],
|
68
|
+
post: Post.find params[:post_id],
|
69
|
+
comment: Comment.find params[:id]
|
70
|
+
}
|
71
|
+
```
|
72
|
+
You must provide the resource associated to the :id param.
|
73
|
+
The script will parse all the params containing '_id', and try to fetch the
|
74
|
+
corresponding resources.
|
75
|
+
|
76
|
+
Particular case if your param is not named like the model :
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
schema: {
|
80
|
+
id: :comment,
|
81
|
+
post_id: :article,
|
82
|
+
}
|
83
|
+
|
84
|
+
@store = SerialFetcher.fetch(params, schema)
|
85
|
+
```
|
86
|
+
|
87
|
+
And then @store will be created with :
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
@store = {
|
91
|
+
article: Article.find params[:post_id],
|
92
|
+
comment: Comment.find params[:id]
|
93
|
+
}
|
94
|
+
```
|
95
|
+
You can provide your own fetcher in an initializer :
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
SerialFetcher.configure |config|
|
99
|
+
|
100
|
+
# If we define repositories for our models, with a
|
101
|
+
# fetch_for_id method
|
102
|
+
config.fetcher = ->(param_name, param) {
|
103
|
+
begin
|
104
|
+
klass_name = "#{param_name}_repository".camelize
|
105
|
+
klass = klass_name.constantize
|
106
|
+
klass.send(:fetch_for_id, param)
|
107
|
+
rescue NameError
|
108
|
+
# The repository does not exist for the given param
|
109
|
+
nil
|
110
|
+
end
|
111
|
+
}
|
112
|
+
```
|
113
|
+
|
114
|
+
## Development
|
115
|
+
|
116
|
+
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.
|
117
|
+
|
118
|
+
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).
|
119
|
+
|
120
|
+
## Contributing
|
121
|
+
|
122
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/o-bo/serial_fetcher.
|
123
|
+
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
128
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "serial_fetcher"
|
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,38 @@
|
|
1
|
+
require "serial_fetcher/version"
|
2
|
+
require "serial_fetcher/configuration"
|
3
|
+
|
4
|
+
module SerialFetcher
|
5
|
+
|
6
|
+
class << self
|
7
|
+
attr_accessor :configuration
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
self.configuration ||= Configuration.new
|
13
|
+
yield(configuration) if block_given?
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def self.fetch(params, schema={})
|
18
|
+
config = self.configuration ||= Configuration.new
|
19
|
+
fetcher = config.fetcher
|
20
|
+
|
21
|
+
raise "You must provide a fetcher method." if fetcher.nil?
|
22
|
+
raise "You must provide a hash for params." if params.nil?
|
23
|
+
raise "You must provide the key id for the basic resource to be fetched." if schema[:id].nil?
|
24
|
+
|
25
|
+
params.keys.each_with_object({}) do |k, res|
|
26
|
+
key_id = schema[k]
|
27
|
+
if key_id
|
28
|
+
param_name = key_id.to_s
|
29
|
+
res[key_id] = fetcher.(param_name, params[k])
|
30
|
+
else
|
31
|
+
if /.*_id/.match(k.to_s)
|
32
|
+
param_name = k.to_s.split('_id').first
|
33
|
+
res[param_name.to_sym] = fetcher.(param_name, params[k])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SerialFetcher
|
2
|
+
module Fetchers
|
3
|
+
class ActiveRecordFetcher
|
4
|
+
|
5
|
+
def self.get
|
6
|
+
->(param_name, param) {
|
7
|
+
begin
|
8
|
+
klass_name = param_name.camelize
|
9
|
+
klass = klass_name.constantize
|
10
|
+
klass.send(:find, param)
|
11
|
+
rescue NameError
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'serial_fetcher/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "serial_fetcher"
|
8
|
+
spec.version = SerialFetcher::VERSION
|
9
|
+
spec.authors = ["o-bo"]
|
10
|
+
|
11
|
+
spec.summary = %q{A script to ease fetching data from hash params in ruby applications.}
|
12
|
+
spec.description = %q{Serial Fetcher provides a way to automatically fetch a list of resources from the params hash. The params must be passed to the `fetch` method with a `schema` describing the associations between params and models. A fetcher must be provided to the SerialFetcher through configuration. By default there is an ActiveRecord fetcher that call `find` to the constantized class name.}
|
13
|
+
spec.homepage = "https://github.com/o-bo/serial_fetcher"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serial_fetcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- o-bo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Serial Fetcher provides a way to automatically fetch a list of resources
|
56
|
+
from the params hash. The params must be passed to the `fetch` method with a `schema`
|
57
|
+
describing the associations between params and models. A fetcher must be provided
|
58
|
+
to the SerialFetcher through configuration. By default there is an ActiveRecord
|
59
|
+
fetcher that call `find` to the constantized class name.
|
60
|
+
email:
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".ruby-gemset"
|
67
|
+
- ".ruby-version"
|
68
|
+
- ".travis.yml"
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- bin/console
|
74
|
+
- bin/setup
|
75
|
+
- lib/serial_fetcher.rb
|
76
|
+
- lib/serial_fetcher/configuration.rb
|
77
|
+
- lib/serial_fetcher/fetchers/active_record_fetcher.rb
|
78
|
+
- lib/serial_fetcher/version.rb
|
79
|
+
- serial_fetcher.gemspec
|
80
|
+
homepage: https://github.com/o-bo/serial_fetcher
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.1
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: A script to ease fetching data from hash params in ruby applications.
|
104
|
+
test_files: []
|