shydra 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shydra.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "guard-rspec"
8
+ end
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # More info at https://github.com/guard/guard#readme
2
+
3
+ guard :rspec, cli: "--color", bundler: false, all_on_start: true do
4
+ watch(%r{^spec/.+_spec\.rb$})
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
6
+ watch('spec/spec_helper.rb') { "spec" }
7
+ end
8
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Michael Johnston
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Shydra
2
+
3
+ A fast, parallel shopify api client using typhoeus/hydra. Attempts to balance being a good citizen about api limits and fetching multiple apai records in parallel.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'shydra'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install shydra
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/lib/shydra.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "shydra/version"
2
+ require "typhoeus"
3
+ require "shopify_api"
4
+ require 'shydra/request'
5
+ require 'shydra/response'
6
+
7
+ module Shydra
8
+ class << self
9
+ attr_accessor :max_concurrency
10
+ end
11
+ end
12
+
13
+ Shydra.max_concurrency = 20
@@ -0,0 +1,62 @@
1
+ =begin
2
+
3
+ Scenarios:
4
+
5
+ 1. request that might return more than 250
6
+ - limit: :none
7
+ - adds callback that fetches another page if result size == SHOPIFY_API_MAX_LIMIT
8
+
9
+ 2. request that will return several pages that needs to be fast
10
+ - use a hydra
11
+ - does count first
12
+ - queues request for each page, callback inserts into correct slot in result_array
13
+ - if too many pages for api limit, fetch as many pages as possible, then sleep for some amount of time
14
+ - time to sleep: requests-used-before-starting / API-LIMIT
15
+ - at the end flatten the result_array
16
+
17
+
18
+ API LIMIT
19
+ - memcached
20
+ - cache {used: x, at: Time.now}
21
+ - before starting hydra:
22
+ - count requests
23
+ - check cache
24
+ - if at < TUNEABLE_PARAM (like, 20 seconds) & enough requests (including TUNEABLE_BUFFER)
25
+ - run everything
26
+ - do_run: run 1 request. update cache
27
+ - if enough requests, run everything
28
+ - else bail or sleep(time allowed)
29
+ - after sleeping time allowed, goto: do_run
30
+ - because requests may have callbacks that queue other requests, the run_loop has to go through this alogrithm every time it pulls a new batch of requests off
31
+ =end
32
+ module Shydra
33
+ class Hydra < Typhoeus::Hydra
34
+
35
+
36
+ def initialize(options = {})
37
+ options[:max_concurrency] ||= Shydra.max_concurrency
38
+ super(options)
39
+ end
40
+
41
+
42
+
43
+ def pause!
44
+ @paused = true
45
+ end
46
+
47
+ def paused?
48
+ @paused
49
+ end
50
+
51
+ def run
52
+ @paused = false
53
+ super
54
+ end
55
+
56
+ def dequeue
57
+ return if paused?
58
+ super
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,64 @@
1
+ require_relative 'response'
2
+
3
+ module Shydra
4
+ class Request < Typhoeus::Request
5
+ SHOPIFY_API_MAX_LIMIT = 250
6
+
7
+ attr_accessor :resource, :data_root
8
+ attr_accessor :options
9
+
10
+ def self.base_uri
11
+ ShopifyAPI::Base.site
12
+ end
13
+
14
+ def initialize(*args)
15
+ @options = args.extract_options!
16
+ @count = @options.delete(:count)
17
+ if args[1] == :count
18
+ @count = true
19
+ end
20
+
21
+ @resource = @options.delete(:resource)
22
+ @resource ||= args.first
23
+ @resource ||= 'shop'
24
+ @resource = @resource.to_s
25
+
26
+ @id = @options.delete(:id)
27
+
28
+ @options[:limit] ||= SHOPIFY_API_MAX_LIMIT
29
+
30
+ resource_path = @resource
31
+ resource_path = resource_path.pluralize unless (@resource == 'shop')
32
+
33
+
34
+ path = [resource_path]
35
+ path.unshift('admin') unless Request.base_uri.path[-1] == '/' #handle quirk of URI.merge
36
+
37
+ @data_root = @resource
38
+ if @count
39
+ path << 'count'
40
+ @options.delete(:limit)
41
+ @data_root = 'count'
42
+ elsif @id
43
+ path << @id.to_s
44
+ @data_root = @resource
45
+ else
46
+ @data_root = @resource.pluralize
47
+ end
48
+
49
+ path = path.join('/') + '.json'
50
+ uri = Request.base_uri.merge(path)
51
+ uri.query = @options.to_param unless @options.empty?
52
+
53
+ super(uri.to_s)
54
+ end
55
+
56
+ def finish(response, bypass_memoization = nil)
57
+ response.extend Shydra::Response
58
+ super(response, bypass_memoization)
59
+ end
60
+
61
+ end
62
+ end
63
+
64
+
@@ -0,0 +1,15 @@
1
+ require 'oj'
2
+ module Shydra
3
+ module Response
4
+ def data
5
+ json = Oj.load(response_body)
6
+ case response_code
7
+ when 200
8
+ json[request.data_root]
9
+ else
10
+ json
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,3 @@
1
+ module Shydra
2
+ VERSION = "0.0.1"
3
+ end
data/shydra.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shydra/version'
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "shydra"
7
+ spec.version = Shydra::VERSION
8
+ spec.authors = ["Michael Johnston"]
9
+ spec.email = ["lastobelus@mac.com"]
10
+ spec.description = %q{A fast, parallel shopify api client using typhoeus/hydra. Attempts to balance being a good citizen about api limits and fetching multiple apai records in parallel. }
11
+ spec.summary = %q{A fast, parallel shopify api client using typhoeus/hydraa}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+
21
+ spec.add_runtime_dependency "typhoeus", ">= 0.6.3"
22
+ spec.add_runtime_dependency "shopify_api", ">= 3.0.3"
23
+ spec.add_runtime_dependency "oj"
24
+ spec.add_development_dependency "bundler", ">= 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Hydra" do
4
+ it "needs to have tests written"
5
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ require 'shydra/request'
4
+
5
+
6
+ describe "Shydra::Request" do
7
+ before do
8
+ ShopifyAPI::Base.stub(:site).and_return(URI("https://xxxx:yyyy@cronin.myshopify.com/admin/"))
9
+ end
10
+
11
+ describe "URIs" do
12
+ subject{ Shydra::Request.new() }
13
+ let(:store_uri){ URI("https://xxxx:yyyy@cronin.myshopify.com/admin/")}
14
+
15
+ specify{ expect(subject).to be_a_kind_of(Typhoeus::Request)}
16
+
17
+ it "gets the base url from Shopify API" do
18
+ expect(Shydra::Request.base_uri).to eq(store_uri)
19
+ end
20
+
21
+ it "creates resource collection uri" do
22
+ expect(Shydra::Request.new(:product).url).to eq(
23
+ store_uri.to_s + "products.json?limit=250")
24
+ end
25
+
26
+ it "creates resource collection uri with params" do
27
+ expect(Shydra::Request.new(:product, collection_id: 789, vendor: 'bob').url).to eq(
28
+ store_uri.to_s + "products.json?collection_id=789&limit=250&vendor=bob")
29
+ end
30
+
31
+ it "sets the resource to shop by default" do
32
+ expect(Shydra::Request.new.url).to eq(
33
+ store_uri.to_s + "shop.json?limit=250")
34
+ end
35
+
36
+ it "creates resource by id uri" do
37
+ expect(Shydra::Request.new(:product, id: 12345).url).to eq(
38
+ store_uri.to_s + "products/12345.json?limit=250")
39
+ end
40
+
41
+ it "creates resource count uri" do
42
+ expect(Shydra::Request.new(:product, :count).url).to eq(
43
+ store_uri.to_s + "products/count.json")
44
+ end
45
+
46
+ it "creates resource count uri with params" do
47
+ expect(Shydra::Request.new(:product, :count, collection_id: 789, vendor: 'bob').url).to eq(
48
+ store_uri.to_s + "products/count.json?collection_id=789&vendor=bob")
49
+ end
50
+ end
51
+
52
+ describe 'responses' do
53
+ describe '#data' do
54
+ it "returns the count as an int for a count request" do
55
+ response = Typhoeus::Response.new(code: 200, body: "{\"count\":14}")
56
+ Typhoeus.stub(/cronin/).and_return(response)
57
+ response = Shydra::Request.new(:product, :count).run
58
+
59
+ expect(response.data).to eq 14
60
+ end
61
+
62
+ it "returns a hash of product attributes for an id request" do
63
+ response = Typhoeus::Response.new(code: 200, body: "{\"product\":{\"body_html\":\"yo yo yo baby pop\",\"id\":137632345}}")
64
+ Typhoeus.stub(/cronin/).and_return(response)
65
+ response = Shydra::Request.new(:product, id: 137632345).run
66
+
67
+ expect(response.data).to eq( {"body_html"=>"yo yo yo baby pop", "id"=>137632345})
68
+ end
69
+
70
+ it "returns an array of hashes of product attributes for an collection request" do
71
+ response = Typhoeus::Response.new(code: 200, body: "{\"products\":[{\"body_html\":\"yo yo yo baby pop\",\"id\":123},{\"body_html\":\"yowza\",\"id\":456}]}")
72
+ Typhoeus.stub(/cronin/).and_return(response)
73
+ response = Shydra::Request.new(:product).run
74
+
75
+ expect(response.data).to eq( [{"body_html"=>"yo yo yo baby pop", "id"=>123}, {"body_html"=>"yowza", "id"=>456}] )
76
+ end
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Response" do
4
+ it "needs to have tests written"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Shydra" do
4
+ it "needs to have tests written"
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'shydra'
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shydra
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Johnston
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: typhoeus
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: shopify_api
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.3
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: oj
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '1.3'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ! 'A fast, parallel shopify api client using typhoeus/hydra. Attempts
111
+ to balance being a good citizen about api limits and fetching multiple apai records
112
+ in parallel. '
113
+ email:
114
+ - lastobelus@mac.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - Guardfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/shydra.rb
126
+ - lib/shydra/hydra.rb
127
+ - lib/shydra/request.rb
128
+ - lib/shydra/response.rb
129
+ - lib/shydra/version.rb
130
+ - shydra.gemspec
131
+ - spec/shydra/hydra_spec.rb
132
+ - spec/shydra/request_spec.rb
133
+ - spec/shydra/response_spec.rb
134
+ - spec/shydra_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: ''
137
+ licenses:
138
+ - MIT
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ segments:
150
+ - 0
151
+ hash: 1355709900760715495
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ segments:
159
+ - 0
160
+ hash: 1355709900760715495
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 1.8.25
164
+ signing_key:
165
+ specification_version: 3
166
+ summary: A fast, parallel shopify api client using typhoeus/hydraa
167
+ test_files:
168
+ - spec/shydra/hydra_spec.rb
169
+ - spec/shydra/request_spec.rb
170
+ - spec/shydra/response_spec.rb
171
+ - spec/shydra_spec.rb
172
+ - spec/spec_helper.rb