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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ba028ffc9f2ea9eeec008971060a0b66c2c99ea
4
- data.tar.gz: 1e80a10fd4e52f22c57789994592ce09c62added
3
+ metadata.gz: a0011750815867641124fd10b42a9c29157cf738
4
+ data.tar.gz: cf3a8fd8a6212bd050b38d188b62c32d0804cf43
5
5
  SHA512:
6
- metadata.gz: ca29741ba878d8c0a29e43c256737d606ed011babe5f0a73102611c4c9bef3d94ca67276f757240a2649ba285d77bf7a5b012ccdaf2c0f62e480102e9bd2cf2b
7
- data.tar.gz: cc1780dca99d100667402dd7b7ac541b545d853232b71af992cffb22ef8c51b8b64c027134e10abb49959091f698476ecc42f96d0ee62df51e19f3074e9bf6d9
6
+ metadata.gz: bd2df6ed0f8f557529d6826bee5525df56b7c2a379db0b4485630c6e33d17c759ca56ba9943cb395ac6363a1509f18c3d1b748ef61fe91dcab4a9be0ea8d16f4
7
+ data.tar.gz: 668f87a2ecc661ab59c7888eb3d7d628d0cf4677f38fa942141883341bd246d1e82ec968673bd868c164d0dd3cf7155267a49f31c8ce0691a92e691c2446b78b
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ test/version_tmp
17
17
  tmp
18
18
  .DS_Store
19
19
  *sublime*
20
+ .idea/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0.0"
4
+ - "2.1.0"
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
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
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[:string]
7
- from_string(params[:string], params[:url], params[:encoding])
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 from_string(xml_string, url, encoding)
29
- @xml_string = xml_string
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
@@ -1,3 +1,3 @@
1
1
  module Goods
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -9,14 +9,14 @@ module Goods
9
9
  @error = nil
10
10
  end
11
11
 
12
- def valid?(xml)
13
- validate(xml)
12
+ def valid?(xml_io)
13
+ validate(xml_io)
14
14
  error.nil?
15
15
  end
16
16
 
17
- def validate(xml)
17
+ def validate(xml_io)
18
18
  @error = nil
19
- document = LibXML::XML::Document.string(xml)
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(string, url = nil, encoding = nil)
8
- @xml_source = Nokogiri::XML::Document.parse(string, url, encoding)
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
- !! (attr.value =~ /true/)
109
- else
110
- true
111
- end
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, url=nil, encoding=nil)
17
- validator = XML::Validator.new
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
- xml_string = self.load url
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
- xml_string = self.load file
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(source) do |f|
39
- f.read
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
@@ -2,13 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe Goods::Catalog do
4
4
  describe "#initialize" do
5
- it "should call #from_string when string is passed" do
6
- expect_any_instance_of(Goods::Catalog).to receive(:from_string).
7
- with("string", "url", "UTF-8").once
8
- Goods::Catalog.new(string: "string", url: "url", encoding: "UTF-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 none of 'string', 'url', 'file' params is passed" do
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
- it "should create #{part}" do
25
+ it "should create #{part}" do
25
26
  expect(part).to receive(:new).and_return(NullObject.new).once
26
- Goods::Catalog.new(string: "xml")
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(:xml) {
33
- File.read(File.expand_path("../../fixtures/simple_catalog.xml", __FILE__))
33
+ let(:xml_io) {
34
+ open File.expand_path("../../fixtures/simple_catalog.xml", __FILE__)
34
35
  }
35
- let(:catalog) { Goods::Catalog.new string: xml}
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.read(File.expand_path("../../../fixtures/simple_catalog.xml", __FILE__)) }
5
- let(:invalid_document) { File.read(File.expand_path("../../../fixtures/empty_catalog.xml", __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 ".from_string" do
9
- let(:valid_document) { File.read(File.expand_path("../fixtures/simple_catalog.xml", __FILE__)) }
10
- let(:invalid_document) { File.read(File.expand_path("../fixtures/empty_catalog.xml", __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 file is passed" do
12
+ it "should return catalog if valid xml io is passed" do
13
13
  expect(Goods::Catalog).to receive(:new).
14
- with({string: valid_document, url: "url", encoding: "UTF-8"})
15
- Goods.from_string(valid_document, "url", "UTF-8")
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 file is passed" do
19
- expect { Goods.from_string(invalid_document) }.to raise_error(Goods::XML::InvalidFormatError)
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 .from_string" do
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(:from_string).with(params[:string], nil, params[:encoding])
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 .from_string" do
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(:from_string).with(params[:string], params[:url], params[:encoding])
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.3
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-01-30 00:00:00.000000000 Z
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