goods 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +4 -0
- data/README.md +3 -1
- data/Rakefile +5 -0
- data/lib/goods/catalog.rb +4 -5
- data/lib/goods/version.rb +1 -1
- data/lib/goods/xml/validator.rb +4 -4
- data/lib/goods/xml.rb +7 -6
- data/lib/goods.rb +14 -13
- data/spec/goods/catalog_spec.rb +12 -13
- data/spec/goods/xml/validator_spec.rb +2 -2
- data/spec/goods_spec.rb +22 -12
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0011750815867641124fd10b42a9c29157cf738
|
4
|
+
data.tar.gz: cf3a8fd8a6212bd050b38d188b62c32d0804cf43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd2df6ed0f8f557529d6826bee5525df56b7c2a379db0b4485630c6e33d17c759ca56ba9943cb395ac6363a1509f18c3d1b748ef61fe91dcab4a9be0ea8d16f4
|
7
|
+
data.tar.gz: 668f87a2ecc661ab59c7888eb3d7d628d0cf4677f38fa942141883341bd246d1e82ec968673bd868c164d0dd3cf7155267a49f31c8ce0691a92e691c2446b78b
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
# Goods
|
1
|
+
# Goods
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/ArtemPyanykh/goods.png?branch=master)](https://travis-ci.org/ArtemPyanykh/goods)
|
2
4
|
|
3
5
|
The purpose of this gem is to provide simple, yet reliable solution for parsing
|
4
6
|
YML (Yandex Market Language) files, with clean and convenient interface,
|
data/Rakefile
CHANGED
data/lib/goods/catalog.rb
CHANGED
@@ -3,8 +3,8 @@ module Goods
|
|
3
3
|
attr_reader :categories, :currencies, :offers
|
4
4
|
|
5
5
|
def initialize(params)
|
6
|
-
if params[:
|
7
|
-
|
6
|
+
if params[:io]
|
7
|
+
from_io(params[:io], params[:url], params[:encoding])
|
8
8
|
else
|
9
9
|
raise ArgumentError, "should provide either :string or :url param"
|
10
10
|
end
|
@@ -25,9 +25,8 @@ module Goods
|
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
def
|
29
|
-
@
|
30
|
-
@xml = XML.new(xml_string, url, encoding)
|
28
|
+
def from_io(xml_io, url, encoding)
|
29
|
+
@xml = XML.new(xml_io, url, encoding)
|
31
30
|
@categories = CategoriesList.new(@xml.categories)
|
32
31
|
@currencies = CurrenciesList.new(@xml.currencies)
|
33
32
|
@offers = OffersList.new(@categories, @currencies, @xml.offers)
|
data/lib/goods/version.rb
CHANGED
data/lib/goods/xml/validator.rb
CHANGED
@@ -9,14 +9,14 @@ module Goods
|
|
9
9
|
@error = nil
|
10
10
|
end
|
11
11
|
|
12
|
-
def valid?(
|
13
|
-
validate(
|
12
|
+
def valid?(xml_io)
|
13
|
+
validate(xml_io)
|
14
14
|
error.nil?
|
15
15
|
end
|
16
16
|
|
17
|
-
def validate(
|
17
|
+
def validate(xml_io)
|
18
18
|
@error = nil
|
19
|
-
document = LibXML::XML::Document.
|
19
|
+
document = LibXML::XML::Document.io(xml_io)
|
20
20
|
|
21
21
|
# Should silence STDERR, because libxml2 spews validation error
|
22
22
|
# to standard error stream
|
data/lib/goods/xml.rb
CHANGED
@@ -4,8 +4,8 @@ module Goods
|
|
4
4
|
class XML
|
5
5
|
class InvalidFormatError < StandardError; end
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@xml_source = Nokogiri::XML::Document.parse(
|
7
|
+
def initialize(io, url = nil, encoding = nil)
|
8
|
+
@xml_source = Nokogiri::XML::Document.parse(io, url, encoding)
|
9
9
|
end
|
10
10
|
|
11
11
|
def categories
|
@@ -105,10 +105,11 @@ module Goods
|
|
105
105
|
}
|
106
106
|
|
107
107
|
offer_hash[:available] = if attr = offer.attribute("available")
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
108
|
+
!! (attr.value =~ /true/)
|
109
|
+
else
|
110
|
+
true
|
111
|
+
end
|
112
|
+
|
112
113
|
{
|
113
114
|
url: "url",
|
114
115
|
currency_id: "currencyId",
|
data/lib/goods.rb
CHANGED
@@ -13,30 +13,31 @@ require "goods/currencies_list"
|
|
13
13
|
require "goods/catalog"
|
14
14
|
|
15
15
|
module Goods
|
16
|
-
def self.from_string(xml_string,
|
17
|
-
|
18
|
-
if validator.valid? xml_string
|
19
|
-
Catalog.new(string: xml_string, url: url, encoding: encoding)
|
20
|
-
else
|
21
|
-
raise XML::InvalidFormatError, validator.error
|
22
|
-
end
|
16
|
+
def self.from_string(xml_string, encoding=nil)
|
17
|
+
from_io(StringIO.new(xml_string), nil, encoding)
|
23
18
|
end
|
24
19
|
|
25
20
|
def self.from_url(url, encoding=nil)
|
26
|
-
|
27
|
-
from_string(xml_string, url, encoding)
|
21
|
+
from_io(load(url), url, encoding)
|
28
22
|
end
|
29
23
|
|
30
24
|
def self.from_file(file, encoding=nil)
|
31
|
-
|
32
|
-
from_string(xml_string, nil, encoding)
|
25
|
+
from_io(load(file), nil, encoding)
|
33
26
|
end
|
34
27
|
|
35
28
|
private
|
36
29
|
|
37
30
|
def self.load(source)
|
38
|
-
open
|
39
|
-
|
31
|
+
open source
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.from_io(xml_io, url=nil, encoding=nil)
|
35
|
+
validator = XML::Validator.new
|
36
|
+
if validator.valid? xml_io
|
37
|
+
xml_io.rewind
|
38
|
+
Catalog.new(io: xml_io, url: url, encoding: encoding)
|
39
|
+
else
|
40
|
+
raise XML::InvalidFormatError, validator.error
|
40
41
|
end
|
41
42
|
end
|
42
43
|
end
|
data/spec/goods/catalog_spec.rb
CHANGED
@@ -2,13 +2,14 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Goods::Catalog do
|
4
4
|
describe "#initialize" do
|
5
|
-
it "should call #
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
it "should call #from_io when io is passed" do
|
6
|
+
catalog_io = StringIO.new("string")
|
7
|
+
expect_any_instance_of(Goods::Catalog).to receive(:from_io).
|
8
|
+
with(catalog_io, "url", "UTF-8").once
|
9
|
+
Goods::Catalog.new(io: catalog_io, url: "url", encoding: "UTF-8")
|
9
10
|
end
|
10
11
|
|
11
|
-
it "should raise error when
|
12
|
+
it "should raise error when no io is passed" do
|
12
13
|
expect{ Goods::Catalog.new({}) }.to raise_error(ArgumentError)
|
13
14
|
end
|
14
15
|
end
|
@@ -21,18 +22,18 @@ describe Goods::Catalog do
|
|
21
22
|
end
|
22
23
|
|
23
24
|
[Goods::XML, Goods::CategoriesList, Goods::CurrenciesList, Goods::OffersList].each do |part|
|
24
|
-
|
25
|
+
it "should create #{part}" do
|
25
26
|
expect(part).to receive(:new).and_return(NullObject.new).once
|
26
|
-
Goods::Catalog.new(
|
27
|
+
Goods::Catalog.new(io: StringIO.new("xml"))
|
27
28
|
end
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
32
|
describe "#prune" do
|
32
|
-
let(:
|
33
|
-
File.
|
33
|
+
let(:xml_io) {
|
34
|
+
open File.expand_path("../../fixtures/simple_catalog.xml", __FILE__)
|
34
35
|
}
|
35
|
-
let(:catalog) { Goods::Catalog.new
|
36
|
+
let(:catalog) { Goods::Catalog.new io: xml_io}
|
36
37
|
|
37
38
|
it "should prune offers and categories" do
|
38
39
|
level = 2
|
@@ -43,9 +44,7 @@ describe Goods::Catalog do
|
|
43
44
|
|
44
45
|
it "should replace categories of offers" do
|
45
46
|
catalog.prune(0)
|
46
|
-
expect(catalog.offers.find("123").category).to be(
|
47
|
-
catalog.categories.find("1")
|
48
|
-
)
|
47
|
+
expect(catalog.offers.find("123").category).to be(catalog.categories.find("1"))
|
49
48
|
end
|
50
49
|
|
51
50
|
it "should remove categories affected by prunning" do
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Goods::XML::Validator do
|
4
|
-
let(:valid_document) { File.
|
5
|
-
let(:invalid_document) { File.
|
4
|
+
let(:valid_document) { open File.expand_path("../../../fixtures/simple_catalog.xml", __FILE__) }
|
5
|
+
let(:invalid_document) { open File.expand_path("../../../fixtures/empty_catalog.xml", __FILE__) }
|
6
6
|
|
7
7
|
describe "#valid?" do
|
8
8
|
it "should return true if document is valid according to dtd" do
|
data/spec/goods_spec.rb
CHANGED
@@ -5,40 +5,50 @@ describe Goods do
|
|
5
5
|
expect(Goods::VERSION).to_not be_nil
|
6
6
|
end
|
7
7
|
|
8
|
-
describe ".
|
9
|
-
let(:valid_document) { File.
|
10
|
-
let(:invalid_document) { File.
|
8
|
+
describe ".from_io" do
|
9
|
+
let(:valid_document) { open File.expand_path("../fixtures/simple_catalog.xml", __FILE__) }
|
10
|
+
let(:invalid_document) { open File.expand_path("../fixtures/empty_catalog.xml", __FILE__) }
|
11
11
|
|
12
|
-
it "should return catalog if valid xml
|
12
|
+
it "should return catalog if valid xml io is passed" do
|
13
13
|
expect(Goods::Catalog).to receive(:new).
|
14
|
-
with({
|
15
|
-
Goods.
|
14
|
+
with({io: valid_document, url: "url", encoding: "UTF-8"})
|
15
|
+
Goods.from_io(valid_document, "url", "UTF-8")
|
16
16
|
end
|
17
17
|
|
18
|
-
it "should raise error if invalid
|
19
|
-
expect { Goods.
|
18
|
+
it "should raise error if invalid xml io is passed" do
|
19
|
+
expect { Goods.from_io(invalid_document) }.to raise_error(Goods::XML::InvalidFormatError)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
describe ".from_file" do
|
24
|
-
it "should load file and call .
|
24
|
+
it "should load file and call .from_io" do
|
25
25
|
params = {
|
26
26
|
file: "file", string: "string", url: nil, encoding: "UTF-8"
|
27
27
|
}
|
28
28
|
expect(Goods).to receive(:load).with(params[:file]) { params[:string] }
|
29
|
-
expect(Goods).to receive(:
|
29
|
+
expect(Goods).to receive(:from_io).with(params[:string], nil, params[:encoding])
|
30
30
|
Goods.from_file params[:file], params[:encoding]
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
describe ".from_url" do
|
35
|
-
it "should load remote page and call .
|
35
|
+
it "should load remote page and call .from_io" do
|
36
36
|
params = {
|
37
37
|
string: "string", url: "url", encoding: "UTF-8"
|
38
38
|
}
|
39
39
|
expect(Goods).to receive(:load).with(params[:url]) { params[:string] }
|
40
|
-
expect(Goods).to receive(:
|
40
|
+
expect(Goods).to receive(:from_io).with(params[:string], params[:url], params[:encoding])
|
41
41
|
Goods.from_url params[:url], params[:encoding]
|
42
42
|
end
|
43
43
|
end
|
44
|
+
|
45
|
+
describe ".from_string" do
|
46
|
+
it "should load string and call .from_io" do
|
47
|
+
params = {
|
48
|
+
string: "string", encoding: "UTF-8"
|
49
|
+
}
|
50
|
+
expect(Goods).to receive(:from_io)
|
51
|
+
Goods.from_string params[:string], params[:encoding]
|
52
|
+
end
|
53
|
+
end
|
44
54
|
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.4
|
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-
|
11
|
+
date: 2014-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,6 +92,7 @@ extra_rdoc_files: []
|
|
92
92
|
files:
|
93
93
|
- .gitignore
|
94
94
|
- .rspec
|
95
|
+
- .travis.yml
|
95
96
|
- Gemfile
|
96
97
|
- LICENSE.txt
|
97
98
|
- README.md
|