Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

serial_fetcher

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serial_fetcher

  • 1.0.3
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

SerialFetcher

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.

Installation

Add this line to your application's Gemfile:

gem 'serial_fetcher'

And then execute:

$ bundle

Or install it yourself as:

$ gem install serial_fetcher

Usage

Suppose you have an url like :

http://localhost:3000/users/1/blogs/5/posts/35/comments/3

The corresponding params are :

params: {
  user_id: "1",
  blog_id: "5",
  post_id: "35",
  id: "3"
}

Now in your comments_controller.rb, you probably have something like :

@user = User.find params[:user_id]
@blog = Blog.find params[:blog_id]
@post = Post.find params[:post_id]
@comment = User.find params[:id]

With SerialFetcher, you could just write :

@store = SerialFetcher.fetch(params, id: :comment)

And then @store will be created with :

@store = {
  user: User.find params[:user_id],
  blog: Blog.find params[:blog_id],
  post: Post.find params[:post_id],
  comment: Comment.find params[:id]
}

You must provide the resource associated to the :id param. The script will parse all the params containing '_id', and try to fetch the corresponding resources.

Particular case if your param is not named like the model :

schema: {
  id: :comment,
  post_id: :article,
}

@store = SerialFetcher.fetch(params, schema)

And then @store will be created with :

@store = {
  article: Article.find params[:post_id],
  comment: Comment.find params[:id]
}

You can provide your own fetcher in an initializer :

SerialFetcher.configure |config|

  # If we define repositories for our models, with a
  # fetch_for_id method
  config.fetcher = ->(param_name, param) {
    begin
      klass_name = "#{param_name}_repository".camelize
      klass = klass_name.constantize
      klass.send(:fetch_for_id, param)
    rescue NameError
      # The repository does not exist for the given param
      nil
    end
  }

Development

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.

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/o-bo/serial_fetcher.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 16 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc