goods 0.0.1 → 0.0.2

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: bc907ce0650d82d39c2d077d63492e2ad9b1d4fa
4
- data.tar.gz: 69b1b31e9bdfc828364fb7c16dda6c53545b3dfe
3
+ metadata.gz: 587e410504981ece079d24dbbc15a0408ca54985
4
+ data.tar.gz: d34b7f6dcbac3169e10ba29fb01bda914c5f0736
5
5
  SHA512:
6
- metadata.gz: 783ddac11cd0e9ac7648a2dc36c8f34573ad2d5935529e91e95ffd8ed2b9d76b7aedec8fdf63818f1a713facb284270e6b4e34c3dc6d6257ddac6dc341225ada
7
- data.tar.gz: 37c18feb8ebe1e7f0108690ee662bb6f869726b1495277250d3fcbbf35a877200d02d3bb009777eac26800dfead0407848dcc75c97f5e8b22ebb93bdaae2a106
6
+ metadata.gz: b94d34341aaab7c7319c85dd0df6614e1c769acdc9490c27cc00a45e38c80cd237cbd3f0a269c1df1dc65f8cfcc4fbf190083bc4264d459d692771efd881aa57
7
+ data.tar.gz: 216ac093e960bf563e6262cd0973d5ae5152ff47846c6e7e6051a8aaa4eb0fad5e4668b78a6c872f62348a2c73e028b71199518e22da686dbcc4d2bbffa3fd1d
data/lib/goods/catalog.rb CHANGED
@@ -19,6 +19,10 @@ module Goods
19
19
  @offers.convert_currency(other_currency)
20
20
  end
21
21
 
22
+ def date
23
+ @xml.generation_date
24
+ end
25
+
22
26
  private
23
27
 
24
28
  def from_string(xml_string, url, encoding)
@@ -5,8 +5,8 @@ module Goods
5
5
  attr_field :parent_id
6
6
  attr_field :name
7
7
 
8
- def initialize(description)
9
- self.description = description
8
+ def initialize(info_hash)
9
+ self._info_hash = info_hash
10
10
  @parent_at_level = {}
11
11
  end
12
12
 
@@ -12,19 +12,20 @@ module Goods
12
12
  if field = instance_variable_get("@#{field_name}")
13
13
  field
14
14
  else
15
- instance_variable_set("@#{field_name}", description[field_name])
15
+ instance_variable_set("@#{field_name}", _info_hash[field_name])
16
16
  instance_variable_get("@#{field_name}")
17
17
  end
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
- def description
23
- @description
22
+ def _info_hash
23
+ @_info_hash
24
24
  end
25
+ private :_info_hash
25
26
 
26
27
  def id
27
- @id ||= description[:id]
28
+ @id ||= _info_hash[:id]
28
29
  end
29
30
 
30
31
  def invalid_fields
@@ -47,8 +48,9 @@ module Goods
47
48
  invalid_fields << field unless predicate.call(send(field))
48
49
  end
49
50
 
50
- def description=(description)
51
- @description = description
51
+ def _info_hash=(info_hash)
52
+ @_info_hash = info_hash
52
53
  end
54
+ private :_info_hash=
53
55
  end
54
56
  end
@@ -2,16 +2,16 @@ module Goods
2
2
  class Currency
3
3
  include Containable
4
4
 
5
- def initialize(description)
6
- self.description = description
5
+ def initialize(info_hash)
6
+ self._info_hash = info_hash
7
7
  end
8
8
 
9
9
  def rate
10
- @rate ||= description[:rate].to_f
10
+ @rate ||= _info_hash[:rate].to_f
11
11
  end
12
12
 
13
13
  def plus
14
- @plus ||= description[:plus].to_f
14
+ @plus ||= _info_hash[:plus].to_f
15
15
  end
16
16
 
17
17
  def in(other_currency)
data/lib/goods/offer.rb CHANGED
@@ -10,10 +10,11 @@ module Goods
10
10
  attr_field :name
11
11
  attr_field :picture
12
12
  attr_field :vendor
13
+ attr_field :url
13
14
 
14
- def initialize(description)
15
- self.description = description
16
- @price = description[:price].to_f
15
+ def initialize(info_hash)
16
+ self._info_hash = info_hash
17
+ @price = _info_hash[:price].to_f
17
18
  end
18
19
 
19
20
  def convert_currency(other_currency)
data/lib/goods/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Goods
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/goods/xml.rb CHANGED
@@ -20,6 +20,10 @@ module Goods
20
20
  @offers ||= extract_offers
21
21
  end
22
22
 
23
+ def generation_date
24
+ @generation_date ||= extract_catalog_generation_date
25
+ end
26
+
23
27
 
24
28
  private
25
29
 
@@ -32,6 +36,10 @@ module Goods
32
36
  catalog_node / "shop"
33
37
  end
34
38
 
39
+ def extract_catalog_generation_date
40
+ Time.parse(catalog_node.attribute("date").value)
41
+ end
42
+
35
43
  # Categories part
36
44
  def categories_node
37
45
  shop_node / "categories" / "category"
@@ -45,14 +53,11 @@ module Goods
45
53
 
46
54
  def category_node_to_hash(category)
47
55
  category_hash = {
48
- id: category.attribute("id").value,
49
- name: category.text
56
+ id: extract_attribute(category, :id),
57
+ name: extract_text(category)
50
58
  }
51
- category_hash[:parent_id] = if category.attribute("parentId")
52
- category.attribute("parentId").value
53
- else
54
- nil
55
- end
59
+ category_hash[:parent_id] = extract_attribute(category, "parentId")
60
+
56
61
  category_hash
57
62
  end
58
63
 
@@ -69,7 +74,7 @@ module Goods
69
74
 
70
75
  def currency_node_to_hash(currency)
71
76
  currency_hash = {
72
- id: currency.attribute("id").value
77
+ id: extract_attribute(currency, "id")
73
78
  }
74
79
 
75
80
  attributes_with_defaults = {
@@ -77,11 +82,7 @@ module Goods
77
82
  plus: "0"
78
83
  }
79
84
  attributes_with_defaults.each do |attr, default|
80
- currency_hash[attr] = if currency.attribute(attr.to_s)
81
- currency.attribute(attr.to_s).value
82
- else
83
- default
84
- end
85
+ currency_hash[attr] = extract_attribute(currency, attr, default)
85
86
  end
86
87
 
87
88
  currency_hash
@@ -100,7 +101,7 @@ module Goods
100
101
 
101
102
  def offer_node_to_hash(offer)
102
103
  offer_hash = {
103
- id: offer.attribute("id").value
104
+ id: extract_attribute(offer, "id")
104
105
  }
105
106
 
106
107
  offer_hash[:available] = if attr = offer.attribute("available")
@@ -117,13 +118,35 @@ module Goods
117
118
  name: "name",
118
119
  vendor: "vendor",
119
120
  model: "model"
120
- }.each do |property, node|
121
- offer_hash[property] = (el = offer.xpath(node).first) ? el.text.strip : nil
121
+ }.each do |property, xpath|
122
+ offer_hash[property] = extract_text(offer, xpath)
122
123
  end
123
124
 
124
- offer_hash[:price] = offer.xpath("price").first.text.to_f
125
+ offer_hash[:price] = extract_text(offer, "price").to_f
125
126
 
126
127
  offer_hash
127
128
  end
129
+
130
+ def extract_attribute(node, attribute, default = nil)
131
+ if node.attribute(attribute.to_s)
132
+ node.attribute(attribute.to_s).value.strip
133
+ else
134
+ default
135
+ end
136
+ end
137
+
138
+ def extract_text(node, xpath = nil, default = nil)
139
+ target = if xpath
140
+ node.xpath(xpath).first
141
+ else
142
+ node
143
+ end
144
+
145
+ if target
146
+ target.text.strip
147
+ else
148
+ default
149
+ end
150
+ end
128
151
  end
129
152
  end
@@ -7,14 +7,14 @@
7
7
  <url>http://www.magazin.ru/</url>
8
8
 
9
9
  <currencies>
10
- <currency id="RUR" rate="1" plus="0"/>
11
- <currency id="USD" rate="30"/>
10
+ <currency id="RUR" rate=" 1 " plus="0"/>
11
+ <currency id="USD" rate=" 30 "/>
12
12
  <currency id="KZT"/>
13
13
  </currencies>
14
14
 
15
15
  <categories>
16
- <category id="1">Оргтехника</category>
17
- <category id="10" parentId="1">Принтеры</category>
16
+ <category id=" 1 ">Оргтехника</category>
17
+ <category id="10" parentId=" 1 ">Принтеры</category>
18
18
  <category id="100" parentId="10">Струйные принтеры</category>
19
19
  <category id="101" parentId="10">Лазерные принтеры</category>
20
20
 
@@ -29,20 +29,30 @@
29
29
  <local_delivery_cost>300</local_delivery_cost>
30
30
 
31
31
  <offers>
32
- <offer id="123" type="vendor.model" bid="13" cbid="20">
32
+ <offer id=" 123 " type="vendor.model" bid="13" cbid="20">
33
33
  <url>http://magazin.ru/product_page.asp?pid=14344</url>
34
34
  <price>15000</price>
35
- <currencyId>RUR</currencyId>
36
- <categoryId type="Own">100</categoryId>
35
+ <currencyId>
36
+ RUR
37
+ </currencyId>
38
+ <categoryId type="Own">
39
+ 100
40
+ </categoryId>
37
41
  <categoryId type="Own">101</categoryId>
38
- <picture>http://magazin.ru/img/device1.jpg</picture>
42
+ <picture>
43
+ http://magazin.ru/img/device1.jpg
44
+ </picture>
39
45
  <picture>http://magazin.ru/img/device2.jpg</picture>
40
46
  <delivery>true</delivery>
41
47
  <local_delivery_cost>300</local_delivery_cost>
42
48
  <typePrefix>Принтер</typePrefix>
43
- <vendor>НP</vendor>
49
+ <vendor>
50
+ НP
51
+ </vendor>
44
52
  <vendorCode>Q7533A</vendorCode>
45
- <model>Color LaserJet 3000</model>
53
+ <model>
54
+ Color LaserJet 3000
55
+ </model>
46
56
  <description>A4, 64Mb, 600x600 dpi, USB 2.0, 29стр/мин ч/б / 15стр/мин цв, лотки на 100л и 250л, плотность до 175г/м, до 60000 стр/месяц </description>
47
57
  <manufacturer_warranty>true</manufacturer_warranty>
48
58
  <country_of_origin>Япония</country_of_origin>
@@ -55,7 +65,9 @@
55
65
  <delivery>true</delivery>
56
66
  <local_delivery_cost>300</local_delivery_cost>
57
67
  <author>Александра Маринина</author>
58
- <name>Все не так. В 2 томах. Том 1</name>
68
+ <name>
69
+ Все не так. В 2 томах. Том 1
70
+ </name>
59
71
  <publisher>ЭКСМО - Пресс</publisher>
60
72
  <series>А. Маринина - королева детектива</series>
61
73
  <year>2009</year>
@@ -4,7 +4,7 @@ describe Goods::Offer do
4
4
  let(:books) { Goods::Category.new(id: "1", name: "Books") }
5
5
  let(:rur) { Goods::Currency.new(id: "RUR", rate: 1, plus: 0) }
6
6
  let(:valid_description) do
7
- {id: "1", name: "The Lord of The Rings", category_id: "1", currency_id: "RUR", price: 10}
7
+ {id: "1", name: "The Lord of The Rings", category_id: "1", currency_id: "RUR", price: 10}
8
8
  end
9
9
  let(:valid_offer) do
10
10
  offer = Goods::Offer.new(valid_description)
@@ -48,6 +48,11 @@ describe Goods::Offer do
48
48
  end
49
49
  end
50
50
 
51
+ it "should return offer description, not description hash" do
52
+ offer = Goods::Offer.new(valid_description.merge(description: "Greatest book"))
53
+ expect(offer.description).to eql("Greatest book")
54
+ end
55
+
51
56
  it "should have floting point price" do
52
57
  expect(Goods::Offer.new(price: 5).price).to be_kind_of(Float)
53
58
  end
@@ -8,6 +8,7 @@ describe Goods::XML do
8
8
  SIMPLE_CATALOG_CATEGORIES_COUNT = 9
9
9
  SIMPLE_CATALOG_CURRENCIES_COUNT = 3
10
10
  SIMPLE_CATALOG_OFFERS_COUNT = 2
11
+ SIMPLE_CATALOG_GENERATION_TIME = Time.parse("2010-04-01 17:00")
11
12
 
12
13
  describe "#initialize" do
13
14
  it 'should use Nokogiri for parsing' do
@@ -175,4 +176,10 @@ describe Goods::XML do
175
176
  end
176
177
  end
177
178
  end
179
+
180
+ describe "#generation_date" do
181
+ it "should correctly get yml_catalog date" do
182
+ expect(simple_catalog.generation_date).to eq(SIMPLE_CATALOG_GENERATION_TIME)
183
+ end
184
+ end
178
185
  end
@@ -1,5 +1,5 @@
1
1
  shared_examples "containable" do
2
- [:description, :id, :invalid_fields].each do |prop|
2
+ [:id, :invalid_fields].each do |prop|
3
3
  it "should give access to #{prop}" do
4
4
  expect(element).to respond_to(prop)
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Pyanykh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-28 00:00:00.000000000 Z
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler