mbrao 1.0.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 (73) hide show
  1. data/.gitignore +6 -0
  2. data/.travis-gemfile +13 -0
  3. data/.travis.yml +8 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +21 -0
  6. data/README.md +60 -0
  7. data/Rakefile +11 -0
  8. data/doc/ActionView/Template/Handlers/MbraoTemplate.html +568 -0
  9. data/doc/ActionView/Template/Handlers.html +125 -0
  10. data/doc/HTML/Pipeline/KramdownFilter.html +354 -0
  11. data/doc/HTML/Pipeline.html +140 -0
  12. data/doc/HTML.html +125 -0
  13. data/doc/Mbrao/Author.html +1402 -0
  14. data/doc/Mbrao/Content.html +4779 -0
  15. data/doc/Mbrao/ContentPublicInterface.html +658 -0
  16. data/doc/Mbrao/Exceptions/InvalidDate.html +133 -0
  17. data/doc/Mbrao/Exceptions/InvalidMetadata.html +133 -0
  18. data/doc/Mbrao/Exceptions/Parsing.html +133 -0
  19. data/doc/Mbrao/Exceptions/Rendering.html +133 -0
  20. data/doc/Mbrao/Exceptions/UnavailableLocalization.html +133 -0
  21. data/doc/Mbrao/Exceptions/Unimplemented.html +133 -0
  22. data/doc/Mbrao/Exceptions/UnknownEngine.html +133 -0
  23. data/doc/Mbrao/Exceptions.html +125 -0
  24. data/doc/Mbrao/Parser.html +418 -0
  25. data/doc/Mbrao/ParsingEngines/Base.html +658 -0
  26. data/doc/Mbrao/ParsingEngines/PlainText.html +571 -0
  27. data/doc/Mbrao/ParsingEngines.html +127 -0
  28. data/doc/Mbrao/PublicInterface/ClassMethods.html +1727 -0
  29. data/doc/Mbrao/PublicInterface.html +134 -0
  30. data/doc/Mbrao/RenderingEngines/Base.html +280 -0
  31. data/doc/Mbrao/RenderingEngines/HtmlPipeline.html +873 -0
  32. data/doc/Mbrao/RenderingEngines.html +127 -0
  33. data/doc/Mbrao/Validations/ClassMethods.html +692 -0
  34. data/doc/Mbrao/Validations.html +134 -0
  35. data/doc/Mbrao/Version.html +189 -0
  36. data/doc/Mbrao.html +130 -0
  37. data/doc/_index.html +395 -0
  38. data/doc/class_list.html +53 -0
  39. data/doc/css/common.css +1 -0
  40. data/doc/css/full_list.css +57 -0
  41. data/doc/css/style.css +338 -0
  42. data/doc/file.README.html +135 -0
  43. data/doc/file_list.html +55 -0
  44. data/doc/frames.html +28 -0
  45. data/doc/index.html +135 -0
  46. data/doc/js/app.js +214 -0
  47. data/doc/js/full_list.js +173 -0
  48. data/doc/js/jquery.js +4 -0
  49. data/doc/method_list.html +516 -0
  50. data/doc/top-level-namespace.html +112 -0
  51. data/lib/mbrao/author.rb +61 -0
  52. data/lib/mbrao/content.rb +300 -0
  53. data/lib/mbrao/exceptions.rb +38 -0
  54. data/lib/mbrao/integrations/rails.rb +51 -0
  55. data/lib/mbrao/parser.rb +276 -0
  56. data/lib/mbrao/parsing_engines/base.rb +51 -0
  57. data/lib/mbrao/parsing_engines/plain_text.rb +187 -0
  58. data/lib/mbrao/rendering_engines/base.rb +22 -0
  59. data/lib/mbrao/rendering_engines/html_pipeline.rb +147 -0
  60. data/lib/mbrao/version.rb +25 -0
  61. data/lib/mbrao.rb +24 -0
  62. data/mbrao.gemspec +31 -0
  63. data/spec/coverage_helper.rb +17 -0
  64. data/spec/mbrao/author_spec.rb +62 -0
  65. data/spec/mbrao/content_spec.rb +280 -0
  66. data/spec/mbrao/integrations/rails_spec.rb +92 -0
  67. data/spec/mbrao/parser_spec.rb +269 -0
  68. data/spec/mbrao/parsing_engines/base_spec.rb +52 -0
  69. data/spec/mbrao/parsing_engines/plain_text_spec.rb +141 -0
  70. data/spec/mbrao/rendering_engines/base_spec.rb +17 -0
  71. data/spec/mbrao/rendering_engines/html_pipeline_spec.rb +128 -0
  72. data/spec/spec_helper.rb +15 -0
  73. metadata +195 -0
@@ -0,0 +1,280 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Mbrao::Content do
10
+ let(:reference) { Mbrao::Content.new("UID") }
11
+
12
+ describe "#initialize" do
13
+ it "store the uid" do
14
+ reference = Mbrao::Content.new("SAMPLE")
15
+ expect(reference.uid).to eq("SAMPLE")
16
+ end
17
+ end
18
+
19
+ describe "#locales=" do
20
+ it "correctly assign a string, a symbol or an array" do
21
+ reference.locales = :it
22
+ expect(reference.locales).to eq(["it"])
23
+ reference.locales = "en"
24
+ expect(reference.locales).to eq(["en"])
25
+ reference.locales = ["en", [:it, :es]]
26
+ expect(reference.locales).to eq(["en", "it", "es"])
27
+ end
28
+ end
29
+
30
+ shared_examples_for("localized setter") do |attribute|
31
+ it "should assign a single string" do
32
+ reference.send("#{attribute}=", "ABC")
33
+ expect(reference.send(attribute)).to eq("ABC")
34
+ reference.send("#{attribute}=", 1)
35
+ expect(reference.send(attribute)).to eq("1")
36
+ reference.send("#{attribute}=", nil)
37
+ expect(reference.send(attribute)).to eq("")
38
+ end
39
+
40
+ it "should assign a string with locales and sanitized entries" do
41
+ reference.send("#{attribute}=", {en: nil, es: "ABC", it: 1})
42
+ value = reference.send(attribute)
43
+ expect(value).to be_a(::HashWithIndifferentAccess)
44
+ expect(value["en"]).to eq("")
45
+ expect(value[:es]).to eq("ABC")
46
+ expect(value["it"]).to eq("1")
47
+ end
48
+ end
49
+
50
+ shared_examples_for("localized getter") do |attribute, v1, v2|
51
+ it "should raise an exception if not available for that locale" do
52
+ reference.locales = [:en, :it, :es]
53
+ reference.send("#{attribute}=", v1)
54
+ expect { reference.send("get_#{attribute}", [:de, :it]) }.not_to raise_error(Mbrao::Exceptions::UnavailableLocalization)
55
+ expect { reference.send("get_#{attribute}", [:de]) }.to raise_error(Mbrao::Exceptions::UnavailableLocalization)
56
+ end
57
+
58
+ it "should return the attribute itself if not localized" do
59
+ reference.locales = [:en, :it, :es]
60
+ reference.send("#{attribute}=", v1)
61
+ expect(reference.send("get_#{attribute}", [:de, :it])).to eq(v1)
62
+ end
63
+
64
+ it "should return the default locale if no locales are specified" do
65
+ Mbrao::Parser.locale = :it
66
+ reference.send("#{attribute}=", {en: v1, it: v2})
67
+ expect(reference.send("get_#{attribute}")).to eq(v2)
68
+ end
69
+
70
+ it "should return only the subset of valid and request locales" do
71
+ Mbrao::Parser.locale = :it
72
+ reference.send("#{attribute}=", {en: v1, it: v2, de: v1, es: v2})
73
+
74
+ value = reference.send("get_#{attribute}", [:de, :es])
75
+ expect(value).to be_a(::HashWithIndifferentAccess)
76
+ expect(value.keys).to eq(["de", "es"])
77
+
78
+ value = reference.send("get_#{attribute}", [:it, :de, :pt, :fr])
79
+ expect(value.keys.sort).to eq(["de", "it"])
80
+
81
+ reference.locales = [:en, :it, :es]
82
+ value = reference.send("get_#{attribute}", "*")
83
+ expect(value.keys.sort).to eq(["de", "en", "es", "it"])
84
+
85
+ reference.send("#{attribute}=", {en: v1, "it,es" => v2, " de, fr " => v1})
86
+ value = reference.send("get_#{attribute}", [:it, :fr])
87
+ expect(value.keys.sort).to eq(["fr", "it"])
88
+ end
89
+ end
90
+
91
+ shared_examples_for("date setter") do |attribute|
92
+ it "should correctly parse a datetime classes" do
93
+ reference.send("#{attribute}=", Date.civil(2012, 8, 8))
94
+ value = reference.send(attribute)
95
+ expect(value).to be_a(DateTime)
96
+ expect(value.strftime("%Y%m%dT%H%M%S%z")).to eq("20120808T000000+0000")
97
+
98
+ reference.send("#{attribute}=", DateTime.civil(2012, 8, 8, 11, 30, 45))
99
+ value = reference.send(attribute)
100
+ expect(value).to be_a(DateTime)
101
+ expect(value.strftime("%Y%m%dT%H%M%S%z")).to eq("20120808T113045+0000")
102
+
103
+ Time.zone = 'Europe/Rome'
104
+ date = Time.at(1344421800)
105
+ reference.send("#{attribute}=", date)
106
+ value = reference.send(attribute)
107
+ expect(value).to be_a(DateTime)
108
+ expect(value.strftime("%Y%m%dT%H%M%S%z")).to eq("20120808T103000+0000")
109
+ end
110
+
111
+ it "should correctly parse a timestamp" do
112
+ reference.send("#{attribute}=", 1344421800)
113
+ value = reference.send(attribute)
114
+ expect(value).to be_a(DateTime)
115
+ expect(value.strftime("%Y%m%dT%H%M%S%z")).to eq("20120808T103000+0000")
116
+ end
117
+
118
+ it "should try to parse everything else as a ISO8601 format" do
119
+ reference.send("#{attribute}=", "20120808T083000-0200")
120
+ value = reference.send(attribute)
121
+ expect(value).to be_a(DateTime)
122
+ expect(value.strftime("%Y%m%dT%H%M%S%z")).to eq("20120808T103000+0000")
123
+
124
+ expect { reference.send("#{attribute}=", "ABC") }.to raise_error(Mbrao::Exceptions::InvalidDate)
125
+ expect { reference.send("#{attribute}=", []) }.to raise_error(Mbrao::Exceptions::InvalidDate)
126
+ end
127
+ end
128
+
129
+ describe "#title=" do
130
+ it_should_behave_like "localized setter", :title
131
+ end
132
+
133
+ describe "#body=" do
134
+ it "should set the content as string" do
135
+ reference.body = "A"
136
+ expect(reference.body).to eq("A")
137
+ reference.body = 1
138
+ expect(reference.body).to eq("1")
139
+ reference.body = nil
140
+ expect(reference.body).to eq("")
141
+ end
142
+ end
143
+
144
+ describe "#tags=" do
145
+ it "should assign a single value" do
146
+ reference.tags = "ABC"
147
+ expect(reference.tags).to eq(["ABC"])
148
+ reference.tags = ["ABC", [1, nil]]
149
+ expect(reference.tags).to eq(["ABC", "1"])
150
+ end
151
+
152
+ it "should assign values with locales and sanitized entries" do
153
+ reference.tags = {en: nil, es: "ABC", it: [1, [2, 3]]}
154
+ value = reference.tags
155
+ expect(value).to be_a(::HashWithIndifferentAccess)
156
+ expect(value["en"]).to eq([])
157
+ expect(value[:es]).to eq(["ABC"])
158
+ expect(value["it"]).to eq(["1", "2", "3"])
159
+ end
160
+ end
161
+
162
+ describe "#more=" do
163
+ it_should_behave_like "localized setter", :more
164
+ end
165
+
166
+ describe "#author=" do
167
+ it "should assign an existing author" do
168
+ author = ::Mbrao::Author.new("NAME")
169
+ reference.author = author
170
+ expect(reference.author).to be(author)
171
+ end
172
+
173
+ it "should only assign a name" do
174
+ reference.author = "NAME"
175
+ expect(reference.author).to be_a(::Mbrao::Author)
176
+ expect(reference.author.name).to eq("NAME")
177
+ end
178
+
179
+ it "should assign by an hash" do
180
+ reference.author = {name: "NAME", email: "EMAIL@email.com", "website" => "http://WEBSITE.TLD", "image" => "http://IMAGE.TLD", metadata: {a: "b"}, uid: "UID"}
181
+ expect(reference.author).to be_a(::Mbrao::Author)
182
+ expect(reference.author.name).to eq("NAME")
183
+ expect(reference.author.email).to eq("EMAIL@email.com")
184
+ expect(reference.author.website).to eq("http://WEBSITE.TLD")
185
+ expect(reference.author.image).to eq("http://IMAGE.TLD")
186
+ expect(reference.author.metadata).to be_a(::HashWithIndifferentAccess)
187
+ expect(reference.author.metadata["a"]).to eq("b")
188
+ expect(reference.author.uid).to eq("UID")
189
+ end
190
+ end
191
+
192
+ describe "#created_at=" do
193
+ it_should_behave_like "date setter", :created_at
194
+ end
195
+
196
+ describe "#updated_at=" do
197
+ it_should_behave_like "date setter", :updated_at
198
+ end
199
+
200
+ describe "#metadata=" do
201
+ it "correctly set a non hash value" do
202
+ reference.metadata = "RAW"
203
+ expect(reference.metadata).to be_a(::HashWithIndifferentAccess)
204
+ expect(reference.metadata["raw"]).to eq("RAW")
205
+ end
206
+
207
+ it "correctly set a hash value" do
208
+ reference.metadata = {en: nil, es: "ABC", it: [1, [2, 3]]}
209
+ expect(reference.metadata).to be_a(::HashWithIndifferentAccess)
210
+ expect(reference.metadata["es"]).to eq("ABC")
211
+ end
212
+ end
213
+
214
+ describe "#enabled_for_locales?" do
215
+ it "correctly check availability for certain locales" do
216
+ reference.locales = [:en, :it]
217
+ expect(reference.enabled_for_locales?).to be_true
218
+ expect(reference.enabled_for_locales?(:en)).to be_true
219
+ expect(reference.enabled_for_locales?(:it, :es)).to be_true
220
+ expect(reference.enabled_for_locales?(:es, :de)).to be_false
221
+ end
222
+ end
223
+
224
+ describe "#get_title" do
225
+ it_should_behave_like "localized getter", :title, "ABC", "123"
226
+ end
227
+
228
+ describe "#get_body" do
229
+ it "should create a parsing engine and use it for filtering" do
230
+ reference.body = "BODY"
231
+ engine = ::Mbrao::ParsingEngines::Base.new
232
+ engine.should_receive(:filter_content).with(reference, ["it", "en"])
233
+ ::Mbrao::Parser.should_receive(:create_engine).with("ENGINE").and_return(engine)
234
+ reference.get_body(["it", "en"], "ENGINE")
235
+ end
236
+ end
237
+
238
+ describe "#get_tags" do
239
+ it_should_behave_like "localized getter", :tags, ["ABC", "123"], ["1", "2", "3", "4"]
240
+ end
241
+
242
+ describe "#get_more" do
243
+ it_should_behave_like "localized getter", :more, "ABC", "123"
244
+ end
245
+
246
+ describe ".create" do
247
+ it "should return a Content object" do
248
+ expect(::Mbrao::Content.create(nil, "BODY")).to be_a(::Mbrao::Content)
249
+ end
250
+
251
+ it "should assign the body" do
252
+ expect(::Mbrao::Content.create(nil, "BODY").body).to eq("BODY")
253
+ end
254
+
255
+ it "should assign metadata if present" do
256
+ created_at = DateTime.civil(1984, 7, 7, 11, 30, 0)
257
+ metadata = {"uid" => "UID", "title" => {it: "IT", en: "EN"}, "author" => "AUTHOR", "tags" => {it: "IT", en: "EN"}, "more" => "MORE", created_at: created_at, locales: ["it", ["en"]], other: ["OTHER"]}
258
+ content = ::Mbrao::Content.create(metadata, "BODY")
259
+
260
+ expect(content.uid).to eq("UID")
261
+ expect(content.title).to eq({"it" => "IT", "en" => "EN"})
262
+ expect(content.author).to be_a(::Mbrao::Author)
263
+ expect(content.author.name).to eq("AUTHOR")
264
+ expect(content.body).to eq("BODY")
265
+ expect(content.tags).to eq({"it" => ["IT"], "en" => ["EN"]})
266
+ expect(content.more).to eq("MORE")
267
+ expect(content.created_at).to eq(created_at)
268
+ expect(content.updated_at).to eq(created_at)
269
+ expect(content.locales).to eq(["it", "en"])
270
+ expect(content.metadata).to eq({"other" => ["OTHER"]})
271
+ end
272
+
273
+ it "should ignore invalid metadata" do
274
+ expect(::Mbrao::Content.create(nil, "BODY").metadata).to eq({})
275
+ expect(::Mbrao::Content.create(1, "BODY").metadata).to eq({})
276
+ expect(::Mbrao::Content.create([], "BODY").metadata).to eq({})
277
+ expect(::Mbrao::Content.create("A", "BODY").metadata).to eq({})
278
+ end
279
+ end
280
+ end
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe ActionView::Template::Handlers::MbraoTemplate do
10
+ class DummyTemplate
11
+ def initialize(content)
12
+ @content = content
13
+ end
14
+
15
+ def source
16
+ @content
17
+ end
18
+ end
19
+
20
+ class DummyController
21
+ def params
22
+ {locale: "LOCALE"}
23
+ end
24
+
25
+ def helper_method(method)
26
+ end
27
+ end
28
+
29
+ class DummyRenderer
30
+ def controller
31
+ @controller ||= DummyController.new
32
+ end
33
+ end
34
+
35
+ describe ".instance" do
36
+ it "should create a new instance" do
37
+ expect(ActionView::Template::Handlers::MbraoTemplate.instance).to be_a(ActionView::Template::Handlers::MbraoTemplate)
38
+ end
39
+
40
+ it "should always return the same instance" do
41
+ other = ActionView::Template::Handlers::MbraoTemplate.instance
42
+ ActionView::Template::Handlers::MbraoTemplate.should_not_receive(:new)
43
+ expect(ActionView::Template::Handlers::MbraoTemplate.instance).to eq(other)
44
+ end
45
+
46
+ it "should recreate an instance" do
47
+ other = ActionView::Template::Handlers::MbraoTemplate.instance
48
+ expect(ActionView::Template::Handlers::MbraoTemplate.instance(true)).not_to eq(other)
49
+ end
50
+ end
51
+
52
+
53
+ describe "#call" do
54
+ it "should return a Ruby snippet" do
55
+ expect(ActionView::Template::Handlers::MbraoTemplate.instance.call(DummyTemplate.new("CONTENT"))).to eq("ActionView::Template::Handlers::MbraoTemplate.instance.render(self, \"CONTENT\")")
56
+ end
57
+ end
58
+
59
+ describe "#render" do
60
+ it "should initialize a Content and assign it to the controller" do
61
+ controller = DummyController.new
62
+ renderer = DummyRenderer.new
63
+ renderer.stub(:controller).and_return(controller)
64
+
65
+ expect(controller.respond_to?(:mbrao_content)).to be_false
66
+ ::Mbrao::Parser.should_receive(:parse).and_call_original
67
+ controller.should_receive(:helper_method).with(:mbrao_content)
68
+ ActionView::Template::Handlers::MbraoTemplate.instance.render(renderer, "CONTENT")
69
+
70
+ expect(controller.respond_to?(:mbrao_content)).to be_true
71
+ expect(controller.mbrao_content.body).to eq("CONTENT")
72
+ end
73
+
74
+ it "should render contents, using specified engine and locale" do
75
+ controller = DummyController.new
76
+ renderer = DummyRenderer.new
77
+ renderer.stub(:controller).and_return(controller)
78
+
79
+ content = ::Mbrao::Content.create({engine: "ENGINE"}, "CONTENT")
80
+ ::Mbrao::Parser.stub(:parse).and_return(content)
81
+
82
+ ::Mbrao::Parser.should_receive(:render).with(content, {engine: "ENGINE", locale: "LOCALE"})
83
+ ActionView::Template::Handlers::MbraoTemplate.instance.render(DummyRenderer.new, "CONTENT")
84
+ end
85
+ end
86
+
87
+ describe "#supports_streaming?" do
88
+ it "should be true" do
89
+ expect(ActionView::Template::Handlers::MbraoTemplate.instance.supports_streaming?).to be_true
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,269 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Mbrao::Parser do
10
+ class BlankParser
11
+ def parse(content, options)
12
+
13
+ end
14
+ end
15
+
16
+ class ::Mbrao::ParsingEngines::ScopedParser
17
+ def parse(content, options)
18
+
19
+ end
20
+ end
21
+
22
+ shared_examples_for("attribute_with_default") do |attribute, default, sanitizer|
23
+ it "should return the default value" do
24
+ ::Mbrao::Parser.send("#{attribute}=", nil)
25
+ expect(::Mbrao::Parser.send(attribute)).to eq(default)
26
+ end
27
+
28
+ it "should return the set value sanitized" do
29
+ ::Mbrao::Parser.send("#{attribute}=", :en)
30
+ expect(::Mbrao::Parser.send(attribute)).to eq("en".send(sanitizer))
31
+ end
32
+ end
33
+
34
+ describe ".locale" do
35
+ it_should_behave_like("attribute_with_default", :locale, "en", :ensure_string)
36
+ end
37
+
38
+ describe ".parsing_engine" do
39
+ it_should_behave_like("attribute_with_default", :parsing_engine, :plain_text, :to_sym)
40
+ end
41
+
42
+ describe ".rendering_engine" do
43
+ it_should_behave_like("attribute_with_default", :rendering_engine, :html_pipeline, :to_sym)
44
+ end
45
+
46
+ describe ".parse" do
47
+ it "should forward to the instance" do
48
+ ::Mbrao::Parser.instance.should_receive(:parse).with("TEXT", {options: {}})
49
+ ::Mbrao::Parser.parse("TEXT", {options: {}})
50
+ end
51
+ end
52
+
53
+ describe ".render" do
54
+ it "should forward to the instance" do
55
+ ::Mbrao::Parser.instance.should_receive(:render).with("TEXT", {options: {}}, {content: {}})
56
+ ::Mbrao::Parser.render("TEXT", {options: {}}, {content: {}})
57
+ end
58
+ end
59
+
60
+ describe ".create_engine" do
61
+ it "should create an engine via .find_class" do
62
+ reference = ::Mbrao::ParsingEngines::ScopedParser.new
63
+ cls = ::Mbrao::ParsingEngines::ScopedParser
64
+ cls.should_receive(:new).exactly(2).and_return(reference)
65
+
66
+ ::Mbrao::Parser.should_receive(:find_class).with(:scoped_parser, "::Mbrao::ParsingEngines::%CLASS%").and_return(cls)
67
+ expect(::Mbrao::Parser.create_engine(:scoped_parser)).to eq(reference)
68
+ ::Mbrao::Parser.should_receive(:find_class).with(:scoped_parser, "::Mbrao::RenderingEngines::%CLASS%").and_return(cls)
69
+ expect(::Mbrao::Parser.create_engine(:scoped_parser, :rendering)).to eq(reference)
70
+ end
71
+
72
+ it "should raise an exception if the engine class is not found" do
73
+ expect { ::Mbrao::Parser.create_engine(:invalid) }.to raise_error(::Mbrao::Exceptions::UnknownEngine)
74
+ end
75
+ end
76
+
77
+ describe ".find_class" do
78
+ it "should return a valid class" do
79
+ expect(::Mbrao::Parser.find_class("String")).to eq(String)
80
+ expect(::Mbrao::Parser.find_class("ScopedParser", "::Mbrao::ParsingEngines::%CLASS%")).to eq(::Mbrao::ParsingEngines::ScopedParser)
81
+ end
82
+
83
+ it "should raise an exception if the class is not found" do
84
+ expect { ::Mbrao::Parser.find_class(:invalid) }.to raise_error(::Mbrao::Exceptions::Unimplemented)
85
+ end
86
+
87
+ it "should not expand engine scope if the class starts with ::" do
88
+ expect { ::Mbrao::Parser.find_class("::ScopedParser", "::Mbrao::ParsingEngines::%CLASS%") }.to raise_error(::Mbrao::Exceptions::Unimplemented)
89
+ end
90
+
91
+ it "should only use scope if requested to" do
92
+ expect { ::Mbrao::Parser.find_class("::Fixnum", "::Mbrao::ParsingEngines::%CLASS%", true) }.to raise_error(::Mbrao::Exceptions::Unimplemented)
93
+ end
94
+
95
+ it "should return anything but string or symbol as their class" do
96
+ expect(::Mbrao::Parser.find_class(nil)).to eq(NilClass)
97
+ expect(::Mbrao::Parser.find_class(1)).to eq(Fixnum)
98
+ expect(::Mbrao::Parser.find_class(["A"])).to eq(Array)
99
+ expect(::Mbrao::Parser.find_class({a: "b"})).to eq(Hash)
100
+ end
101
+ end
102
+
103
+ describe ".instance" do
104
+ it("should call .new") do
105
+ ::Mbrao::Parser.instance_variable_set(:@instance, nil)
106
+ ::Mbrao::Parser.should_receive(:new)
107
+ ::Mbrao::Parser.instance
108
+ end
109
+
110
+ it("should return the same instance") do
111
+ ::Mbrao::Parser.stub(:new) do Time.now end
112
+ instance = ::Mbrao::Parser.instance
113
+ expect(::Mbrao::Parser.instance).to be(instance)
114
+ ::Mbrao::Parser.instance_variable_set(:@instance, nil)
115
+ end
116
+
117
+ it("should return a new instance if requested to") do
118
+ ::Mbrao::Parser.stub(:new) do Time.now end
119
+ instance = ::Mbrao::Parser.instance
120
+ expect(::Mbrao::Parser.instance(true)).not_to be(instance)
121
+ ::Mbrao::Parser.instance_variable_set(:@instance, nil)
122
+ end
123
+ end
124
+
125
+ describe ".is_email?" do
126
+ it "should check for valid emails" do
127
+ expect(Mbrao::Parser.is_email?("valid@email.com")).to be_true
128
+ expect(Mbrao::Parser.is_email?("valid@localhost")).to be_false
129
+ expect(Mbrao::Parser.is_email?("this.is.9@email.com")).to be_true
130
+ expect(Mbrao::Parser.is_email?("INVALID")).to be_false
131
+ expect(Mbrao::Parser.is_email?(nil)).to be_false
132
+ expect(Mbrao::Parser.is_email?([])).to be_false
133
+ expect(Mbrao::Parser.is_email?("this.is.9@email.com.uk")).to be_true
134
+ end
135
+ end
136
+
137
+ describe ".is_url?" do
138
+ it "should check for valid URLs" do
139
+ expect(Mbrao::Parser.is_url?("http://google.it")).to be_true
140
+ expect(Mbrao::Parser.is_url?("ftp://ftp.google.com")).to be_true
141
+ expect(Mbrao::Parser.is_url?("http://google.it/?q=FOO+BAR")).to be_true
142
+ expect(Mbrao::Parser.is_url?("INVALID")).to be_false
143
+ expect(Mbrao::Parser.is_url?([])).to be_false
144
+ expect(Mbrao::Parser.is_url?(nil)).to be_false
145
+ expect(Mbrao::Parser.is_url?({})).to be_false
146
+ end
147
+ end
148
+
149
+ describe ".sanitized_hash" do
150
+ it "should inject a new hash from existing one" do
151
+ reference = {a: "b"}
152
+ reference.should_receive(:inject).with(an_instance_of(HashWithIndifferentAccess)).and_call_original
153
+ sanitized = Mbrao::Parser.sanitized_hash(reference)
154
+ expect(sanitized).to be_a(HashWithIndifferentAccess)
155
+ expect(sanitized["a"]).to eq("b")
156
+
157
+ end
158
+
159
+ it "should collect sanitized elements" do
160
+ reference = [{a: "b"}]
161
+ reference.should_receive(:collect).and_call_original
162
+ sanitized = Mbrao::Parser.sanitized_hash(reference)
163
+ expect(sanitized).to be_a(Array)
164
+ expect(sanitized[0]).to eq({"a" => "b"})
165
+
166
+ end
167
+
168
+ it "should iterate over nested elements recursively" do
169
+ reference = [{a: "b"}]
170
+ Mbrao::Parser.should_receive(:sanitized_hash).with(anything, :ensure_array).exactly(4).and_call_original
171
+ sanitized = Mbrao::Parser.sanitized_hash({c: reference}, :ensure_array)
172
+ expect(sanitized).to be_a(HashWithIndifferentAccess)
173
+ expect(sanitized["c"]).to eq([{"a" => ["b"]}])
174
+ end
175
+
176
+ it "should convert a non-enumerable object using a method" do
177
+ reference = {a: "b"}
178
+ expect(Mbrao::Parser.sanitized_hash(reference, :ensure_array).fetch("a")).to eq(["b"])
179
+ end
180
+
181
+ it "should convert a non-enumerable object using a block" do
182
+ reference = {a: ["b", "c"]}
183
+ expect((Mbrao::Parser.sanitized_hash(reference) {|v| v.upcase }.fetch("a"))).to eq(["B", "C"])
184
+ end
185
+
186
+ it "should not modify a non-enumerable object if requested to" do
187
+ reference = {a: nil}
188
+ expect((Mbrao::Parser.sanitized_hash(reference, false).fetch("a"))).to be_nil
189
+ end
190
+ end
191
+
192
+ describe ".sanitized_array" do
193
+ it "should return a sanitized array" do
194
+ expect(Mbrao::Parser.sanitized_array([:en, ["A", 1]])).to eq(["en", "A", "1"])
195
+ expect(Mbrao::Parser.sanitized_array(:en)).to eq(["en"])
196
+ expect(Mbrao::Parser.sanitized_array(:en)).to eq(["en"])
197
+ expect(Mbrao::Parser.sanitized_array([:en, nil])).to eq(["en", ""])
198
+ end
199
+
200
+ it "should not uniq if requested to" do
201
+ ref = [{a: "b"}]
202
+ ref.should_not_receive(:uniq)
203
+ expect(Mbrao::Parser.sanitized_array(ref, false)).to eq(["{:a=>\"b\"}"])
204
+ end
205
+
206
+ it "should compact if requested to" do
207
+ ref = [{a: "b"}, nil]
208
+ ref.should_not_receive(:compact)
209
+ expect(Mbrao::Parser.sanitized_array(ref, false, true)).to eq(["{:a=>\"b\"}"])
210
+ end
211
+
212
+ it "should use the requested method for sanitizing" do
213
+ ref = [{a: "b"}]
214
+ ref.should_not_receive(:ensure_string)
215
+ Hash.any_instance.should_receive(:to_json).and_call_original
216
+ expect(Mbrao::Parser.sanitized_array(ref, true, false, :to_json)).to eq(["{\"a\":\"b\"}"])
217
+ end
218
+
219
+ it "should use the block for sanitizing" do
220
+ ref = [{a: "b"}]
221
+ ref.should_not_receive(:ensure_string)
222
+ expect(Mbrao::Parser.sanitized_array(ref) {|e| e.keys.first }).to eq([:a])
223
+ end
224
+ end
225
+
226
+ describe "#parse" do
227
+ it "should sanitize options" do
228
+ reference = BlankParser.new
229
+ ::Mbrao::Parser.should_receive(:create_engine).exactly(3).and_return(reference)
230
+
231
+ reference.should_receive(:parse).with("CONTENT", {"metadata" => true, "content" => true, "engine" => :blank_parser})
232
+ reference.should_receive(:parse).with("CONTENT", {"metadata" => true, "content" => false, "engine" => :blank_parser})
233
+ reference.should_receive(:parse).with("CONTENT", {"metadata" => false, "content" => false, "engine" => :blank_parser})
234
+
235
+ ::Mbrao::Parser.new.parse("CONTENT", {engine: :blank_parser})
236
+ ::Mbrao::Parser.new.parse("CONTENT", {engine: :blank_parser, content: 2})
237
+ ::Mbrao::Parser.new.parse("CONTENT", {engine: :blank_parser, metadata: false, content: false})
238
+ end
239
+
240
+ it "should call .create_engine call its #parse method" do
241
+ reference = BlankParser.new
242
+ ::Mbrao::Parser.should_receive(:create_engine).and_return(reference)
243
+
244
+ reference.should_receive(:parse).with("CONTENT", {"metadata" => true, "content" => true, "engine" => :blank_parser, "other" => "OK"})
245
+ ::Mbrao::Parser.new.parse("CONTENT", {engine: :blank_parser, other: "OK"})
246
+ end
247
+ end
248
+
249
+ describe "#render" do
250
+ it "should sanitize options" do
251
+ reference = Object.new
252
+ ::Mbrao::Parser.should_receive(:create_engine).exactly(2).and_return(reference)
253
+
254
+ reference.should_receive(:render).with("CONTENT", {"engine" => :blank_rendered}, {content: "OK"})
255
+ reference.should_receive(:render).with("CONTENT", {"engine" => :html_pipeline}, {content: "OK"})
256
+
257
+ ::Mbrao::Parser.new.render("CONTENT", {engine: :blank_rendered}, {content: "OK"})
258
+ ::Mbrao::Parser.new.render("CONTENT", {engine: :html_pipeline}, {content: "OK"})
259
+ end
260
+
261
+ it "should call .create_engine call its #parse method" do
262
+ reference = Object.new
263
+ ::Mbrao::Parser.should_receive(:create_engine).with(:blank_rendered, :rendering).and_return(reference)
264
+
265
+ reference.should_receive(:render).with("CONTENT", {"engine" => :blank_rendered}, {content: "OK"})
266
+ ::Mbrao::Parser.new.render("CONTENT", {engine: :blank_rendered}, {content: "OK"})
267
+ end
268
+ end
269
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Mbrao::ParsingEngines::Base do
10
+ let(:reference) { Mbrao::ParsingEngines::Base.new }
11
+
12
+ shared_examples_for("unimplemented") do |method|
13
+ it "should raise an exception" do
14
+ expect { reference.send(method, "CONTENT", {}) }.to raise_error(Mbrao::Exceptions::Unimplemented)
15
+ end
16
+ end
17
+
18
+ describe "#separate_components" do
19
+ it_should_behave_like "unimplemented", :separate_components
20
+ end
21
+
22
+ describe "#parse_metadata" do
23
+ it_should_behave_like "unimplemented", :parse_metadata
24
+ end
25
+
26
+ describe "#filter_content" do
27
+ it_should_behave_like "unimplemented", :filter_content
28
+ end
29
+
30
+ describe "#parse" do
31
+ let(:reference) { ::Mbrao::ParsingEngines::Base.new }
32
+
33
+ it "should forward to ::Mbrao::Content.create" do
34
+ reference.should_receive(:separate_components).with("CONTENT", {a: "b"}).and_return([{a: "b"}, "BODY"])
35
+ reference.stub(:parse_metadata).and_return({a: "b"})
36
+ ::Mbrao::Content.should_receive(:create).with({a: "b"}, "BODY")
37
+ reference.parse("CONTENT", {a: "b"})
38
+ end
39
+
40
+ it "should return a Content object" do
41
+ reference.stub(:separate_components).with("CONTENT", {a: "b"}).and_return([])
42
+ reference.stub(:parse_metadata).and_return({})
43
+ expect(reference.parse("CONTENT", {a: "b"})).to be_a(::Mbrao::Content)
44
+ end
45
+
46
+ it "should separate_components" do
47
+ reference.should_receive(:separate_components).with("CONTENT", {a: "b"})
48
+ reference.stub(:parse_metadata).and_return({})
49
+ reference.parse("CONTENT", {a: "b"})
50
+ end
51
+ end
52
+ end