autobots 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +15 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +142 -0
  7. data/Rakefile +13 -0
  8. data/autobots.gemspec +28 -0
  9. data/lib/autobots.rb +9 -0
  10. data/lib/autobots/assembler.rb +35 -0
  11. data/lib/autobots/helpers.rb +6 -0
  12. data/lib/autobots/helpers/active_record_preloading.rb +18 -0
  13. data/lib/autobots/helpers/caching.rb +38 -0
  14. data/lib/autobots/version.rb +3 -0
  15. data/test/active_record_test.rb +66 -0
  16. data/test/basic_test.rb +22 -0
  17. data/test/caching_test.rb +27 -0
  18. data/test/dummy/README.rdoc +261 -0
  19. data/test/dummy/Rakefile +7 -0
  20. data/test/dummy/app/assets/javascripts/application.js +15 -0
  21. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  22. data/test/dummy/app/controllers/application_controller.rb +3 -0
  23. data/test/dummy/app/helpers/application_helper.rb +2 -0
  24. data/test/dummy/app/mailers/.gitkeep +0 -0
  25. data/test/dummy/app/models/.gitkeep +0 -0
  26. data/test/dummy/app/models/comment.rb +5 -0
  27. data/test/dummy/app/models/issue.rb +6 -0
  28. data/test/dummy/app/models/project.rb +5 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/test/dummy/config.ru +4 -0
  31. data/test/dummy/config/application.rb +53 -0
  32. data/test/dummy/config/boot.rb +10 -0
  33. data/test/dummy/config/database.yml +25 -0
  34. data/test/dummy/config/environment.rb +5 -0
  35. data/test/dummy/config/environments/development.rb +33 -0
  36. data/test/dummy/config/environments/production.rb +69 -0
  37. data/test/dummy/config/environments/test.rb +33 -0
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/test/dummy/config/initializers/inflections.rb +15 -0
  40. data/test/dummy/config/initializers/mime_types.rb +5 -0
  41. data/test/dummy/config/initializers/secret_token.rb +7 -0
  42. data/test/dummy/config/initializers/session_store.rb +8 -0
  43. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  44. data/test/dummy/config/locales/en.yml +5 -0
  45. data/test/dummy/config/routes.rb +58 -0
  46. data/test/dummy/db/migrate/20141119222556_schema.rb +19 -0
  47. data/test/dummy/lib/assets/.gitkeep +0 -0
  48. data/test/dummy/log/.gitkeep +0 -0
  49. data/test/dummy/public/404.html +26 -0
  50. data/test/dummy/public/422.html +26 -0
  51. data/test/dummy/public/500.html +25 -0
  52. data/test/dummy/public/favicon.ico +0 -0
  53. data/test/dummy/script/rails +6 -0
  54. data/test/dummy/test/fixtures/comments.yml +14 -0
  55. data/test/dummy/test/fixtures/issues.yml +9 -0
  56. data/test/dummy/test/fixtures/projects.yml +5 -0
  57. data/test/test_helper.rb +44 -0
  58. data/test/test_models.rb +73 -0
  59. metadata +232 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f3045074e374f37719b3922bfea5023f73fa5c0
4
+ data.tar.gz: 19c06ac4b4e923aee25392adcc4bfb5577f4b183
5
+ SHA512:
6
+ metadata.gz: 290662e0a21676241bca53b76cc4c966f9733d095291a5c167c78ff4f2b8e7b483a8d1908ad93d352571fba8ac6fbe2569733e1938330be00bd3f26b477ffb8d
7
+ data.tar.gz: 13c683363bd0f4230e447440e38168c9dd0497e0e15a68d725c6e4e907c22bdc16352159b09113a38c0b0626306e9c134723b9a5e5e6555dfa399a05b4a5ae5f
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.log
15
+ mkmf.log
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ env:
6
+ matrix:
7
+ - "RAILS_VERSION=4.0.0"
8
+ - "RAILS_VERSION=4.1.0"
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autobots.gemspec
4
+ gemspec
5
+
6
+ rails_version = ENV["RAILS_VERSION"] || "default"
7
+
8
+ rails = case rails_version
9
+ when "default"
10
+ ">= 3.2.0"
11
+ else
12
+ "~> #{rails_version}"
13
+ end
14
+
15
+ gem "rails", rails
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jeff Ching
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.
@@ -0,0 +1,142 @@
1
+ # Autobots [![Build Status](https://travis-ci.org/chingor13/autobots.png)](https://travis-ci.org/chingor13/autobots)
2
+
3
+ Loading and serializing models in bulk with caching.
4
+
5
+ Separate the loading/serialization of resources from their protocol (http/json, avro, xml).
6
+
7
+ ## Motivation
8
+
9
+ We want to improve api response time by loading the minimum amount of data needed to check cache keys. If we miss, we want to optimally load our data, serialize it, cache it and then return it.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'autobots'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install autobots
26
+
27
+ ## Usage
28
+
29
+ When trying to render a resource for an API, we have 3 parts:
30
+
31
+ 1. Fetching data
32
+ 2. Figuring out the data to return
33
+ 3. Format it for the protocol
34
+
35
+ ### Fetching data
36
+
37
+ `autobots` uses `Autobots::Assembler` classes to fetch data from given identifiers. The assembler's job is to fetch all data needed for serialization as optimally as possible. The identifiers should be the minimum amount of data needed to fetch data and determine caching.
38
+
39
+ ```ruby
40
+
41
+ class ProjectAssembler < Autobots::Assembler
42
+
43
+ # assembles the base objects needed for cache key generation
44
+ def assemble(identifiers)
45
+ Project.where(id: identifiers).to_a
46
+ end
47
+
48
+ end
49
+
50
+
51
+ project_ids = [1,2,3]
52
+ assembler = ProjectAssembler.new(project_ids)
53
+
54
+ # returns preloaded objects for serialization
55
+ resources = assembler.resources
56
+
57
+ ```
58
+
59
+ ### Figuring out the data to return
60
+
61
+ We use the `active_model_serializers` gem to accomplish serialization. An assembler declares the type of serializer used to specify the data returned
62
+
63
+ ```ruby
64
+
65
+ class ProjectAssembler < Autobots::Assembler
66
+ self.serializer = ProjectSerializer # an ActiveModel::Serializer
67
+ end
68
+
69
+ ```
70
+
71
+ ### Formatting the response
72
+
73
+ `autobots` gem only handles the loading and representation of the data.
74
+
75
+ ### Caching
76
+
77
+ We can get large performance boosts by caching our serializable data. Caching is straight forward:
78
+
79
+ ```ruby
80
+
81
+ # some sort of ActiveSupport::CacheStore
82
+ cache = Rails.cache
83
+ assembler = ProjectAssembler.new(project_ids, cache: cache)
84
+
85
+ ```
86
+
87
+ The cache store must implement `read_multi` as we use [`bulk_cache_fetcher` gem](https://github.com/justinweiss/bulk_cache_fetcher/)
88
+
89
+ By default, we use each resource's `cache_key` implementation. You can provide your own cache key generator by passing in a cache_key proc:
90
+
91
+ ```ruby
92
+
93
+ assembler = ProjectAssembler.new(project_ids, {
94
+ cache: cache,
95
+ cache_key: -> (obj, assembler) {
96
+ "#{obj.cache_key}-foo"
97
+ }
98
+ })
99
+
100
+ ```
101
+
102
+ ### Minimizing fetch requests
103
+
104
+ The strength of this model lies in minimizing data fetch requests when building our data to serialize. We do this by delaying the loading of nested models and only load them if we have to.
105
+
106
+ If a resources's cache is up to date, we shouldn't have to fetch it's dependencies from the database. However, if we have a cache miss, we want to optimally load the data needed.
107
+
108
+ ```ruby
109
+
110
+ class ProjectAssembler < Autobots::Assembler
111
+
112
+ # for any missing resources, preload the nested attributes
113
+ def transform(resources)
114
+ ActiveRecord::Associations::Preloader.new.preload(resources, {issues: :comments})
115
+ end
116
+
117
+ end
118
+
119
+ ```
120
+
121
+ The preloading step can be customized (load from an external service or whatever you want). We provide a helpful mixin if you're using ActiveRecord and you want preloading:
122
+
123
+ ```ruby
124
+
125
+ class ProjectAssembler < Autobots::Assembler
126
+ include Autobots::Helpers::ActiveRecordPreloading
127
+
128
+ # in this case, project has_many issues which has_many comments
129
+ def preloads
130
+ {issues: :comments}
131
+ end
132
+ end
133
+
134
+ ```
135
+
136
+ ## Contributing
137
+
138
+ 1. Fork it ( https://github.com/chingor13/autobots/fork )
139
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
140
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
141
+ 4. Push to the branch (`git push origin my-new-feature`)
142
+ 5. Create a new Pull Request
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+
13
+ task default: :test
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'autobots/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "autobots"
8
+ spec.version = Autobots::VERSION
9
+ spec.authors = ["Jeff Ching"]
10
+ spec.email = ["jching@avvo.com"]
11
+ spec.summary = "Loading and serializing models"
12
+ spec.description = "Loading and serializing models in bulk with caching"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "bulk_cache_fetcher", '~> 0.0.3'
22
+ spec.add_dependency "activesupport", '~> 4.0'
23
+ spec.add_dependency "active_model_serializers", '~> 0.8.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "sqlite3"
28
+ end
@@ -0,0 +1,9 @@
1
+ require 'autobots/version'
2
+ require 'active_support/all'
3
+ require 'active_model_serializers'
4
+ require 'bulk_cache_fetcher'
5
+
6
+ module Autobots
7
+ autoload :Assembler, 'autobots/assembler'
8
+ autoload :Helpers, 'autobots/helpers'
9
+ end
@@ -0,0 +1,35 @@
1
+ module Autobots
2
+ class Assembler
3
+ class_attribute :serializer
4
+ attr_reader :identifiers, :options, :objects
5
+
6
+ def initialize(identifiers, options = {})
7
+ @identifiers, @options = identifiers, options
8
+ @objects = assemble(identifiers)
9
+ end
10
+
11
+ def data
12
+ roll_out(objects)
13
+ end
14
+
15
+ private
16
+
17
+ # builds our skeleton resources
18
+ def assemble(identifiers)
19
+ identifiers
20
+ end
21
+
22
+ # fetch any additional objects and modify our resources
23
+ def transform(resources)
24
+ resources
25
+ end
26
+
27
+ # serialize everything
28
+ def roll_out(objects)
29
+ transform(objects).map{|obj| serializer.new(obj).serializable_hash }
30
+ end
31
+
32
+ prepend Helpers::Caching
33
+
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ module Autobots
2
+ module Helpers
3
+ autoload :ActiveRecordPreloading, 'autobots/helpers/active_record_preloading'
4
+ autoload :Caching, 'autobots/helpers/caching'
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ module Autobots
2
+ module Helpers
3
+ module ActiveRecordPreloading
4
+
5
+ def transform(objects)
6
+ ActiveRecord::Associations::Preloader.new(objects, preloads).run
7
+ objects
8
+ rescue ArgumentError
9
+ ActiveRecord::Associations::Preloader.new.preload(objects, preloads)
10
+ objects
11
+ end
12
+
13
+ def preloads
14
+ []
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ module Autobots
2
+ module Helpers
3
+ module Caching
4
+ def initialize(_, options = {})
5
+ super
6
+ @cache = options[:cache]
7
+ end
8
+
9
+ def data
10
+ return @data if defined?(@data)
11
+
12
+ if @cache
13
+ key_proc = options.fetch(:cache_key) do
14
+ method(:cache_key)
15
+ end
16
+ identifiers = objects.inject({}) do |acc, obj|
17
+ acc[key_proc.call(obj, self)] = obj
18
+ acc
19
+ end
20
+
21
+ # misses: { key => obj }
22
+ @data = BulkCacheFetcher.new(@cache).fetch(identifiers) do |misses|
23
+ roll_out(misses.values)
24
+ end
25
+ else
26
+ @data = super
27
+ end
28
+ @data
29
+ end
30
+
31
+ protected
32
+
33
+ def cache_key(object, _)
34
+ [object.cache_key, serializer.name, 'serializable-hash']
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Autobots
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveRecordTest < ActiveSupport::TestCase
4
+ fixtures :all
5
+
6
+ def test_basic
7
+ projects = Project.all
8
+ assembler = ProjectAssembler.new(projects)
9
+
10
+ data = nil
11
+ assert_queries 5 do
12
+ data = assembler.data
13
+ end
14
+
15
+ assert data.is_a?(Array)
16
+ data.each do |item|
17
+ assert item.is_a?(Hash)
18
+ end
19
+ end
20
+
21
+ def test_can_preload
22
+ projects = Project.all
23
+ assembler = ProjectPreloadAssembler.new(projects)
24
+
25
+ data = nil
26
+ assert_queries 3 do
27
+ data = assembler.data
28
+ end
29
+
30
+ assert data.is_a?(Array)
31
+ data.each do |item|
32
+ assert item.is_a?(Hash)
33
+ end
34
+ end
35
+
36
+ def test_can_preload_with_mixin
37
+ projects = Project.all
38
+ assembler = ProjectPreloadIncludedAssembler.new(projects)
39
+
40
+ data = nil
41
+ assert_queries 3 do
42
+ data = assembler.data
43
+ end
44
+
45
+ assert data.is_a?(Array)
46
+ data.each do |item|
47
+ assert item.is_a?(Hash)
48
+ end
49
+ end
50
+
51
+ def test_can_use_ids
52
+ project_ids = Project.all.pluck(:id)
53
+ assembler = ProjectIdAssembler.new(project_ids)
54
+
55
+ data = nil
56
+ assert_queries 4 do
57
+ data = assembler.data
58
+ end
59
+
60
+ assert data.is_a?(Array)
61
+ data.each do |item|
62
+ assert item.is_a?(Hash)
63
+ end
64
+ end
65
+
66
+ end