saper 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
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,37 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::ConvertToTime do
4
+
5
+ let(:action_without_tz) do
6
+ Saper::Action.new(:convert_to_time, '%d-%m-%Y %H:%M')
7
+ end
8
+
9
+ let(:action_with_tz) do
10
+ Saper::Action.new(:convert_to_time, '%d-%m-%Y %H:%M', 'Europe/Moscow')
11
+ end
12
+
13
+ context ".new" do
14
+ it "raises InvalidArgument when argument is missing" do
15
+ expect { subject }.to raise_error(Saper::InvalidArgument)
16
+ end
17
+ end
18
+
19
+ context ".accepts" do
20
+ it "returns acceptable input types" do
21
+ Saper::Actions::ConvertToTime.accepts.should == [:text]
22
+ end
23
+ end
24
+
25
+ context "#run" do
26
+ it "raises InvalidInput when input is nil" do
27
+ expect { action_without_tz.run(nil) }.to raise_error(Saper::InvalidInput)
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'
31
+ 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'
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,24 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::ConvertToXML do
4
+
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
13
+ end
14
+
15
+ context "#run" do
16
+ it "raises InvalidInput when input is nil" do
17
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
18
+ end
19
+ it "raises no errors when input is a valid Text" do
20
+ expect { action.run('<root>test</root>') }.to_not raise_error
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,31 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::CreateAtom do
4
+
5
+ let(:action) do
6
+ Saper::Action.new(:create_atom)
7
+ end
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]
16
+ end
17
+ end
18
+
19
+ context "#run" do
20
+ it "raises RuntimeMissing when runtime is missing" do
21
+ expect { action.run(nil) }.to raise_error(Saper::RuntimeMissing)
22
+ end
23
+ it "raises no errors when runtime is defined and input is nil" do
24
+ expect { action.run(nil, runtime) }.to_not raise_error
25
+ end
26
+ it "returns Saper::Atom" do
27
+ action.run(nil, runtime).should be_a(Saper::Items::Atom)
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::Fetch do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::FindFirst do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::Find do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::GetAttribute do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::GetContents do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::GetText do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,30 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::PrependWith do
4
+
5
+ let(:action) do
6
+ Saper::Action.new(:prepend_with, '!')
7
+ end
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
+ context ".new" do
16
+ it "raises InvalidArgument when argument is missing" do
17
+ expect { subject }.to raise_error(Saper::InvalidArgument)
18
+ end
19
+ end
20
+
21
+ context "#run" do
22
+ it "raises InvalidInput when input is nil" do
23
+ expect { action.run(nil) }.to raise_error(Saper::InvalidInput)
24
+ end
25
+ it "returns valid output when input is Text" do
26
+ action.run('string').should == '!string'
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::RemoveAfter do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::RemoveBefore do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::Replace do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,52 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::RunRecipeAndSave 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
+ context "when argument is missing" do
17
+ it "raises InvalidArgument" do
18
+ expect { Saper::Action.new(:run_recipe_and_save).run }.to raise_error(Saper::InvalidArgument)
19
+ end
20
+ end
21
+
22
+ context "when input is invalid" do
23
+ let(:runtime) do
24
+ namespace.run(:parent, nil)
25
+ end
26
+ it "raises no errors" do
27
+ expect { runtime }.to_not raise_error
28
+ end
29
+ it "returns valid output" do
30
+ runtime.results.should == nil
31
+ end
32
+ it "sets error flag in runtime" do
33
+ runtime.error?.should be_true
34
+ end
35
+ it "records error in runtime" do
36
+ runtime.error.should be_a(Saper::InvalidInput)
37
+ end
38
+ end
39
+
40
+ context "when input and arguments are valid" do
41
+ let(:runtime) do
42
+ Saper::Action.new(:run_recipe_and_save, :variable, :child).run('string', :namespace => namespace)
43
+ end
44
+ it "raises no errors" do
45
+ expect { runtime }.to_not raise_error
46
+ end
47
+ it "returns valid output" do
48
+ runtime.results.should == ['test-string']
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,53 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::RunRecipe do
4
+
5
+ let(:child) do
6
+ Saper::Recipe.new do |recipe|
7
+ recipe << Saper::Action.new(:prepend_with, 'test-')
8
+ end
9
+ end
10
+
11
+ let(:namespace) do
12
+ Saper::Namespace.new do |ns|
13
+ ns[:child] = child
14
+ end
15
+ end
16
+
17
+ context "when argument is missing" do
18
+ it "raises InvalidArgument" do
19
+ expect { Saper::Action.new(:run_recipe).run }.to raise_error(Saper::InvalidArgument)
20
+ end
21
+ end
22
+
23
+ context "when input is invalid" do
24
+ let(:runtime) do
25
+ Saper::Action.new(:run_recipe, :child).run(nil, :namespace => namespace)
26
+ end
27
+ it "raises no errors" do
28
+ expect { runtime }.to_not raise_error
29
+ end
30
+ it "returns valid output" do
31
+ runtime.results.should == nil
32
+ end
33
+ it "sets error flag in runtime" do
34
+ runtime.error?.should be_true
35
+ end
36
+ it "records error in runtime" do
37
+ runtime.error.should be_a(Saper::InvalidInput)
38
+ end
39
+ end
40
+
41
+ context "when input and arguments are valid" do
42
+ let(:runtime) do
43
+ Saper::Action.new(:run_recipe, :child).run('string', :namespace => namespace)
44
+ end
45
+ it "raises no errors" do
46
+ expect { runtime }.to_not raise_error
47
+ end
48
+ it "returns valid output" do
49
+ runtime.results.should == 'test-string'
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::Save do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::SelectMatching do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::SetInput do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::SkipTags do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Actions::Split do
4
+
5
+ it # TODO
6
+
7
+ end
@@ -0,0 +1,151 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Saper::Action do
4
+
5
+ context ".[]" do
6
+ it "raises ActionNotFound with unrecognized type" do
7
+ expect { Saper::Action[:unknown] }.to raise_error(Saper::ActionNotFound)
8
+ end
9
+ end
10
+
11
+ context ".new with unrecognized type" do
12
+ it "raises ActionNotFound" do
13
+ expect { Saper::Action.new(:unknown) }.to raise_error(Saper::ActionNotFound)
14
+ end
15
+ end
16
+
17
+ context ".accepts" do
18
+ it "returns an empty array" do
19
+ Saper::Action.accepts.should == []
20
+ end
21
+ end
22
+
23
+ context ".unserialize(Hash)" do
24
+ it "returns Action instance" do
25
+ Saper::Action.unserialize({ :type => :test_action }).should be_a(Saper::Action)
26
+ end
27
+ end
28
+
29
+ context ".unserialize(Array)" do
30
+ it "returns an Array" do
31
+ Saper::Action.unserialize([{ :type => :test_action }, { :type => :test_action }]).should be_a(Array)
32
+ end
33
+ end
34
+
35
+ context ".unserialize(nil)" do
36
+ it "raises InvalidAction" do
37
+ expect { Saper::Action.unserialize(nil) }.to raise_error(Saper::InvalidAction)
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ describe TestAction do
44
+
45
+ context ".type" do
46
+ it "returns underscored class name" do
47
+ TestAction.type.should == 'test_action'
48
+ end
49
+ end
50
+
51
+ context ".accepts" do
52
+ it "returns an array of acceptable input types" do
53
+ TestAction.accepts.should == [:text]
54
+ end
55
+ end
56
+
57
+ context ".[]" do
58
+ it "returns new class" do
59
+ Saper::Action[:test_action].should == TestAction
60
+ end
61
+ end
62
+
63
+ context ".new" do
64
+ it "returns an instance of new class" do
65
+ Saper::Action.new(:test_action).should be_a(TestAction)
66
+ end
67
+ end
68
+
69
+ context "#name" do
70
+ it "returns human readable name" do
71
+ TestAction.new.name.should == 'Test Action'
72
+ end
73
+ end
74
+
75
+ context "#options" do
76
+ it "returns empty Hash by default" do
77
+ TestAction.new.options.should == {}
78
+ end
79
+ it "returns Hash when options are set" do
80
+ TestAction.new(:test => true).options.should == { :test => true }
81
+ end
82
+ end
83
+
84
+ context "#namespace" do
85
+ it "returns nil by default" do
86
+ TestAction.new.namespace.should be_nil
87
+ end
88
+ it "returns Saper::Namespace when set" do
89
+ TestAction.new(:namespace => Saper::Namespace.new).namespace.should be_a(Saper::Namespace)
90
+ end
91
+ end
92
+
93
+ context "#run with valid input" do
94
+ it "returns input by default" do
95
+ TestAction.new.run("OK").should == 'OK'
96
+ end
97
+ end
98
+
99
+ context "#run with invalid input" do
100
+ it "raises InvalidInput" do
101
+ expect { TestAction.new.run(nil) }.to raise_error(Saper::InvalidInput)
102
+ end
103
+ end
104
+
105
+ context "#serialize" do
106
+ it "returns a Hash" do
107
+ TestAction.new.serialize.should be_a(Hash)
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ describe ActionWithMandatoryArgument do
114
+
115
+ context ".new" do
116
+ it "raises InvalidArgument when argument is missing" do
117
+ expect { ActionWithMandatoryArgument.new }.to raise_error(Saper::InvalidArgument)
118
+ end
119
+ it "raises no errors when argument is present" do
120
+ expect { ActionWithMandatoryArgument.new("text") }.to_not raise_error
121
+ end
122
+ end
123
+
124
+ context "#run with valid input" do
125
+ it "returns valid output" do
126
+ ActionWithMandatoryArgument.new("append this").run("with that").should == 'append this with that'
127
+ end
128
+ end
129
+
130
+ context "#run with invalid input" do
131
+ it "raises InvalidInput" do
132
+ expect { ActionWithMandatoryArgument.new("text").run(nil) }.to raise_error(Saper::InvalidInput)
133
+ end
134
+ end
135
+
136
+ end
137
+
138
+ describe ActionWithOptionalArgument do
139
+
140
+ context ".new" do
141
+ it "raises no errors when argument is missing" do
142
+ expect { ActionWithOptionalArgument.new }.to_not raise_error
143
+ end
144
+ it "raises no errors when argument is present" do
145
+ expect { ActionWithOptionalArgument.new("text") }.to_not raise_error
146
+ end
147
+ end
148
+
149
+ end
150
+
151
+