saper 0.5.0 → 0.5.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be3fc3ae66117c4a6e4c28cb4a85834f9de5bea4
4
- data.tar.gz: 41f8df60e4ee69d04868beef49a234218ec7e3d2
3
+ metadata.gz: 1e3837b908a7c2e26f770d28ba24bc8e5ceb5beb
4
+ data.tar.gz: 1a442deae6a10374bab445b695f1d83597ce37f7
5
5
  SHA512:
6
- metadata.gz: 6533dd86c1d9b23cf20094666131d78197a89782a5d9ff6b4237127a14ab8f6be45727461592b2cad3a1d28074b8c4abce17f2da04c95498e5213d216f00f89b
7
- data.tar.gz: 8b72ff711a4d6244e0ee8655fa40cd280a51961837a73604d090c014ea3f446a6d0ecfb434ed20df3d465038e52bea9ff2151e55aacb01cdf0c101fe267847e0
6
+ metadata.gz: 55844e966392ebd596d8a62ace3a9fc70831ea7b60ebfb2df81fc7ddbaad2aacac9c431adc5ff4dbd504fbe7b951670780c2778395473111adbbda2ca9c3e3d5
7
+ data.tar.gz: 3da6224447204cd0784d7abcc4806a68aa086ea89fe23401cde51eb3d5968b8b83a307be80ffcd4506979791b6c22f01cf99e1dc704a71c4b86c9e9c7623d884
data/Rakefile CHANGED
@@ -15,3 +15,5 @@ desc "Generate docs"
15
15
  YARD::Rake::YardocTask.new do |t|
16
16
  t.files = ['lib/saper/**/*.rb']
17
17
  end
18
+
19
+ task :default => :spec
data/lib/lib/nokogiri.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  # Monkey patching Nokogiri:
2
4
  # - correctly handle — character
3
5
 
@@ -6,7 +6,7 @@ module Saper
6
6
  accepts :text, :returns => :text
7
7
 
8
8
  run do |input, string|
9
- "%s%s" % [input, string]
9
+ Items::Text.new "%s%s" % [input, string]
10
10
  end
11
11
 
12
12
  end
@@ -201,9 +201,11 @@ module Saper
201
201
  self.class.run || Proc.new { |input, *args| input }
202
202
  end
203
203
 
204
+ # Returns string representation of Action.
205
+ # @return [String]
204
206
  def to_string
205
207
  "\t%s %s" % [self.class.type, @arguments.map(&:to_string).join(", ")]
206
208
  end
207
209
 
208
210
  end
209
- end
211
+ end
@@ -1,10 +1,20 @@
1
1
  module Saper
2
2
  class Browser
3
3
 
4
- require 'mechanize'
4
+ # Array of requested URLs.
5
+ attr_reader :history
5
6
 
6
- attr_reader :history, :received, :sent
7
+ # Approximate number of bytes received.
8
+ attr_reader :received
7
9
 
10
+ # Approximate number of bytes sent.
11
+ attr_reader :sent
12
+
13
+ # Returns a new Browser instance.
14
+ # @option options [Symbol] :agent User agent
15
+ # @option options [Hash] :headers Additional request headers
16
+ # @option options [Logger] :logger Logger instance
17
+ # @return [Saper::Browser]
8
18
  def initialize(options = {})
9
19
  @agent = options.delete(:agent)
10
20
  @headers = options.delete(:headers)
@@ -31,17 +41,22 @@ module Saper
31
41
  end
32
42
  end
33
43
 
34
- # Returns number of HTTP requests
44
+ # Returns the number of HTTP requests.
45
+ # @return [Integer]
35
46
  def requests
36
47
  @history.size
37
48
  end
38
49
 
39
- #
50
+ # Returns additional request headers.
51
+ # @return [Hash]
40
52
  def headers
41
53
  @headers.respond_to?(:to_hash) ? @headers : {}
42
54
  end
43
55
 
44
- #
56
+ # Performs a GET request and returns Saper::Document.
57
+ # @param url [String] URL to request
58
+ # @param query [Hash] query options
59
+ # @return [Saper::Document]
45
60
  def get(url, query = {})
46
61
  @logger.download(url)
47
62
  @history.push url
@@ -49,7 +64,10 @@ module Saper
49
64
  Saper::Items::Document.new data
50
65
  end
51
66
 
52
- #
67
+ # Performs a POST request and returns Saper::Document.
68
+ # @param url [String] URL to request
69
+ # @param query [Hash] payload
70
+ # @return [Saper::Document]
53
71
  def post(url, query = {})
54
72
  @logger.download(url)
55
73
  @history.push url
@@ -57,7 +75,8 @@ module Saper
57
75
  Saper::Items::Document.new data
58
76
  end
59
77
 
60
- #
78
+ # Returns User-Agent string used with requests.
79
+ # @return [String]
61
80
  def agent
62
81
  case @agent
63
82
  when :ie6
@@ -84,4 +103,4 @@ module Saper
84
103
  end
85
104
 
86
105
  end
87
- end
106
+ end
@@ -1,14 +1,19 @@
1
1
  module Saper
2
2
  module DSL
3
3
 
4
+ # Returns a new empty module with embedded parsing methods.
5
+ # @return [Object]
4
6
  def self.new
5
7
  Module.new.extend(Methods)
6
8
  end
7
9
 
10
+ # Enables recipe parsing in modules that include Saper::DSL.
11
+ # @return [void]
8
12
  def self.included(base)
9
13
  base.extend(Methods)
10
14
  end
11
15
 
16
+ # A mixin module with a set of parsing methods
12
17
  module Methods
13
18
 
14
19
  def namespace
@@ -65,4 +70,4 @@ module Saper
65
70
 
66
71
  end
67
72
  end
68
- end
73
+ end
@@ -66,5 +66,10 @@ module Saper
66
66
  self.class.type.to_sym
67
67
  end
68
68
 
69
+ #
70
+ def serialize
71
+ to_native
72
+ end
73
+
69
74
  end
70
75
  end
@@ -1,11 +1,19 @@
1
1
  module Saper
2
2
 
3
- def run(file, recipe, input = nil)
3
+ # Runs a recipe saved in a file.
4
+ # @param file [String] file name
5
+ # @param recipe [Symbol] recipe name
6
+ # @param input [Object] input for recipe
7
+ # @return [Saper::Runtime]
8
+ def self.run(file, recipe, input = nil)
4
9
  load(file).run(recipe, input)
5
10
  end
6
11
 
7
- def load(file)
8
- parse File.read(path)
12
+ # Parses recipes saved in a file.
13
+ # @param file [String] file name
14
+ # @return [Saper::Namespace]
15
+ def self.load(file)
16
+ Namespace.parse File.read(path)
9
17
  end
10
18
 
11
19
  # Namespace is a parsing / storage utility for recipes
@@ -175,7 +175,7 @@ module Saper
175
175
  # Returns runtime results as JSON.
176
176
  # @return [Array]
177
177
  def to_json(*args)
178
- JSON.generate(non_native_results)
178
+ JSON.generate non_native_results.map(&:serialize)
179
179
  end
180
180
 
181
181
  # Returns runtime results as Saper::Item instances.
@@ -15,24 +15,16 @@ module Saper
15
15
  @atts = hash
16
16
  end
17
17
 
18
- def serialize
19
- @atts.dup
20
- end
21
-
22
18
  def to_hash
23
19
  @atts.dup
24
20
  end
25
21
 
26
- def delete(name)
27
- @atts.delete(name)
28
- end
29
-
30
22
  def [](name)
31
23
  @atts[name]
32
24
  end
33
25
 
34
- def to_yaml(*args)
35
- @atts.to_yaml(*args)
26
+ def serialize
27
+ @atts.dup
36
28
  end
37
29
 
38
30
  def to_native(object = nil)
@@ -51,12 +43,8 @@ module Saper
51
43
  object
52
44
  end
53
45
 
54
- def to_json(*args)
55
- to_hash.to_json(*args)
56
- end
57
-
58
- def to_s
59
- "{ %s }" % @atts.map { |key, value| "%s = %s" % [key, value] }.join(", ")
46
+ def to_json
47
+ JSON.new(@hash, true)
60
48
  end
61
49
 
62
50
  end
@@ -51,6 +51,10 @@ module Saper
51
51
  JSON.new(body)
52
52
  end
53
53
 
54
+ def to_markdown
55
+ Markdown.new(to_html)
56
+ end
57
+
54
58
  def to_native
55
59
  @body
56
60
  end
@@ -56,10 +56,6 @@ module Saper
56
56
  @noko.search(xpath).each { |item| item.replace(item.children) }; self
57
57
  end
58
58
 
59
- def remove(tag)
60
- remove_children_preserving_content(tag)
61
- end
62
-
63
59
  def inner_html
64
60
  @noko.inner_html
65
61
  end
@@ -2,12 +2,14 @@ module Saper
2
2
  module Items
3
3
  class JSON < Item
4
4
 
5
- def self.new(item)
5
+ def self.new(item, bypass = false)
6
6
  super case item
7
7
  when Text
8
8
  parse(item.to_s)
9
9
  when String
10
10
  parse(item)
11
+ when Hash
12
+ bypass ? item : raise(InvalidItem, item)
11
13
  else
12
14
  raise(InvalidItem, item)
13
15
  end
@@ -25,43 +27,35 @@ module Saper
25
27
  @hash = hash
26
28
  end
27
29
 
28
- def [](name)
29
- value = @hash[name.to_s]
30
- case value
31
- when Hash
32
- self.class.new(value)
33
- when Array
34
- self.class.new(value)
35
- else
36
- value
37
- end
38
- end
39
-
40
30
  def find(xpath)
41
31
  find_all(xpath).first
42
32
  end
43
33
 
44
34
  def find_all(xpath)
45
- JSONSearch.find(@hash, xpath).map do |item|
46
- sanitize(item)
47
- end
35
+ sanitize JSONSearch.find(@hash, xpath)
36
+ end
37
+
38
+ def to_s
39
+ ::JSON.dump(@hash)
40
+ end
41
+
42
+ def to_native
43
+ to_s
48
44
  end
49
45
 
46
+ private
47
+
50
48
  def sanitize(item)
51
49
  case item
52
50
  when Hash
53
- self.class.new(item)
51
+ self.class.new(item, true)
54
52
  when Array
55
53
  item.map { |i| sanitize(i) }
56
54
  else
57
- item
55
+ Text.new(item.to_s)
58
56
  end
59
57
  end
60
58
 
61
- def to_native
62
- @hash
63
- end
64
-
65
59
  end
66
60
  end
67
61
  end
@@ -33,8 +33,8 @@ module Saper
33
33
  @vremya.to_time
34
34
  end
35
35
 
36
- def to_json(*args)
37
- @vremya.to_i.to_s
36
+ def serialize
37
+ to_s
38
38
  end
39
39
 
40
40
  end
@@ -3,18 +3,20 @@ module Saper
3
3
  class URL < Item
4
4
 
5
5
  def self.new(item)
6
- item = case item
6
+ super case item
7
7
  when Text
8
- item.to_s
8
+ parse(item.to_s)
9
9
  when String
10
- item
10
+ parse(item)
11
11
  else
12
12
  raise(InvalidItem, item)
13
13
  end
14
- unless item =~ /^#{URI::regexp}$/
15
- raise(InvalidItem, item)
14
+ end
15
+
16
+ def self.parse(string)
17
+ unless string =~ /^#{URI::regexp}$/
18
+ raise(InvalidItem, string)
16
19
  end
17
- super(item)
18
20
  end
19
21
 
20
22
  def initialize(url)
@@ -54,10 +54,6 @@ module Saper
54
54
  @noko.search(xpath).each { |item| item.replace(item.children) }; self
55
55
  end
56
56
 
57
- def remove(tag)
58
- remove_children_preserving_content(tag)
59
- end
60
-
61
57
  def inner_html
62
58
  @noko.inner_html
63
59
  end
data/lib/saper/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Saper
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -3,27 +3,57 @@ require './spec/spec_helper'
3
3
  describe Saper::Actions::AppendWith do
4
4
 
5
5
  let(:action) do
6
- Saper::Action.new(:append_with, '!')
6
+ Saper::Actions::AppendWith.new('!')
7
7
  end
8
8
 
9
- context ".accepts" do
10
- it "returns acceptable input types" do
11
- Saper::Actions::AppendWith.accepts.should == [:text]
9
+ context ".new(nil)" do
10
+ it "raises InvalidArgument" do
11
+ expect { Saper::Actions::AppendWith.new(nil) }.to raise_error(Saper::InvalidArgument)
12
12
  end
13
13
  end
14
14
 
15
- context ".new" do
16
- it "raises InvalidArgument when argument is missing" do
17
- expect { subject }.to raise_error(Saper::InvalidArgument)
15
+ context ".new(Number)" do
16
+ it "raises InvalidArgument" do
17
+ expect { Saper::Actions::AppendWith.new(2) }.to raise_error(Saper::InvalidArgument)
18
+ end
19
+ end
20
+
21
+ context ".new(String)" do
22
+ it "raises no errors" do
23
+ expect { Saper::Actions::AppendWith.new("!") }.to_not raise_error
24
+ end
25
+ it "returns Action instance" do
26
+ Saper::Actions::AppendWith.new("!").should be_a(Saper::Action)
18
27
  end
19
28
  end
20
29
 
21
- context "#run" do
22
- it "raises InvalidInput when input is nil" do
30
+ context "#run(nil)" do
31
+ it "raises InvalidInput" do
23
32
  expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
24
33
  end
25
- it "returns valid output when input is Text" do
26
- action.run('string').should == 'string!'
34
+ end
35
+
36
+ context "#run(String)" do
37
+ let(:input) do
38
+ 'string'
39
+ end
40
+ it "returns valid output" do
41
+ action.run(input).should == 'string!'
42
+ end
43
+ it "returns Text instance" do
44
+ action.run(input).should be_a(Saper::Items::Text)
45
+ end
46
+ end
47
+
48
+ context "#run(Text)" do
49
+ let(:input) do
50
+ Saper::Items::Text.new('string')
51
+ end
52
+ it "returns valid output" do
53
+ action.run(input).should == 'string!'
54
+ end
55
+ it "returns Text instance" do
56
+ action.run(input).should be_a(Saper::Items::Text)
27
57
  end
28
58
  end
29
59
 
@@ -3,21 +3,36 @@ require './spec/spec_helper'
3
3
  describe Saper::Actions::ConvertToHTML do
4
4
 
5
5
  let(:action) do
6
- Saper::Action.new(:convert_to_html)
6
+ Saper::Actions::ConvertToHTML.new
7
7
  end
8
8
 
9
- context ".accepts" do
10
- it "returns acceptable input types" do
11
- Saper::Actions::ConvertToHTML.accepts.should == [:text, :document]
9
+ context "#run(nil)" do
10
+ it "raises InvalidInput" do
11
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
12
12
  end
13
13
  end
14
14
 
15
- context "#run" do
16
- it "raises InvalidInput when input is nil" do
17
- expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
15
+ context "#run(String)" do
16
+ let(:input) do
17
+ '<p>test</p>'
18
+ end
19
+ it "raises no errors" do
20
+ expect { action.run(input) }.to_not raise_error
21
+ end
22
+ it "returns HTML instance" do
23
+ action.run(input).should be_a(Saper::Items::HTML)
24
+ end
25
+ end
26
+
27
+ context "#run(Text)" do
28
+ let(:input) do
29
+ Saper::Items::Text.new('<p>test</p>')
30
+ end
31
+ it "raises no errors" do
32
+ expect { action.run(input) }.to_not raise_error
18
33
  end
19
- it "raises no errors when input is a valid Text" do
20
- expect { action.run('<p>test</p>') }.to_not raise_error
34
+ it "returns HTML instance" do
35
+ action.run(input).should be_a(Saper::Items::HTML)
21
36
  end
22
37
  end
23
38
 
@@ -3,21 +3,36 @@ require './spec/spec_helper'
3
3
  describe Saper::Actions::ConvertToJSON do
4
4
 
5
5
  let(:action) do
6
- Saper::Action.new(:convert_to_json)
6
+ Saper::Actions::ConvertToJSON.new
7
7
  end
8
8
 
9
- context ".accepts" do
10
- it "returns acceptable input types" do
11
- Saper::Actions::ConvertToJSON.accepts.should == [:text, :document]
9
+ context "#run(nil)" do
10
+ it "raises InvalidInput" do
11
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
12
12
  end
13
13
  end
14
14
 
15
- context "#run" do
16
- it "raises InvalidInput when input is nil" do
17
- expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
15
+ context "#run(String)" do
16
+ let(:input) do
17
+ '{"test":"ok"}'
18
+ end
19
+ it "raises no errors" do
20
+ expect { action.run(input) }.to_not raise_error
21
+ end
22
+ it "returns JSON instance" do
23
+ action.run(input).should be_a(Saper::Items::JSON)
24
+ end
25
+ end
26
+
27
+ context "#run(String)" do
28
+ let(:input) do
29
+ Saper::Items::Text.new('{"test":"ok"}')
30
+ end
31
+ it "raises no errors" do
32
+ expect { action.run(input) }.to_not raise_error
18
33
  end
19
- it "raises no errors when input is a valid Text" do
20
- expect { action.run('{"test":"ok"}') }.to_not raise_error
34
+ it "returns JSON instance" do
35
+ action.run(input).should be_a(Saper::Items::JSON)
21
36
  end
22
37
  end
23
38
 
@@ -3,21 +3,24 @@ require './spec/spec_helper'
3
3
  describe Saper::Actions::ConvertToMarkdown do
4
4
 
5
5
  let(:action) do
6
- Saper::Action.new(:convert_to_markdown)
6
+ Saper::Actions::ConvertToMarkdown.new
7
7
  end
8
8
 
9
- context ".accepts" do
10
- it "returns acceptable input types" do
11
- Saper::Actions::ConvertToMarkdown.accepts.should == [:html]
9
+ context "#run(nil)" do
10
+ it "raises InvalidInput" do
11
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
12
12
  end
13
13
  end
14
14
 
15
- context "#run" do
16
- it "raises InvalidInput when input is nil" do
17
- expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
15
+ context "#run(HTML)" do
16
+ let(:input) do
17
+ Saper::Items::HTML.new('<p>test</p>')
18
+ end
19
+ it "returns valid output" do
20
+ action.run(input).should == 'test'
18
21
  end
19
- it "returns valid markdown when input is HTML" do
20
- action.run(Saper::Items::HTML.new('<p>test</p>')).should == 'test'
22
+ it "returns Markdown instance" do
23
+ action.run(input).should be_a(Saper::Items::Markdown)
21
24
  end
22
25
  end
23
26
 
@@ -1,36 +1,51 @@
1
1
  require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Actions::ConvertToTime do
4
-
4
+
5
5
  let(:action_without_tz) do
6
- Saper::Action.new(:convert_to_time, '%d-%m-%Y %H:%M')
6
+ Saper::Actions::ConvertToTime.new('%d-%m-%Y %H:%M')
7
7
  end
8
8
 
9
9
  let(:action_with_tz) do
10
- Saper::Action.new(:convert_to_time, '%d-%m-%Y %H:%M', 'Europe/Moscow')
10
+ Saper::Actions::ConvertToTime.new('%d-%m-%Y %H:%M', 'Europe/Moscow')
11
11
  end
12
12
 
13
- context ".new" do
14
- it "raises InvalidArgument when argument is missing" do
15
- expect { subject }.to raise_error(Saper::InvalidArgument)
13
+ context ".new(nil, nil)" do
14
+ it "raises InvalidArgument" do
15
+ expect { Saper::Actions::ConvertToTime.new }.to raise_error(Saper::InvalidArgument)
16
16
  end
17
17
  end
18
-
19
- context ".accepts" do
20
- it "returns acceptable input types" do
21
- Saper::Actions::ConvertToTime.accepts.should == [:text]
18
+
19
+ context ".new(format, nil)" do
20
+ it "raises no errors" do
21
+ expect { Saper::Actions::ConvertToTime.new("%H") }.to_not raise_error
22
22
  end
23
23
  end
24
24
 
25
- context "#run" do
26
- it "raises InvalidInput when input is nil" do
25
+ context "#run(nil)" do
26
+ it "raises InvalidInput" do
27
27
  expect { action_without_tz.run(nil) }.to raise_error(Saper::InvalidInput)
28
28
  end
29
- it "returns valid time when input is a valid date with timezone" do
30
- action_with_tz.run('19-04-2014 18:56').should == '2014-04-19T18:56:00+04:00'
29
+ end
30
+
31
+ context "#run(valid date)" do
32
+ let(:input) do
33
+ '19-04-2014 18:56'
34
+ end
35
+ it "returns valid output if TZ is set" do
36
+ action_with_tz.run(input).should == '2014-04-19T18:56:00+04:00'
37
+ end
38
+ it "returns valid output if TZ is missing" do
39
+ action_without_tz.run(input).should == '2014-04-19T18:56:00+00:00'
31
40
  end
32
- it "returns valid time when input is a valid date without timezone" do
33
- action_without_tz.run('19-04-2014 18:56').should == '2014-04-19T18:56:00+00:00'
41
+ it "returns Time instance" do
42
+ action_with_tz.run(input).should be_a(Saper::Items::Time)
43
+ end
44
+ end
45
+
46
+ context "#run(invalid date)" do
47
+ it "raises InvalidItem" do
48
+ expect { action_with_tz.run("ABCDEF") }.to raise_error(Saper::InvalidItem)
34
49
  end
35
50
  end
36
51
 
@@ -3,13 +3,7 @@ require './spec/spec_helper'
3
3
  describe Saper::Actions::ConvertToXML do
4
4
 
5
5
  let(:action) do
6
- Saper::Action.new(:convert_to_xml)
7
- end
8
-
9
- context ".accepts" do
10
- it "returns acceptable input types" do
11
- Saper::Actions::ConvertToXML.accepts.should == [:text, :document]
12
- end
6
+ Saper::Actions::ConvertToXML.new
13
7
  end
14
8
 
15
9
  context "#run" do
@@ -3,24 +3,20 @@ require './spec/spec_helper'
3
3
  describe Saper::Actions::CreateAtom do
4
4
 
5
5
  let(:action) do
6
- Saper::Action.new(:create_atom)
6
+ Saper::Actions::CreateAtom.new
7
7
  end
8
8
 
9
- let(:runtime) do
10
- Saper::Runtime.new
11
- end
12
-
13
- context ".accepts" do
14
- it "returns acceptable input types" do
15
- Saper::Actions::CreateAtom.accepts.should == [:atom, :document, :html, :json, :markdown, :nothing, :text, :time, :url, :xml]
9
+ context "#run(nil)" do
10
+ it "raises RuntimeMissing" do
11
+ expect { action.run(nil) }.to raise_error(Saper::RuntimeMissing)
16
12
  end
17
13
  end
18
14
 
19
- context "#run" do
20
- it "raises RuntimeMissing when runtime is missing" do
21
- expect { action.run(nil) }.to raise_error(Saper::RuntimeMissing)
15
+ context "#run(nil, Runtime)" do
16
+ let(:runtime) do
17
+ Saper::Runtime.new
22
18
  end
23
- it "raises no errors when runtime is defined and input is nil" do
19
+ it "raises no errors" do
24
20
  expect { action.run(nil, runtime) }.to_not raise_error
25
21
  end
26
22
  it "returns Saper::Atom" do
@@ -2,6 +2,14 @@ require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Actions::Fetch do
4
4
 
5
- it # TODO
5
+ let(:action) do
6
+ Saper::Actions::Fetch.new
7
+ end
8
+
9
+ context "#run(nil)" do
10
+ it "raises InvalidInput" do
11
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
12
+ end
13
+ end
6
14
 
7
15
  end
@@ -2,6 +2,51 @@ require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Actions::FindFirst do
4
4
 
5
- it # TODO
5
+ let(:action) do
6
+ Saper::Actions::FindFirst.new("//p")
7
+ end
8
+
9
+ context ".new(nil)" do
10
+ it "raises InvalidArgument" do
11
+ expect { Saper::Actions::FindFirst.new(nil) }.to raise_error(Saper::InvalidArgument)
12
+ end
13
+ end
14
+
15
+ context ".new(CSS)" do
16
+ it "raises no errors" do
17
+ expect { Saper::Actions::FindFirst.new("a") }.to_not raise_error
18
+ end
19
+ end
20
+
21
+ context "#run(nil)" do
22
+ it "raises InvalidInput" do
23
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
24
+ end
25
+ end
26
+
27
+ context "#run(HTML)" do
28
+ let(:input) do
29
+ Saper::Items::HTML.new('<div><p>test</p></div>')
30
+ end
31
+ it "returns HTML instance" do
32
+ action.run(input).should be_a(Saper::Items::HTML)
33
+ end
34
+ it "returns valid output" do
35
+ action.run(input).name.should == 'p'
36
+ end
37
+ end
38
+
39
+ context "#run(JSON)" do
40
+ let(:input) do
41
+ Saper::Items::JSON.new('{"p":{"a":0}}')
42
+ end
43
+ it "returns JSON instance" do
44
+ action.run(input).should be_a(Saper::Items::JSON)
45
+ end
46
+ it "returns valid output" do
47
+ action.run(input).to_s.should == '{"a":0}'
48
+ end
49
+
50
+ end
6
51
 
7
52
  end
@@ -2,6 +2,50 @@ require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Actions::Find do
4
4
 
5
- it # TODO
5
+ let(:action) do
6
+ Saper::Actions::Find.new("//p")
7
+ end
8
+
9
+ context ".new(nil)" do
10
+ it "raises InvalidArgument" do
11
+ expect { Saper::Actions::Find.new(nil) }.to raise_error(Saper::InvalidArgument)
12
+ end
13
+ end
14
+
15
+ context ".new(CSS)" do
16
+ it "raises no errors" do
17
+ expect { Saper::Actions::Find.new("a") }.to_not raise_error
18
+ end
19
+ end
20
+
21
+ context "#run(nil)" do
22
+ it "raises InvalidInput" do
23
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
24
+ end
25
+ end
26
+
27
+ context "#run(HTML)" do
28
+ let(:input) do
29
+ Saper::Items::HTML.new('<div><p>test</p><p>test</p></div>')
30
+ end
31
+ it "returns an array of results" do
32
+ action.run(input).should be_a(Array)
33
+ end
34
+ it "returns an array with first item an HTML instance" do
35
+ action.run(input).first.should be_a(Saper::Items::HTML)
36
+ end
37
+ end
38
+
39
+ context "#run(JSON)" do
40
+ let(:input) do
41
+ Saper::Items::JSON.new('{"p":{"a":0},"z":{"a":1}}')
42
+ end
43
+ it "returns an array of results" do
44
+ action.run(input).should be_a(Array)
45
+ end
46
+ it "returns an array with first item a JSON instance" do
47
+ action.run(input).first.should be_a(Saper::Items::JSON)
48
+ end
49
+ end
6
50
 
7
51
  end
@@ -6,12 +6,6 @@ describe Saper::Actions::PrependWith do
6
6
  Saper::Action.new(:prepend_with, '!')
7
7
  end
8
8
 
9
- context ".accepts" do
10
- it "returns acceptable input types" do
11
- Saper::Actions::AppendWith.accepts.should == [:text]
12
- end
13
- end
14
-
15
9
  context ".new" do
16
10
  it "raises InvalidArgument when argument is missing" do
17
11
  expect { subject }.to raise_error(Saper::InvalidArgument)
@@ -2,6 +2,24 @@ require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Items::Document do
4
4
 
5
- it # TODO
5
+ context ".new" do
6
6
 
7
+ end
8
+
9
+ context "#to_html" do
10
+
11
+ end
12
+
13
+ context "#to_markdown" do
14
+
15
+ end
16
+
17
+ context "#to_text" do
18
+
19
+ end
20
+
21
+ context "#to_xml" do
22
+
23
+ end
24
+
7
25
  end
@@ -2,6 +2,40 @@ require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Items::HTML do
4
4
 
5
- it # TODO
5
+ context ".new" do
6
6
 
7
+ end
8
+
9
+ context "#[]" do
10
+
11
+ end
12
+
13
+ context "#find" do
14
+
15
+ end
16
+
17
+ context "#find_all" do
18
+
19
+ end
20
+
21
+ context "#inner_html" do
22
+
23
+ end
24
+
25
+ context "#inner_text" do
26
+
27
+ end
28
+
29
+ context "#remove_children_preserving_content" do
30
+
31
+ end
32
+
33
+ context "#remove_children_with_content" do
34
+
35
+ end
36
+
37
+ context "#to_markdown" do
38
+
39
+ end
40
+
7
41
  end
@@ -2,6 +2,16 @@ require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Items::JSON do
4
4
 
5
- it # TODO
5
+ context ".new" do
6
6
 
7
+ end
8
+
9
+ context "#find" do
10
+
11
+ end
12
+
13
+ context ".find_all" do
14
+
15
+ end
16
+
7
17
  end
@@ -14,4 +14,32 @@ describe Saper::Items::Text do
14
14
  end
15
15
  end
16
16
 
17
- end
17
+ context "#gsub" do
18
+
19
+ end
20
+
21
+ context "#split" do
22
+
23
+ end
24
+
25
+ context "#to_json" do
26
+
27
+ end
28
+
29
+ context "#to_html" do
30
+
31
+ end
32
+
33
+ context "#to_s" do
34
+
35
+ end
36
+
37
+ context "#to_time" do
38
+
39
+ end
40
+
41
+ context "#to_xml" do
42
+
43
+ end
44
+
45
+ end
@@ -2,6 +2,12 @@ require './spec/spec_helper'
2
2
 
3
3
  describe Saper::Items::URL do
4
4
 
5
- it # TODO
5
+ context ".new" do
6
6
 
7
+ end
8
+
9
+ context "#to_s" do
10
+
11
+ end
12
+
7
13
  end
@@ -14,4 +14,32 @@ describe Saper::Items::XML do
14
14
  end
15
15
  end
16
16
 
17
+ context "#[]" do
18
+
19
+ end
20
+
21
+ context "#find" do
22
+
23
+ end
24
+
25
+ context "#find_all" do
26
+
27
+ end
28
+
29
+ context "#inner_html" do
30
+
31
+ end
32
+
33
+ context "#inner_text" do
34
+
35
+ end
36
+
37
+ context "#remove_children_preserving_content" do
38
+
39
+ end
40
+
41
+ context "#remove_children_with_content" do
42
+
43
+ end
44
+
17
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Serebryakov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-04 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize