json_api_resource_adapters 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93572f345844bba1fb90020222f7d8350ed9671c
4
- data.tar.gz: f3c0e4570931243f88e4836d16a3acdaf2211c4c
3
+ metadata.gz: 7ce8914f0c409d7e0d93d7ab672bf12b648ed85a
4
+ data.tar.gz: c08ef916dc16f49df84a5699ca5cf8bf2966ffa7
5
5
  SHA512:
6
- metadata.gz: 0e0b2e0283c2e827aeb1580c5d9f930e2a11f4264f4eee63fc6eab646b1ddd71635f0d8379183a7ff02cf62a5afbef9db084cb8867eeac5a21e69ff54c203e33
7
- data.tar.gz: 1d7748ba94f508edbf5f21c6607c27f90bb13fbe661cbf4d4a04b5bba95f9172e0a976429b25a68c489bef1709ea393dfa44fd812624e35b6dac66aa04289661
6
+ metadata.gz: 3a489f9cd93b8334aada20a7cc3e50c230c92d7149a2c5247225e3c3266851ea227ba686eb428e242af5d500e9ff864749b59c253c9d3e66ade5a212ed09f454
7
+ data.tar.gz: b756bd8d9393f3303403652e405765f6d957385431fc8e546ed6ed12e76d3921035bef229ccd8bcfd4f3c7b8a8a63c054e7c0b3fc5ea49824748bd28f4d024b8
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_api_resource_adapters.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Greg
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,58 @@
1
+ # JsonApiResourceAdapters
2
+
3
+ A collection of handy adapters for projects tha use [JsonApiResource](http://github.com/avvo/json_api_resource)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'json_api_resource_adapters'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install json_api_resource_adapters
20
+
21
+ ## Usage
22
+
23
+ Include the gem and you will have access to
24
+
25
+ ### Autobots::JsonApiResourceAssembler
26
+
27
+ This is an adapter for [autobots](https://github.com/chingor13/autobots) that allows autobots to bulk fetch records for caching.
28
+
29
+ The setup is pretty simple:
30
+
31
+ ```ruby
32
+ module MyApi
33
+ module V1
34
+ class MyAssembler < Autobots::JsonApiResourceAssembler
35
+
36
+ self.cache = Rails.cache
37
+
38
+ end
39
+ end
40
+ end
41
+
42
+ ```
43
+
44
+ ### Elasticsearch adapter
45
+
46
+ This is an adapter for [elasticsearch-model](https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model)
47
+
48
+ There's nothing you have to do. It will autoload and let you call interacr with your resource class the same way you would with an ActiveRecord model
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gaorlov/json_api_resource_adapters.
53
+
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
58
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_api_resource_adapters"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_api_resource_adapters/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_api_resource_adapters"
8
+ spec.version = JsonApiResourceAdapters::VERSION
9
+ spec.authors = ["Greg"]
10
+ spec.email = ["greg@avvo.com"]
11
+
12
+ spec.summary = "Various adapters for JsonApiResource"
13
+ spec.description = "Adapters for JsonApiResource: Autobots preloading; Elasticsearch"
14
+ spec.homepage = "https://github.com/gaorlov/json_api_resource_adapters"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "json_api_resource"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ end
@@ -0,0 +1,11 @@
1
+ module Autobots
2
+ class JsonApiResourceAssembler < Assembler
3
+ class_attribute :preloads
4
+ self.preloads = []
5
+
6
+ def transform( objects )
7
+ JsonApiResource::Associations::Preloader.preload(objects, preloads)
8
+ objects
9
+ end
10
+ end
11
+ end
data/lib/autobots.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Autobots
2
+ autoload :JsonApiResourceAssembler, "autobots/json_api_resource_assembler"
3
+ end
@@ -0,0 +1,7 @@
1
+ module Elasticsearch
2
+ module Model
3
+ module Adapter
4
+ autoload :JsonApiResource, 'elasticsearch/model/adapter/json_api_resource'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,70 @@
1
+ module Elasticsearch
2
+ module Model
3
+ module Adapter
4
+
5
+ # An adapter for JsonApiResource-based models
6
+ #
7
+ module JsonApiResource
8
+
9
+ Adapter.register self,
10
+ lambda { |klass| !!defined?(::JsonApiResource::Resource) && klass.respond_to?(:ancestors) && klass.ancestors.include?(::JsonApiResource::Resource) }
11
+
12
+ module Records
13
+ attr_writer :options
14
+
15
+ def options
16
+ @options ||= {}
17
+ end
18
+
19
+ # Returns an array of `JsonApiResource::Resource`
20
+ #
21
+ def records
22
+ klass.where(id: ids, skip_pagination: true, order: "field(id, #{ids.join(", ")})")
23
+ end
24
+ end
25
+
26
+ module Callbacks
27
+
28
+ # Handle index updates (creating, updating or deleting documents)
29
+ # when the model changes, by hooking into the lifecycle
30
+ #
31
+ # @see http://guides.rubyonrails.org/active_record_callbacks.html
32
+ #
33
+ def self.included(base)
34
+ base.class_eval do
35
+ after_commit lambda { __elasticsearch__.index_document }, on: :create
36
+ after_commit lambda { __elasticsearch__.update_document }, on: :update
37
+ after_commit lambda { __elasticsearch__.delete_document }, on: :destroy
38
+ end
39
+ end
40
+ end
41
+
42
+ module Importing
43
+
44
+ # Fetch batches of records from the database (used by the import method)
45
+ #
46
+ #
47
+ # @see http://api.rubyonrails.org/classes/AcriveRecord/Batches.html AcriveRecord::Batches.find_in_batches
48
+ #
49
+ def __find_in_batches(options={}, &block)
50
+ page = 1
51
+ loop do
52
+ objects = where page: page, per_page: 100
53
+
54
+ yield objects
55
+
56
+ page +=1
57
+
58
+ break if page >= objects.meta[:total_pages]
59
+
60
+ end
61
+ end
62
+
63
+ def __transform
64
+ lambda { |model| { index: { _id: model.id, data: model.__elasticsearch__.as_indexed_json } } }
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ module Elasticsearch
2
+ module Model
3
+ autoload :Adapter, 'elasticsearch/model/adapter'
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Elasticsearch
2
+ autoload :Model, 'elasticsearch/model'
3
+ end
@@ -0,0 +1,3 @@
1
+ module JsonApiResourceAdapters
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,15 @@
1
+ require "json_api_resource_adapters/version"
2
+
3
+ puts "loading adapters"
4
+ if defined? Autobots
5
+ # bluk loader adapter for the autobots assembler
6
+ autoload :Autobots, "autobots"
7
+ end
8
+
9
+ if defined? Elasticsearch
10
+ # import and result retreival adapter for elasticsearch
11
+ autoload :Elasticsearch, "elasticsearch"
12
+ end
13
+
14
+ module JsonApiResourceAdapters
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_resource_adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg
@@ -10,6 +10,20 @@ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2017-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json_api_resource
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,7 +72,24 @@ email:
58
72
  executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
- files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - json_api_resource_adapters.gemspec
85
+ - lib/autobots.rb
86
+ - lib/autobots/json_api_resource_assembler.rb
87
+ - lib/elasticsearch.rb
88
+ - lib/elasticsearch/model/adapter/adapter.rb
89
+ - lib/elasticsearch/model/adapter/json_api_resource.rb
90
+ - lib/elasticsearch/model/model.rb
91
+ - lib/json_api_resource_adapters.rb
92
+ - lib/json_api_resource_adapters/version.rb
62
93
  homepage: https://github.com/gaorlov/json_api_resource_adapters
63
94
  licenses:
64
95
  - MIT