goods 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/goods/catalog.rb +4 -0
- data/lib/goods/category.rb +2 -2
- data/lib/goods/containable.rb +8 -6
- data/lib/goods/currency.rb +4 -4
- data/lib/goods/offer.rb +4 -3
- data/lib/goods/version.rb +1 -1
- data/lib/goods/xml.rb +40 -17
- data/spec/fixtures/simple_catalog.xml +23 -11
- data/spec/goods/offer_spec.rb +6 -1
- data/spec/goods/xml_spec.rb +7 -0
- data/spec/support/shared_examples_for_containable.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 587e410504981ece079d24dbbc15a0408ca54985
|
4
|
+
data.tar.gz: d34b7f6dcbac3169e10ba29fb01bda914c5f0736
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b94d34341aaab7c7319c85dd0df6614e1c769acdc9490c27cc00a45e38c80cd237cbd3f0a269c1df1dc65f8cfcc4fbf190083bc4264d459d692771efd881aa57
|
7
|
+
data.tar.gz: 216ac093e960bf563e6262cd0973d5ae5152ff47846c6e7e6051a8aaa4eb0fad5e4668b78a6c872f62348a2c73e028b71199518e22da686dbcc4d2bbffa3fd1d
|
data/lib/goods/catalog.rb
CHANGED
data/lib/goods/category.rb
CHANGED
data/lib/goods/containable.rb
CHANGED
@@ -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}",
|
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
|
23
|
-
@
|
22
|
+
def _info_hash
|
23
|
+
@_info_hash
|
24
24
|
end
|
25
|
+
private :_info_hash
|
25
26
|
|
26
27
|
def id
|
27
|
-
@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
|
51
|
-
@
|
51
|
+
def _info_hash=(info_hash)
|
52
|
+
@_info_hash = info_hash
|
52
53
|
end
|
54
|
+
private :_info_hash=
|
53
55
|
end
|
54
56
|
end
|
data/lib/goods/currency.rb
CHANGED
@@ -2,16 +2,16 @@ module Goods
|
|
2
2
|
class Currency
|
3
3
|
include Containable
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
self.
|
5
|
+
def initialize(info_hash)
|
6
|
+
self._info_hash = info_hash
|
7
7
|
end
|
8
8
|
|
9
9
|
def rate
|
10
|
-
@rate ||=
|
10
|
+
@rate ||= _info_hash[:rate].to_f
|
11
11
|
end
|
12
12
|
|
13
13
|
def plus
|
14
|
-
@plus ||=
|
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(
|
15
|
-
self.
|
16
|
-
@price =
|
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
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
|
49
|
-
name: category
|
56
|
+
id: extract_attribute(category, :id),
|
57
|
+
name: extract_text(category)
|
50
58
|
}
|
51
|
-
category_hash[:parent_id] =
|
52
|
-
|
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
|
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] =
|
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
|
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,
|
121
|
-
offer_hash[property] = (
|
121
|
+
}.each do |property, xpath|
|
122
|
+
offer_hash[property] = extract_text(offer, xpath)
|
122
123
|
end
|
123
124
|
|
124
|
-
offer_hash[:price] = offer
|
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>
|
36
|
-
|
35
|
+
<currencyId>
|
36
|
+
RUR
|
37
|
+
</currencyId>
|
38
|
+
<categoryId type="Own">
|
39
|
+
100
|
40
|
+
</categoryId>
|
37
41
|
<categoryId type="Own">101</categoryId>
|
38
|
-
<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
|
49
|
+
<vendor>
|
50
|
+
НP
|
51
|
+
</vendor>
|
44
52
|
<vendorCode>Q7533A</vendorCode>
|
45
|
-
<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
|
68
|
+
<name>
|
69
|
+
Все не так. В 2 томах. Том 1
|
70
|
+
</name>
|
59
71
|
<publisher>ЭКСМО - Пресс</publisher>
|
60
72
|
<series>А. Маринина - королева детектива</series>
|
61
73
|
<year>2009</year>
|
data/spec/goods/offer_spec.rb
CHANGED
@@ -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
|
data/spec/goods/xml_spec.rb
CHANGED
@@ -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
|
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.
|
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-
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|