google-content-api 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: 7befcf58ddc6c724cdb03706d3f388bf034e9c59
4
- data.tar.gz: 2485b540d118c17240864510667805f8d2857e2a
3
+ metadata.gz: 51a3d8a021a793def5f0192465327b813ca77ced
4
+ data.tar.gz: 7b9393a2dd126af28e549951611d29089a252a42
5
5
  SHA512:
6
- metadata.gz: 158b953cf148e4cb71441067821f0b2eabf523b67983d35bf003fde06937545bb1e14d9b6c4eccd633ec636b96eeabed0edab7ea97ef09800b81b9285c0779e2
7
- data.tar.gz: 74dd48bfad2e3da6c4c75a6bd3c307683924b032c064b163f95e118c9f021b35dfc5a4db9443d7cb62a902393fdc7831289362d7282372fcd800b9cb8469a77a
6
+ metadata.gz: e96100f4aba6b6fb907b1626ed1cd7008c27934515064bfd0b8ccaa4e3507819bc4b47e87c4b10689a72b1da91bf8f0aeaa6f761375a4aa5bd1b515c1a872c1f
7
+ data.tar.gz: ee571bb03b1766c6d94baba02fb107e79467ffce1250b174d45aca7896818620372d1952179c8cd0262c5a10d346620e5dda89307e56374a5f35f6339eae0511
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Google Content API
1
+ # Google Content API [![Gem Version](https://badge.fury.io/rb/google-content-api.png)](http://badge.fury.io/rb/google-content-api)
2
2
 
3
3
  Gem for interacting with [Google's Content API for Shopping](https://developers.google.com/shopping-content/).
4
4
 
@@ -6,9 +6,9 @@ Gem for interacting with [Google's Content API for Shopping](https://developers.
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'google-content-api'
9
+ gem 'google-content-api', :require => "google_content_api"
10
10
 
11
- And then execute:
11
+ And execute:
12
12
 
13
13
  $ bundle
14
14
 
@@ -16,6 +16,8 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install google-content-api
18
18
 
19
+ And `require 'google_content_api'`
20
+
19
21
  ## Example Usage
20
22
 
21
23
  ```ruby
@@ -18,7 +18,7 @@ module GoogleContentApi
18
18
  when "managed_accounts"
19
19
  "#{base_url}/managedaccounts"
20
20
  when "products"
21
- "#{base_url}/items/products/schema/batch?warnings"
21
+ "#{base_url}/items/products/schema/batch?warnings="
22
22
  when "item", "product"
23
23
  raise "must supply language, country and item id" \
24
24
  if options[:language].nil? || options[:country].nil? || options[:item_id].nil?
@@ -28,6 +28,6 @@ module GoogleContentApi
28
28
  raise "unknown zone"
29
29
  end
30
30
 
31
- options[:dry_run] ? url + "&dry-run" : url
31
+ options[:dry_run] ? url + "&dry-run=" : url
32
32
  end
33
33
  end
@@ -22,6 +22,24 @@ module GoogleContentApi
22
22
  end
23
23
  alias_method :create_items, :create_products
24
24
 
25
+ def update_products(sub_account_id, products, dry_run = false)
26
+ token = Authorization.fetch_token
27
+ products_url = GoogleContentApi.urls("products", sub_account_id, :dry_run => dry_run)
28
+ xml = update_product_items_batch_xml(products)
29
+ Faraday.headers = {
30
+ "Content-Type" => "application/atom+xml",
31
+ "Authorization" => "AuthSub token=#{token}"
32
+ }
33
+
34
+ response = Faraday.post products_url, xml
35
+
36
+ if response.status == 200
37
+ response
38
+ else
39
+ raise "Unable to batch insert products - received status #{response.status}. body: #{response.body}"
40
+ end
41
+ end
42
+
25
43
  def delete(sub_account_id, params)
26
44
  token = Authorization.fetch_token
27
45
  product_url = GoogleContentApi.urls("product", sub_account_id, :language => params[:language], :country => params[:country], :item_id => params[:item_id], :dry_run => params[:dry_run])
@@ -62,25 +80,34 @@ module GoogleContentApi
62
80
  end
63
81
 
64
82
  def create_product_items_batch_xml(items)
83
+ generalized_product_items_batch_xml(items, :type => 'INSERT')
84
+ end
85
+
86
+ def update_product_items_batch_xml(items)
87
+ generalized_product_items_batch_xml(items, :type => 'UPDATE')
88
+ end
89
+
90
+ def generalized_product_items_batch_xml(items, opts)
65
91
  Nokogiri::XML::Builder.new do |xml|
66
92
  xml.feed('xmlns' => 'http://www.w3.org/2005/Atom', 'xmlns:batch' => 'http://schemas.google.com/gdata/batch') do
67
93
  items.each do |attributes|
68
94
  xml.entry('xmlns' => 'http://www.w3.org/2005/Atom', 'xmlns:sc' => 'http://schemas.google.com/structuredcontent/2009', 'xmlns:scp' => 'http://schemas.google.com/structuredcontent/2009/products', 'xmlns:app' => 'http://www.w3.org/2007/app') do
69
- xml['batch'].operation_(:type => 'INSERT')
70
- add_mandatory_values(xml, attributes)
71
- add_optional_values(xml, attributes)
95
+ xml['batch'].operation_(:type => opts[:type])
96
+ add_mandatory_values(xml, attributes, opts)
97
+ add_optional_values(xml, attributes, opts)
72
98
  end
73
99
  end
74
100
  end
75
101
  end.to_xml
76
102
  end
77
103
 
78
- def add_mandatory_values(xml, attributes)
104
+ def add_mandatory_values(xml, attributes, opts)
79
105
  xml['batch'].id_ attributes[:id]
80
106
  xml['sc'].id_ attributes[:id]
81
107
  xml.title_ attributes[:title]
82
108
  xml.content_ attributes[:description], :type => 'text'
83
- xml.link_(:rel => 'alternate', :type => 'text/html', :href => attributes[:link])
109
+ link_rel = opts[:type] == 'UPDATE' ? 'edit' : 'alternate'
110
+ xml.link_(:rel => link_rel, :type => 'text/html', :href => attributes[:link])
84
111
  xml['sc'].image_link_ attributes[:image]
85
112
  xml['sc'].content_language_ attributes[:content_language]
86
113
  xml['sc'].target_country_ attributes[:target_country]
@@ -89,7 +116,7 @@ module GoogleContentApi
89
116
  xml['scp'].price_ attributes[:price], :unit => attributes[:currency]
90
117
  end
91
118
 
92
- def add_optional_values(xml, attributes)
119
+ def add_optional_values(xml, attributes, opts)
93
120
  if attributes[:expiration_date]
94
121
  xml['sc'].expiration_date_ attributes[:expiration_date]
95
122
  end
@@ -149,4 +176,4 @@ module GoogleContentApi
149
176
  end
150
177
  end
151
178
 
152
- end
179
+ end
@@ -1,3 +1,3 @@
1
1
  module GoogleContentApi
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -28,9 +28,9 @@ describe GoogleContentApi::Product do
28
28
  GoogleContentApi::Authorization.should_receive(:fetch_token).once.and_return(fake_token)
29
29
  stub_request(:post, GoogleContentApi.urls("products", sub_account_id, :dry_run => dry_run)).
30
30
  with(:headers => {
31
- 'Content-Type' => 'application/atom+xml',
32
- 'Authorization' => "AuthSub token=#{fake_token}"
33
- }).to_return(:status => 200)
31
+ 'Content-Type' => 'application/atom+xml',
32
+ 'Authorization' => "AuthSub token=#{fake_token}"
33
+ }).to_return(:status => 200)
34
34
 
35
35
  subject.create_products(sub_account_id, [], dry_run).status.should == 200
36
36
  end
@@ -44,13 +44,28 @@ describe GoogleContentApi::Product do
44
44
  end
45
45
  end
46
46
 
47
+
48
+ describe ".update" do
49
+ it "status == 200" do
50
+ GoogleContentApi::Authorization.should_receive(:fetch_token).once.and_return(fake_token)
51
+ stub_request(:post, GoogleContentApi.urls("products", sub_account_id, :dry_run => dry_run)).
52
+ with(:headers => {
53
+ 'Content-Type' => 'application/atom+xml',
54
+ 'Authorization' => "AuthSub token=#{fake_token}"
55
+ }).to_return(:status => 200)
56
+
57
+ subject.update_products(sub_account_id, [], dry_run).status.should == 200
58
+ end
59
+ end
60
+
47
61
  describe "private" do
48
62
  describe ".create_product_items_batch_xml" do
49
63
  it "creates an xml with all given product attributes" do
50
64
  result_xml = subject.send(:create_product_items_batch_xml, [product_attributes])
51
65
 
52
- result_xml.should match 'xmlns:batch="http://schemas.google.com/gdata/batch">'
66
+ result_xml.should match 'xmlns:batch="http://schemas.google.com/gdata/batch"'
53
67
  result_xml.should match 'xmlns:scp="http://schemas.google.com/structuredcontent/2009/products"'
68
+ result_xml.should match 'batch:operation type="INSERT"'
54
69
  product_attributes.each { |attribute, value| result_xml.should match /#{value}/ }
55
70
  end
56
71
 
@@ -63,6 +78,14 @@ describe GoogleContentApi::Product do
63
78
  end
64
79
  end
65
80
 
81
+ describe ".update_product_items_batch_xml" do
82
+ it "creates an xml with all given product attributes" do
83
+ result_xml = subject.send(:update_product_items_batch_xml, [product_attributes])
84
+ result_xml.should match 'batch:operation type="UPDATE"'
85
+ product_attributes.each{ |attribute, value| result_xml.should match /#{value}/ }
86
+ end
87
+ end
88
+
66
89
  describe ".add_optional_values" do
67
90
  let(:unit) { "g" }
68
91
  let(:unit_pricing_measure) { 100 }
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-content-api
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
  - DaWanda GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-29 00:00:00.000000000 Z
11
+ date: 2014-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '2.13'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.13'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: webmock
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.11.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.11.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: google-api-client
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0.4'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.4'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: nokogiri
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '1.5'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.5'
97
97
  description: Gem for interacting with Google's Content API.
@@ -101,8 +101,8 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - .gitignore
105
- - .rspec
104
+ - ".gitignore"
105
+ - ".rspec"
106
106
  - Gemfile
107
107
  - LICENSE.txt
108
108
  - README.md
@@ -129,17 +129,17 @@ require_paths:
129
129
  - lib
130
130
  required_ruby_version: !ruby/object:Gem::Requirement
131
131
  requirements:
132
- - - '>='
132
+ - - ">="
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  requirements:
137
- - - '>='
137
+ - - ">="
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0'
140
140
  requirements: []
141
141
  rubyforge_project:
142
- rubygems_version: 2.1.1
142
+ rubygems_version: 2.2.2
143
143
  signing_key:
144
144
  specification_version: 4
145
145
  summary: Gem for interacting with Google's Content API.