项目作者: ianks

项目描述 :
Hit a million different APIs and combine the results in one simple hash (without pulling your hair out). A simple workflow system to gather aggregate attributes for something.
高级语言: Ruby
项目地址: git://github.com/ianks/attr-gather.git
创建时间: 2019-10-24T04:03:23Z
项目社区:https://github.com/ianks/attr-gather

开源协议:MIT License

下载


attr-gather

Actions Status
Maintainability

A gem for creating workflows that “enhance” entities with extra attributes. At a high level, attr-gather provides a process to fetch information from many data sources (such as third party APIs, legacy databases, etc.) in a fully parallelized fashion.

Usage

Defining your workflow

  1. # define a workflow
  2. class EnhanceProfile
  3. include Attr::Gather::Workflow
  4. # contains all the task implementations
  5. container TasksContainer
  6. # filter out invalid data using a Dry::Validation::Contract
  7. # anything that doesn't match this schema will be filtered out
  8. filter_with_contract do
  9. params do
  10. required(:user_id).filled(:integer)
  11. optional(:user).hash do
  12. optional(:name).filled(:string)
  13. optional(:email).filled(:string)
  14. optional(:gravatar).filled(:string)
  15. optional(:email_info).hash do
  16. optional(:deliverable).filled(:bool?)
  17. optional(:free).filled(:bool?)
  18. end
  19. end
  20. end
  21. end
  22. # each task returns a hash of data that will be merged into the result
  23. task :fetch_post do |t|
  24. t.depends_on = []
  25. end
  26. # will run in parallel
  27. task :fetch_user do |t|
  28. t.depends_on = [:fetch_post]
  29. end
  30. # will run in parallel
  31. task :fetch_email_info do |t|
  32. t.depends_on = [:fetch_user]
  33. end
  34. end

Defining some tasks

  1. class PostFetcher
  2. def call(attrs)
  3. res = HTTP.get("https://jsonplaceholder.typicode.com/posts/#{attrs[:id]}")
  4. post = JSON.parse(res.to_s, symbolize_names: true)
  5. { title: post[:title], user_id: post[:userId], body: post[:body] }
  6. end
  7. end
  1. class UserFetcher
  2. # will have access to the PostFetcher attributes here
  3. def call(attrs)
  4. res = HTTP.get("https://jsonplaceholder.typicode.com/users/#{attrs[:user_id]}")
  5. user = JSON.parse(res.to_s, symbolize_names: true)
  6. { user: { name: user[:name], email: user[:email] } }
  7. end
  8. end
  1. class EmailInfoFetcher
  2. # will have access to the PostFetcher attributes here
  3. def call(user:)
  4. res = HTTP.timeout(3).get("https://api.trumail.io/v2/lookups/json?email=#{user[:email]}")
  5. info = JSON.parse(res.to_s, symbolize_names: true)
  6. # will deep merge with the final result
  7. { user: { email_info: { deliverable: info[:deliverable], free: info[:free] } } }
  8. end
  9. end

Registering your tasks

  1. class MyContainer
  2. extend Dry::Container::Mixin
  3. register :fetch_post, PostFetcher
  4. register :fetch_user, UserFetcher
  5. register :fetch_email_info, EmailInfoFetcher
  6. end

Run it!

  1. enhancer = EnhanceUserProfile.new
  2. enhancer.call(id: 12).value!

And this is the result…

  1. {
  2. :id => 12,
  3. :user_id => 2,
  4. :user => {
  5. :email => "Shanna@melissa.tv",
  6. :name => "Ervin Howell",
  7. :email_info => { :deliverable => true, :free => true },
  8. :gravatar => "https://www.gravatar.com/avatar/241af7d19a0a7438794aef21e4e19b79"
  9. }
  10. }

You can even preview it as an SVG!

  1. enhancer.to_dot(preview: true) # requires graphviz (brew install graphviz)

Features

  • Offers DSL for defining workflows and merging the results from each task
  • Execution engine optimally parallelizes the execution of the workflow dependency graph using concurrent-ruby Promises
  • Very easy to unit test
  • Ability to filter out bad/junky data using dry-validation contracts

What are the main difference between this Ruby project and similar ones?

  • Operates on a single entity rather than a list, so easily adoptable in existing systems
  • Focuses on the “fetching” and filtering of data solely, and not transformation or storage
  • Focuses on having a clean PORO interface to make testing simple
  • Provides a declarative interface for merging results from many sources (APIs, legacy databases, etc.) which allows for prioritization

Examples

SVG of Workflow
Example of workflow that enhances a blog post

Installation

Add this line to your application’s Gemfile:

  1. gem 'attr-gather'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install attr-gather

Development

After checking out the repo, run bin/setup to install dependencies. Then, run
rake spec 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/ianks/attr-gather. This project is intended to be a safe,
welcoming space for collaboration, and contributors are expected to adhere to
the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the Attr::Gather project’s codebases, issue trackers,
chat rooms and mailing lists is expected to follow the code of
conduct
.