saper 0.5.0

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.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +126 -0
  4. data/Rakefile +17 -0
  5. data/bin/saper +60 -0
  6. data/lib/lib/json_search.rb +54 -0
  7. data/lib/lib/mechanize.rb +26 -0
  8. data/lib/lib/nokogiri.rb +12 -0
  9. data/lib/saper.rb +37 -0
  10. data/lib/saper/actions/append_with.rb +14 -0
  11. data/lib/saper/actions/convert_to_html.rb +14 -0
  12. data/lib/saper/actions/convert_to_json.rb +14 -0
  13. data/lib/saper/actions/convert_to_markdown.rb +13 -0
  14. data/lib/saper/actions/convert_to_time.rb +15 -0
  15. data/lib/saper/actions/convert_to_xml.rb +14 -0
  16. data/lib/saper/actions/create_atom.rb +18 -0
  17. data/lib/saper/actions/fetch.rb +17 -0
  18. data/lib/saper/actions/find.rb +18 -0
  19. data/lib/saper/actions/find_first.rb +16 -0
  20. data/lib/saper/actions/get_attribute.rb +15 -0
  21. data/lib/saper/actions/get_contents.rb +14 -0
  22. data/lib/saper/actions/get_text.rb +14 -0
  23. data/lib/saper/actions/prepend_with.rb +14 -0
  24. data/lib/saper/actions/remove_after.rb +14 -0
  25. data/lib/saper/actions/remove_before.rb +14 -0
  26. data/lib/saper/actions/remove_matching.rb +14 -0
  27. data/lib/saper/actions/remove_tags.rb +15 -0
  28. data/lib/saper/actions/replace.rb +15 -0
  29. data/lib/saper/actions/run_recipe.rb +24 -0
  30. data/lib/saper/actions/run_recipe_and_save.rb +22 -0
  31. data/lib/saper/actions/save.rb +14 -0
  32. data/lib/saper/actions/select_matching.rb +14 -0
  33. data/lib/saper/actions/set_input.rb +19 -0
  34. data/lib/saper/actions/skip_tags.rb +15 -0
  35. data/lib/saper/actions/split.rb +24 -0
  36. data/lib/saper/arguments/attribute.rb +11 -0
  37. data/lib/saper/arguments/recipe.rb +42 -0
  38. data/lib/saper/arguments/text.rb +11 -0
  39. data/lib/saper/arguments/timezone.rb +11 -0
  40. data/lib/saper/arguments/variable.rb +11 -0
  41. data/lib/saper/arguments/xpath.rb +11 -0
  42. data/lib/saper/core/action.rb +209 -0
  43. data/lib/saper/core/argument.rb +106 -0
  44. data/lib/saper/core/browser.rb +87 -0
  45. data/lib/saper/core/dsl.rb +68 -0
  46. data/lib/saper/core/error.rb +47 -0
  47. data/lib/saper/core/item.rb +70 -0
  48. data/lib/saper/core/keychain.rb +18 -0
  49. data/lib/saper/core/logger.rb +74 -0
  50. data/lib/saper/core/namespace.rb +139 -0
  51. data/lib/saper/core/recipe.rb +134 -0
  52. data/lib/saper/core/runtime.rb +237 -0
  53. data/lib/saper/core/type.rb +45 -0
  54. data/lib/saper/items/atom.rb +64 -0
  55. data/lib/saper/items/document.rb +66 -0
  56. data/lib/saper/items/html.rb +85 -0
  57. data/lib/saper/items/json.rb +67 -0
  58. data/lib/saper/items/markdown.rb +36 -0
  59. data/lib/saper/items/nothing.rb +15 -0
  60. data/lib/saper/items/text.rb +54 -0
  61. data/lib/saper/items/time.rb +42 -0
  62. data/lib/saper/items/url.rb +34 -0
  63. data/lib/saper/items/xml.rb +79 -0
  64. data/lib/saper/version.rb +3 -0
  65. data/spec/actions/append_with_spec.rb +30 -0
  66. data/spec/actions/convert_to_html_spec.rb +24 -0
  67. data/spec/actions/convert_to_json_spec.rb +24 -0
  68. data/spec/actions/convert_to_markdown_spec.rb +24 -0
  69. data/spec/actions/convert_to_time_spec.rb +37 -0
  70. data/spec/actions/convert_to_xml_spec.rb +24 -0
  71. data/spec/actions/create_atom_spec.rb +31 -0
  72. data/spec/actions/fetch_spec.rb +7 -0
  73. data/spec/actions/find_first_spec.rb +7 -0
  74. data/spec/actions/find_spec.rb +7 -0
  75. data/spec/actions/get_attribute_spec.rb +7 -0
  76. data/spec/actions/get_contents.rb +7 -0
  77. data/spec/actions/get_text.rb +7 -0
  78. data/spec/actions/prepend_with_spec.rb +30 -0
  79. data/spec/actions/remove_after.rb +7 -0
  80. data/spec/actions/remove_before.rb +7 -0
  81. data/spec/actions/replace_spec.rb +7 -0
  82. data/spec/actions/run_recipe_and_save_spec.tmp.rb +52 -0
  83. data/spec/actions/run_recipe_spec.tmp.rb +53 -0
  84. data/spec/actions/save_spec.rb +7 -0
  85. data/spec/actions/select_matching_spec.rb +7 -0
  86. data/spec/actions/set_input_spec.rb +7 -0
  87. data/spec/actions/skip_tags_spec.rb +7 -0
  88. data/spec/actions/split_spec.rb +7 -0
  89. data/spec/core/action_spec.rb +151 -0
  90. data/spec/core/argument_spec.rb +79 -0
  91. data/spec/core/browser_spec.rb +7 -0
  92. data/spec/core/dsl_spec.rb +7 -0
  93. data/spec/core/item_spec.rb +7 -0
  94. data/spec/core/keychain_spec.rb +7 -0
  95. data/spec/core/logger_spec.rb +7 -0
  96. data/spec/core/namespace_spec.rb +18 -0
  97. data/spec/core/recipe_spec.rb +81 -0
  98. data/spec/core/runtime_spec.rb +165 -0
  99. data/spec/core/type_spec.rb +7 -0
  100. data/spec/items/atom_spec.rb +7 -0
  101. data/spec/items/document_spec.rb +7 -0
  102. data/spec/items/html_spec.rb +7 -0
  103. data/spec/items/json_spec.rb +7 -0
  104. data/spec/items/markdown_spec.rb +7 -0
  105. data/spec/items/nothing_spec.rb +7 -0
  106. data/spec/items/text_spec.rb +17 -0
  107. data/spec/items/time_spec.rb +7 -0
  108. data/spec/items/url_spec.rb +7 -0
  109. data/spec/items/xml_spec.rb +17 -0
  110. data/spec/spec_helper.rb +22 -0
  111. metadata +355 -0
@@ -0,0 +1,79 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Argument do
4
+
5
+ context ".new" do
6
+ context "when required value is nil" do
7
+ it "raises InvalidArgument" do
8
+ expect { Saper::Argument.new(:text, :value => nil) }.to raise_error(Saper::InvalidArgument)
9
+ end
10
+ end
11
+ context "when required value is missing" do
12
+ it "raises InvalidArgument" do
13
+ expect { Saper::Argument.new(:text) }.to raise_error(Saper::InvalidArgument)
14
+ end
15
+ end
16
+ context "when required value is invalid" do
17
+ it "raises InvalidArgument" do
18
+ expect { Saper::Argument.new(:text, :value => 1) }.to raise_error(Saper::InvalidArgument)
19
+ end
20
+ end
21
+ context "when required value is valid" do
22
+ it "raises no exceptions" do
23
+ expect { Saper::Argument.new(:text, :value => "text") }.to_not raise_error
24
+ end
25
+ end
26
+ context "when optional value is missing" do
27
+ it "raises no exceptions" do
28
+ expect { Saper::Argument.new(:text, :optional => true) }.to_not raise_error
29
+ end
30
+ end
31
+ context "when optional value is invalid" do
32
+ it "raises InvalidArgument" do
33
+ expect { Saper::Argument.new(:text, :optional => true, :value => 1) }.to raise_error(Saper::InvalidArgument)
34
+ end
35
+ end
36
+ context "when optional value is valid" do
37
+ it "returns Action instance" do
38
+ expect { Saper::Argument.new(:text, :optional => true, :value => "text") }.to_not raise_error
39
+ end
40
+ end
41
+ end
42
+
43
+ context "#set" do
44
+ context "when required value is invalid" do
45
+ it "raises InvalidArgument" do
46
+ expect { Saper::Argument.new(:text, :value => "text").set(1) }.to raise_error(Saper::InvalidArgument)
47
+ end
48
+ end
49
+ context "when optional value is invalid" do
50
+ it "raises InvalidArgument" do
51
+ expect { Saper::Argument.new(:text, :optional => true, :value => "text").set(1) }.to raise_error(Saper::InvalidArgument)
52
+ end
53
+ end
54
+ context "when value is valid" do
55
+ it "returns self" do
56
+ Saper::Argument.new(:text, :optional => true).set("text").should be_a(Saper::Argument)
57
+ end
58
+ end
59
+ end
60
+
61
+ context "#value" do
62
+ it "returns nil by default" do
63
+ Saper::Argument.new(:text, :optional => true).value.should be_nil
64
+ end
65
+ it "returns stored value" do
66
+ Saper::Argument.new(:text, :value => "text").value.should == "text"
67
+ end
68
+ end
69
+
70
+ context "#optional?" do
71
+ it "returns false by default" do
72
+ Saper::Argument.new(:text, :value => "text").should_not be_optional
73
+ end
74
+ it "returns true if optionality flag is set" do
75
+ Saper::Argument.new(:text, :optional => true).should be_optional
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Browser do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::DSL::Recipe do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Item do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Keychain do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Logger do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,18 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Namespace do
4
+
5
+ let(:namespace) do
6
+ Saper::Namespace.parse do |ns|
7
+ recipe :child do
8
+ prepend_with 'test-'
9
+ end
10
+ recipe :parent do
11
+ run_recipe_and_save :variable, :child
12
+ end
13
+ end
14
+ end
15
+
16
+ it # TODO
17
+
18
+ end
@@ -0,0 +1,81 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Recipe do
4
+
5
+ context ".unserialize(Hash)" do
6
+ it "returns a Recipe" do
7
+ Saper::Recipe.unserialize({ :id => 0 }).should be_a(Saper::Recipe)
8
+ end
9
+ end
10
+
11
+ context ".unserialize(Array)" do
12
+ it "returns an Array" do
13
+ Saper::Recipe.unserialize([{ :id => 0 }, { :id => 1 }]).should be_a(Array)
14
+ end
15
+ end
16
+
17
+ context ".unserialize(nil)" do
18
+ it "raises InvalidRecipe" do
19
+ expect { Saper::Recipe.unserialize("invalid argument") }.to raise_error(Saper::InvalidRecipe)
20
+ end
21
+ end
22
+
23
+ context "#id" do
24
+ it "returns recipe ID" do
25
+ subject.id.should be_a(String)
26
+ end
27
+ end
28
+
29
+ context "#actions" do
30
+ it "returns an empty array by default" do
31
+ subject.actions.should == []
32
+ end
33
+ end
34
+
35
+ let(:action) do
36
+ Saper::Action.new(:append_with, "!")
37
+ end
38
+
39
+ context "#<<" do
40
+ it "returns self with valid action" do
41
+ (subject << action).should be_a(Saper::Recipe)
42
+ end
43
+ it "raises ActionExpected with invalid argument" do
44
+ expect { subject << 'not an action' }.to raise_error(Saper::ActionExpected)
45
+ end
46
+ end
47
+
48
+ context "#push" do
49
+ it "returns self with valid action" do
50
+ subject.push(action).should be_a(Saper::Recipe)
51
+ end
52
+ it "raises ActionExpected with invalid argument" do
53
+ expect { subject.push('not an action') }.to raise_error(Saper::ActionExpected)
54
+ end
55
+ end
56
+
57
+ context "#run" do
58
+ it "returns self" do
59
+ subject.run.should be_a(Saper::Runtime)
60
+ end
61
+ end
62
+
63
+ context "#serialize" do
64
+ it "returns a Hash" do
65
+ subject.serialize.should be_a(Hash)
66
+ end
67
+ end
68
+
69
+ context "#to_string" do
70
+ it "returns a string" do
71
+ subject.to_string.should be_a(String)
72
+ end
73
+ end
74
+
75
+ context "#to_json" do
76
+ it "returns a string" do
77
+ subject.to_json.should be_a(String)
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,165 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Runtime do
4
+
5
+ context "#browser" do
6
+ it "returns Browser instance" do
7
+ subject.browser.should be_a(Saper::Browser)
8
+ end
9
+ end
10
+
11
+ context "#bytes_received" do
12
+ it "returns zero by default" do
13
+ subject.bytes_received.should == 0
14
+ end
15
+ end
16
+
17
+ context "#bytes_sent" do
18
+ it "returns zero by default" do
19
+ subject.bytes_sent.should == 0
20
+ end
21
+ end
22
+
23
+ context "#http_requests" do
24
+ it "returns zero by default" do
25
+ subject.http_requests.should == 0
26
+ end
27
+ end
28
+
29
+ context "#keychain" do
30
+ it "returns Keychain instance" do
31
+ subject.keychain.should be_a(Saper::Keychain)
32
+ end
33
+ end
34
+
35
+ context "#credentials" do
36
+ it "returns nil if serivce not found" do
37
+ subject.credentials(:services).should == nil
38
+ end
39
+ end
40
+
41
+ #context "#namespace" do
42
+ # it "returns nil" do
43
+ # subject.namespace.should be_nil
44
+ # end
45
+ #end
46
+
47
+ context "#depth" do
48
+ it "returns zero by default" do
49
+ subject.depth.should == 0
50
+ end
51
+ end
52
+
53
+ context "#descendants" do
54
+ it "returns self by default" do
55
+ subject.descendants.should == [subject]
56
+ end
57
+ end
58
+
59
+ context "#results" do
60
+ it "returns nil by default" do
61
+ subject.results.should == nil
62
+ end
63
+ end
64
+
65
+ context "#results" do
66
+ it "returns input by default" do
67
+ Saper::Runtime.new([], "input").results.should == "input"
68
+ end
69
+ end
70
+
71
+ context "#[]" do
72
+ it "returns nil if variable is not defined" do
73
+ subject[:varname].should be_nil
74
+ end
75
+ end
76
+
77
+ context "#[]=" do
78
+ it "stores variale value" do
79
+ subject[:var] = 'OK'; subject[:var].should == 'OK'
80
+ end
81
+ end
82
+
83
+ context "#variables" do
84
+ it "returns list of variable names" do
85
+ subject[:var] = 'OK'; subject.variables.should == { :var => 'OK' }
86
+ end
87
+ end
88
+
89
+ let(:simple_recipe) do
90
+ Saper::Recipe.parse do
91
+ convert_to_html
92
+ find_first "p"
93
+ get_contents
94
+ append_with "!"
95
+ end
96
+ end
97
+
98
+ context "#results" do
99
+ context "with failed single-output recipe" do
100
+ it "returns nil" do
101
+ Saper::Runtime.new(simple_recipe, nil).results.should == nil
102
+ end
103
+ end
104
+ context "with successful single-output recipe" do
105
+ it "returns result" do
106
+ Saper::Runtime.new(simple_recipe, "<p>a</p><p>b</p>").results.should == "a!"
107
+ end
108
+ end
109
+ end
110
+
111
+ let(:multiple_recipe) do
112
+ Saper::Recipe.parse do
113
+ convert_to_html
114
+ find "p"
115
+ get_contents
116
+ append_with "!"
117
+ end
118
+ end
119
+
120
+ context "#results" do
121
+ context "with failed multiple-output recipe" do
122
+ it "returns empty array" do
123
+ Saper::Runtime.new(multiple_recipe, nil).results.should == []
124
+ end
125
+ end
126
+ context "with successful multiple-output recipe" do
127
+ it "returns array with results" do
128
+ Saper::Runtime.new(multiple_recipe, "<p>a</p><p>b</p>").results.should == ["a!", "b!"]
129
+ end
130
+ end
131
+ end
132
+
133
+ let(:namespaced_recipes) do
134
+ Saper::Namespace.parse do |ns|
135
+ recipe :child_multiple do
136
+ find "p"
137
+ get_contents
138
+ end
139
+ recipe :child_single do
140
+ find_first "p"
141
+ get_contents
142
+ end
143
+ recipe :parent_multiple do
144
+ run_recipe :child_multiple
145
+ end
146
+ recipe :parent_single do
147
+ run_recipe :child_single
148
+ end
149
+ end
150
+ end
151
+
152
+ context "#results" do
153
+ context "with embedded multiple-output recipe" do
154
+ it "returns correct ouput" do
155
+ Saper::Runtime.new(namespaced_recipes[:parent_multiple], "<p>a</p><p>b</p>").results.should == ['a', 'b']
156
+ end
157
+ end
158
+ context "with embedded single-output recipe" do
159
+ it "returns correct ouput" do
160
+ Saper::Runtime.new(namespaced_recipes[:parent_single], "<p>a</p><p>b</p>").results.should == 'a'
161
+ end
162
+ end
163
+ end
164
+
165
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Type do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Items::Atom do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Items::Document do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Items::HTML do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Items::JSON do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Items::Markdown do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Items::Nothing do
4
+
5
+ it # TODO
6
+
7
+ end