shydra 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/lib/shydra.rb CHANGED
@@ -3,6 +3,10 @@ require "typhoeus"
3
3
  require "shopify_api"
4
4
  require 'shydra/request'
5
5
  require 'shydra/response'
6
+ require 'shydra/resource'
7
+ require 'shydra/resources/product'
8
+ require 'shydra/resources/metafield'
9
+ require 'shydra/resources/variant'
6
10
 
7
11
  module Shydra
8
12
  class << self
@@ -11,6 +11,10 @@ module Shydra
11
11
  ShopifyAPI::Base.site
12
12
  end
13
13
 
14
+ def self.headers
15
+ ShopifyAPI::Base.headers
16
+ end
17
+
14
18
  def initialize(*args)
15
19
  @options = args.extract_options!
16
20
  @count = @options.delete(:count)
@@ -23,18 +27,24 @@ module Shydra
23
27
  @resource ||= 'shop'
24
28
  @resource = @resource.to_s
25
29
 
30
+ @parent_resource = @options.delete(:parent_resource)
31
+ @parent_resource, @parent_resource_id = @parent_resource.first if @parent_resource
32
+
33
+
26
34
  @id = @options.delete(:id)
27
35
 
28
36
  @options[:limit] ||= SHOPIFY_API_MAX_LIMIT
29
37
 
30
38
  resource_path = @resource
31
39
  resource_path = resource_path.pluralize unless (@resource == 'shop')
40
+ resource_path = [@parent_resource.to_s.pluralize, @parent_resource_id, resource_path].join('/') if @parent_resource
32
41
 
33
42
 
34
43
  path = [resource_path]
35
44
  path.unshift('admin') unless Request.base_uri.path[-1] == '/' #handle quirk of URI.merge
36
45
 
37
- @data_root = @resource
46
+ @data_root = nil
47
+
38
48
  if @count
39
49
  path << 'count'
40
50
  @options.delete(:limit)
@@ -50,7 +60,7 @@ module Shydra
50
60
  uri = Request.base_uri.merge(path)
51
61
  uri.query = @options.to_param unless @options.empty?
52
62
 
53
- super(uri.to_s)
63
+ super(uri.to_s, headers: Request.headers)
54
64
  end
55
65
 
56
66
  def finish(response, bypass_memoization = nil)
@@ -0,0 +1,71 @@
1
+ module Shydra
2
+ class Resource < Request
3
+
4
+ class << self
5
+ attr_reader :parents
6
+
7
+ def has_parents(*args)
8
+ @parents = args.map(&:to_sym)
9
+ end
10
+
11
+ def parent_keys
12
+ @parent_keys ||= parents.map{|p| [p, "#{p.to_s}_id".to_sym]}.flatten.unshift(:parent_resource)
13
+ end
14
+ end
15
+
16
+
17
+ =begin
18
+ parents can be specified in the following ways:
19
+
20
+ --> assume the parent is :product
21
+ product: 1234
22
+ product_id: 1234
23
+ product: {id: 1234, blah}
24
+ product: ShopifyAPI::Product.find(1234) #ie, a shopify object
25
+ parent_resource: {:product => 1234}
26
+ parent_resource: ShopifyAPI::Product.find(1234)
27
+ =end
28
+ def initialize(*args)
29
+ # the class name is the resource
30
+ args.unshift(self.class.name.demodulize.underscore)
31
+
32
+ # if we can be a child resource
33
+ unless self.class.parents.nil? || self.class.parents.empty?
34
+ options = args.extract_options!
35
+
36
+ # find which parent we have.
37
+ options.symbolize_keys!
38
+ parent = options.keys.select{|k| self.class.parent_keys.include?(k)}
39
+ raise "#{self.class.name} cannot have multiple parents: #{parent.inspect}" if parent.length > 1
40
+
41
+ unless parent.empty?
42
+ parent_id = options.delete(parent.first)
43
+
44
+ # user can use parent_resource when parent type can vary at runtime
45
+ if parent.first == :parent_resource
46
+ # parent_resource: {product: 123}
47
+ if parent_id.is_a?(Hash)
48
+ raise "#{self.class.name} cannot have multiple parents: #{parent_id.inspect}" if parent_id.length > 1
49
+ parent_key = parent_id.first.first
50
+ parent_id = parent_id.first.last
51
+ elsif parent_id.is_a?(ActiveResource::Base)
52
+ # parent_resource: <ShopifyAPI::Product instance>
53
+ parent_key = parent_id.class.collection_name.to_sym
54
+ parent_element = parent_id.class.element_name.to_sym
55
+ raise "#{parent_id.inspect} cannot be a parent of #{self.class.name}" unless self.class.parents.include?(parent_element)
56
+ parent_id = parent_id.id
57
+ end
58
+ else
59
+ parent_key = parent.first.to_s.sub(/_id$/,'').to_sym
60
+ parent_id = parent_id[:id] if parent_id.is_a?(Hash)
61
+ parent_id = parent_id.id if parent_id.respond_to?(:id)
62
+ end
63
+ options.merge!({:parent_resource => {parent_key => parent_id}})
64
+
65
+ end
66
+ args << options
67
+ end
68
+ super(*args)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ module Shydra
2
+ module Resources
3
+ class Metafield < Shydra::Resource
4
+ has_parents :product, :variant, :blog, :custom_collection, :order, :page, :customer
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module Shydra
2
+ module Resources
3
+ class Product < Shydra::Request
4
+ def initialize(*args)
5
+ args.unshift(:product)
6
+ super(*args)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Shydra
2
+ module Resources
3
+ class Variant < Shydra::Resource
4
+ has_parents :product
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Shydra
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -5,7 +5,9 @@ require 'shydra/request'
5
5
 
6
6
  describe "Shydra::Request" do
7
7
  before do
8
+ @shopify_headers = {"X-Shopify-Access-Token"=>"yyyyyyyyy"}
8
9
  ShopifyAPI::Base.stub(:site).and_return(URI("https://xxxx:yyyy@cronin.myshopify.com/admin/"))
10
+ ShopifyAPI::Base.stub(:headers).and_return(@shopify_headers)
9
11
  end
10
12
 
11
13
  describe "URIs" do
@@ -18,6 +20,10 @@ describe "Shydra::Request" do
18
20
  expect(Shydra::Request.base_uri).to eq(store_uri)
19
21
  end
20
22
 
23
+ it "gets headers from Shopify API" do
24
+ expect(Shydra::Request.new(:product).options[:headers]["X-Shopify-Access-Token"]).to eq("yyyyyyyyy")
25
+ end
26
+
21
27
  it "creates resource collection uri" do
22
28
  expect(Shydra::Request.new(:product).url).to eq(
23
29
  store_uri.to_s + "products.json?limit=250")
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Shydra::Resource" do
4
+ it "needs to have tests written"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Shydra::Resources::Metafield" do
4
+ it "needs to have tests written"
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shydra::Resources::Product do
4
+ before do
5
+ ShopifyAPI::Base.stub(:site).and_return(URI("https://xxxx:yyyy@cronin.myshopify.com/admin/"))
6
+ end
7
+
8
+ let(:store_uri){ URI("https://xxxx:yyyy@cronin.myshopify.com/admin/")}
9
+
10
+ it "creates a product request" do
11
+ expect(Shydra::Resources::Product.new(:product, id: 12345).url).to eq(
12
+ store_uri.to_s + "products/12345.json?limit=250")
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shydra::Resources::Variant do
4
+ before do
5
+ ShopifyAPI::Base.stub(:site).and_return(URI("https://xxxx:yyyy@cronin.myshopify.com/admin/"))
6
+ end
7
+
8
+ let(:store_uri){ URI("https://xxxx:yyyy@cronin.myshopify.com/admin/")}
9
+
10
+ it "creates a variant request" do
11
+ expect(Shydra::Resources::Variant.new(id: 12345).url).to eq(
12
+ store_uri.to_s + "variants/12345.json?limit=250")
13
+ end
14
+
15
+
16
+ it "creates a product variant request" do
17
+ expect(Shydra::Resources::Variant.new(product_id: 7777).url).to eq(
18
+ store_uri.to_s + "products/7777/variants.json?limit=250")
19
+ expect(Shydra::Resources::Variant.new(product: 7777).url).to eq(
20
+ store_uri.to_s + "products/7777/variants.json?limit=250")
21
+ expect(Shydra::Resources::Variant.new(product: {id: 7777}).url).to eq(
22
+ store_uri.to_s + "products/7777/variants.json?limit=250")
23
+ end
24
+
25
+ it "creates a product variant request from a shopify object" do
26
+ product = ShopifyAPI::Product.new(id: 888)
27
+ expect(Shydra::Resources::Variant.new(product: product).url).to eq(
28
+ store_uri.to_s + "products/888/variants.json?limit=250")
29
+ end
30
+
31
+ it "creates a variant request from a parent_resource" do
32
+ # expect(Shydra::Resources::Variant.new(parent_resource: {product: 7777}).url).to eq(
33
+ # store_uri.to_s + "products/7777/variants.json?limit=250")
34
+
35
+ product = ShopifyAPI::Product.new(id: 888)
36
+ expect(Shydra::Resources::Variant.new(parent_resource: product).url).to eq(
37
+ store_uri.to_s + "products/888/variants.json?limit=250")
38
+
39
+ metafield = ShopifyAPI::Metafield.new(id: 444)
40
+ expect{Shydra::Resources::Variant.new(parent_resource: metafield)}.to raise_error(
41
+ /cannot be a parent of/
42
+ )
43
+ end
44
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shydra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-06 00:00:00.000000000 Z
12
+ date: 2013-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
@@ -117,6 +117,7 @@ extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
119
  - .gitignore
120
+ - .rspec
120
121
  - Gemfile
121
122
  - Guardfile
122
123
  - LICENSE.txt
@@ -125,11 +126,19 @@ files:
125
126
  - lib/shydra.rb
126
127
  - lib/shydra/hydra.rb
127
128
  - lib/shydra/request.rb
129
+ - lib/shydra/resource.rb
130
+ - lib/shydra/resources/metafield.rb
131
+ - lib/shydra/resources/product.rb
132
+ - lib/shydra/resources/variant.rb
128
133
  - lib/shydra/response.rb
129
134
  - lib/shydra/version.rb
130
135
  - shydra.gemspec
131
136
  - spec/shydra/hydra_spec.rb
132
137
  - spec/shydra/request_spec.rb
138
+ - spec/shydra/resource_spec.rb
139
+ - spec/shydra/resources/metafield_spec.rb
140
+ - spec/shydra/resources/product_spec.rb
141
+ - spec/shydra/resources/variant_spec.rb
133
142
  - spec/shydra/response_spec.rb
134
143
  - spec/shydra_spec.rb
135
144
  - spec/spec_helper.rb
@@ -148,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
157
  version: '0'
149
158
  segments:
150
159
  - 0
151
- hash: 1355709900760715495
160
+ hash: -2036902327160108675
152
161
  required_rubygems_version: !ruby/object:Gem::Requirement
153
162
  none: false
154
163
  requirements:
@@ -157,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
166
  version: '0'
158
167
  segments:
159
168
  - 0
160
- hash: 1355709900760715495
169
+ hash: -2036902327160108675
161
170
  requirements: []
162
171
  rubyforge_project:
163
172
  rubygems_version: 1.8.25
@@ -167,6 +176,10 @@ summary: A fast, parallel shopify api client using typhoeus/hydraa
167
176
  test_files:
168
177
  - spec/shydra/hydra_spec.rb
169
178
  - spec/shydra/request_spec.rb
179
+ - spec/shydra/resource_spec.rb
180
+ - spec/shydra/resources/metafield_spec.rb
181
+ - spec/shydra/resources/product_spec.rb
182
+ - spec/shydra/resources/variant_spec.rb
170
183
  - spec/shydra/response_spec.rb
171
184
  - spec/shydra_spec.rb
172
185
  - spec/spec_helper.rb