ballast 1.9.3 → 2.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 (78) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +35 -0
  3. data/.travis-gemfile +4 -5
  4. data/.travis.yml +3 -2
  5. data/CHANGELOG.md +62 -6
  6. data/Gemfile +7 -8
  7. data/README.md +2 -2
  8. data/ballast.gemspec +9 -12
  9. data/doc/Ballast/AjaxResponse.html +1380 -0
  10. data/doc/Ballast/Concerns/AjaxHandling.html +662 -0
  11. data/doc/Ballast/Concerns/Common.html +81 -361
  12. data/doc/Ballast/Concerns/ErrorsHandling.html +18 -36
  13. data/doc/Ballast/Concerns/View.html +181 -157
  14. data/doc/Ballast/Concerns.html +6 -6
  15. data/doc/Ballast/Configuration.html +204 -31
  16. data/doc/Ballast/Emoji/Character.html +411 -0
  17. data/doc/Ballast/Emoji/Utils.html +794 -0
  18. data/doc/Ballast/Emoji.html +125 -0
  19. data/doc/Ballast/Errors/{BaseError.html → Base.html} +61 -34
  20. data/doc/Ballast/Errors/{PerformError.html → Failure.html} +21 -17
  21. data/doc/Ballast/Errors/InvalidDomain.html +11 -11
  22. data/doc/Ballast/Errors/{ValidationError.html → ValidationFailure.html} +24 -16
  23. data/doc/Ballast/Errors.html +5 -5
  24. data/doc/Ballast/Middlewares/DefaultHost.html +6 -6
  25. data/doc/Ballast/Middlewares.html +4 -4
  26. data/doc/Ballast/RequestDomainMatcher.html +28 -28
  27. data/doc/Ballast/Service/Response.html +1243 -0
  28. data/doc/Ballast/Service.html +1314 -0
  29. data/doc/Ballast/Version.html +7 -7
  30. data/doc/Ballast.html +15 -15
  31. data/doc/_index.html +59 -30
  32. data/doc/class_list.html +6 -2
  33. data/doc/css/style.css +1 -0
  34. data/doc/file.README.html +6 -6
  35. data/doc/file_list.html +5 -1
  36. data/doc/frames.html +1 -1
  37. data/doc/index.html +6 -6
  38. data/doc/js/full_list.js +4 -1
  39. data/doc/method_list.html +167 -79
  40. data/doc/top-level-namespace.html +41 -4
  41. data/lib/ballast/ajax_response.rb +76 -0
  42. data/lib/ballast/concerns/ajax_handling.rb +73 -0
  43. data/lib/ballast/concerns/common.rb +25 -47
  44. data/lib/ballast/concerns/errors_handling.rb +21 -30
  45. data/lib/ballast/concerns/view.rb +24 -22
  46. data/lib/ballast/configuration.rb +30 -10
  47. data/lib/ballast/emoji.rb +114 -0
  48. data/lib/ballast/errors.rb +16 -13
  49. data/lib/ballast/middlewares/default_host.rb +3 -3
  50. data/lib/ballast/request_domain_matcher.rb +7 -7
  51. data/lib/ballast/service.rb +147 -0
  52. data/lib/ballast/version.rb +3 -3
  53. data/lib/ballast.rb +22 -22
  54. data/spec/ballast/ajax_response_spec.rb +61 -0
  55. data/spec/ballast/concerns/ajax_handling_spec.rb +86 -0
  56. data/spec/ballast/concerns/common_spec.rb +17 -52
  57. data/spec/ballast/concerns/errors_handling_spec.rb +35 -29
  58. data/spec/ballast/concerns/view_spec.rb +21 -32
  59. data/spec/ballast/configuration_spec.rb +66 -16
  60. data/spec/ballast/emoji_spec.rb +103 -0
  61. data/spec/ballast/errors_spec.rb +5 -5
  62. data/spec/ballast/middlewares/default_host_spec.rb +3 -5
  63. data/spec/ballast/request_domain_matcher_spec.rb +4 -4
  64. data/spec/ballast/service_spec.rb +137 -0
  65. data/spec/spec_helper.rb +1 -13
  66. metadata +42 -80
  67. data/doc/Ballast/Concerns/Ajax.html +0 -945
  68. data/doc/Ballast/Context.html +0 -417
  69. data/doc/Ballast/Operation.html +0 -1304
  70. data/doc/Ballast/OperationsChain.html +0 -597
  71. data/lib/ballast/concerns/ajax.rb +0 -134
  72. data/lib/ballast/context.rb +0 -38
  73. data/lib/ballast/operation.rb +0 -136
  74. data/lib/ballast/operations_chain.rb +0 -45
  75. data/spec/ballast/concerns/ajax_spec.rb +0 -141
  76. data/spec/ballast/context_spec.rb +0 -23
  77. data/spec/ballast/operation_spec.rb +0 -177
  78. data/spec/ballast/operations_chain_spec.rb +0 -61
@@ -8,11 +8,6 @@ require "spec_helper"
8
8
  describe Ballast::Concerns::View do
9
9
  class ViewMockClass < OpenStruct
10
10
  include Ballast::Concerns::View
11
-
12
- def initialize(attrs)
13
- @operation = OpenStruct.new(attrs.delete(:operation))
14
- super(attrs)
15
- end
16
11
  end
17
12
 
18
13
  subject{ ViewMockClass.new(response: OpenStruct.new(headers: {}), headers: {}, params: {}, performed?: false) }
@@ -27,42 +22,36 @@ describe Ballast::Concerns::View do
27
22
 
28
23
  describe "#browser" do
29
24
  it "should return a browser object" do
30
- expect(subject).to receive(:request).and_return(OpenStruct.new(user_agent: "AGENT"))
31
- expect(Brauser::Browser).to receive(:new).with("AGENT").and_return("BROWSER")
25
+ expect(subject).to receive(:request).exactly(2).and_return(OpenStruct.new(user_agent: "AGENT", headers: {"Accept-Language" => "FOO"}))
26
+ expect(Brauser::Browser).to receive(:new).with("AGENT", "FOO").and_return("BROWSER")
32
27
  expect(subject.browser).to eq("BROWSER")
33
28
  end
34
29
  end
35
30
 
36
31
  describe "#browser_supported?" do
37
- before(:each) do
38
- expect(subject).to receive(:request).and_return(OpenStruct.new(user_agent: "AGENT"))
32
+ before(:example) do
33
+ expect(subject).to receive(:request).exactly(2).and_return(OpenStruct.new(user_agent: "AGENT", headers: {"Accept-Language" => "FOO"}))
39
34
  end
40
35
 
41
36
  it "should check if a browser is supported" do
42
- expect(subject.browser).to receive(:supported?).with("CONF").and_return("SUPPORTED")
37
+ expect(subject.browser).to receive(:supported?).with(Dir.pwd + "/CONF").and_return("SUPPORTED")
43
38
  expect(subject.browser_supported?("CONF")).to eq("SUPPORTED")
44
39
  end
45
40
 
46
41
  it "should use a default file" do
47
- class Rails
48
- def self.root
49
- Pathname.new("ROOT")
50
- end
51
- end
52
-
53
- expect(subject.browser).to receive(:supported?).with("ROOT/config/supported-browsers.yml").and_return(true)
42
+ expect(subject.browser).to receive(:supported?).with(Dir.pwd + "/config/supported-browsers.yml").and_return(true)
54
43
  subject.browser_supported?
55
44
  end
56
45
  end
57
46
 
58
47
  describe "#layout_params" do
59
48
  it "should return a single parameter" do
60
- subject.set_layout_params(a: 1)
49
+ subject.update_layout_params(a: 1)
61
50
  expect(subject.layout_params(:a)).to eq(1)
62
51
  end
63
52
 
64
53
  it "should return a default value for a missing parameter" do
65
- subject.set_layout_params(a: 1)
54
+ subject.update_layout_params(a: 1)
66
55
  expect(subject.layout_params(:b, 2)).to eq(2)
67
56
  end
68
57
 
@@ -73,27 +62,27 @@ describe Ballast::Concerns::View do
73
62
 
74
63
  it "should return the entire hash if no arguments are passed" do
75
64
  expect(subject.layout_params).to eq({})
76
- subject.set_layout_params(a: 1)
65
+ subject.update_layout_params(a: 1)
77
66
  expect(subject.layout_params).to eq({"a" => 1})
78
67
  end
79
68
  end
80
69
 
81
- describe "#set_layout_params" do
70
+ describe "#update_layout_params" do
82
71
  it "should merge arguments into the existing parameters" do
83
72
  expect(subject.layout_params).to eq({})
84
- subject.set_layout_params(a: 1, b: 2)
73
+ subject.update_layout_params(a: 1, b: 2)
85
74
  expect(subject.layout_params).to eq({"a" => 1, "b" => 2})
86
75
  end
87
76
  end
88
77
 
89
78
  describe "#javascript_params" do
90
- before(:each) do
79
+ before(:example) do
91
80
  subject.instance_variable_set(:@javascript_params, {a: "1", b: 2})
92
81
  end
93
82
 
94
83
  it "should output Javascript as HTML" do
95
84
  expect(subject).to receive(:content_tag).with(:tag, '{"a":"1","b":2}', {"data-jid" => "ID"}).and_return("HTML")
96
- expect(subject.javascript_params("ID", :tag)).to eq("HTML")
85
+ expect(subject.javascript_params("ID", tag: :tag)).to eq("HTML")
97
86
  end
98
87
 
99
88
  it "should return Javascript as Hash" do
@@ -102,9 +91,9 @@ describe Ballast::Concerns::View do
102
91
  end
103
92
  end
104
93
 
105
- describe "#add_javascript_params" do
106
- before(:each) do
107
- subject.add_javascript_params(:a, {b: 1})
94
+ describe "#update_javascript_params" do
95
+ before(:example) do
96
+ subject.update_javascript_params(:a, {b: 1})
108
97
  end
109
98
 
110
99
  it "should create an Hash" do
@@ -112,27 +101,27 @@ describe Ballast::Concerns::View do
112
101
  end
113
102
 
114
103
  it "should add new keys" do
115
- subject.add_javascript_params(:c, {d: 2})
104
+ subject.update_javascript_params(:c, {d: 2})
116
105
  expect(subject.instance_variable_get(:@javascript_params)).to eq({a: {b: 1}, c: {d: 2}}.with_indifferent_access)
117
106
  end
118
107
 
119
108
  it "should merge values for the same key" do
120
- subject.add_javascript_params(:a, {d: 2})
109
+ subject.update_javascript_params(:a, {d: 2})
121
110
  expect(subject.instance_variable_get(:@javascript_params)).to eq({a: {b: 1, d: 2}}.with_indifferent_access)
122
111
  end
123
112
 
124
113
  it "should replace values for same key if requested to" do
125
- subject.add_javascript_params(:a, {d: 2}, true)
114
+ subject.update_javascript_params(:a, {d: 2}, replace: true)
126
115
  expect(subject.instance_variable_get(:@javascript_params)).to eq({a: {d: 2}}.with_indifferent_access)
127
116
  end
128
117
 
129
118
  it "should merge from the root" do
130
- subject.add_javascript_params(nil, {d: 2})
119
+ subject.update_javascript_params(nil, {d: 2})
131
120
  expect(subject.instance_variable_get(:@javascript_params)).to eq({a: {b: 1}, d: 2}.with_indifferent_access)
132
121
  end
133
122
 
134
123
  it "should replace the entire hash" do
135
- subject.add_javascript_params(nil, {d: 2}, true)
124
+ subject.update_javascript_params(nil, {d: 2}, replace: true)
136
125
  expect(subject.instance_variable_get(:@javascript_params)).to eq({d: 2}.with_indifferent_access)
137
126
  end
138
127
  end
@@ -6,29 +6,79 @@
6
6
  require "spec_helper"
7
7
 
8
8
  describe Ballast::Configuration do
9
- describe "#initialize" do
10
- before(:each) do
11
- expect(YAML).to receive(:load_file).with("ROOT/config/section_a.yml").and_return({"ENV" => {a: {b: 1}}, "OTHER" => {aa: 3}})
12
- expect(YAML).to receive(:load_file).with("ROOT/config/section-b.yml").and_return({"ENV" => {c: {d: 2}}, "OTHER" => {cc: 4}})
9
+ describe ".default_root" do
10
+ it "should return the Rails root" do
11
+ stub_const("Rails", OpenStruct.new(root: "ROOT"))
12
+ expect(Ballast::Configuration.default_root).to eq("ROOT")
13
+ end
14
+
15
+ it "should fallback to the current working directory" do
16
+ expect(Ballast::Configuration.default_root).to eq(Dir.pwd)
17
+ end
18
+ end
19
+
20
+ describe ".default_environment" do
21
+ it "should return the Rails environment" do
22
+ stub_const("Rails", OpenStruct.new(env: "RAILS-ENV"))
23
+ expect(Ballast::Configuration.default_environment).to eq("RAILS-ENV")
24
+ end
25
+
26
+ it "should return the Rack environment" do
27
+ old_env = ENV["RACK_ENV"]
28
+ ENV["RACK_ENV"] = "RACK-ENV"
29
+
30
+ expect(Ballast::Configuration.default_environment).to eq("RACK-ENV")
31
+ ENV["RACK_ENV"] = old_env
13
32
  end
14
33
 
15
- it "should load a list of sections" do
16
- Ballast::Configuration.new(sections: ["section_a", "section-b"], root: "ROOT", environment: "ENV")
34
+ it "should fallback to production" do
35
+ old_env = ENV["RACK_ENV"]
36
+ ENV["RACK_ENV"] = nil
37
+
38
+ expect(Ballast::Configuration.default_environment).to eq("production")
39
+ ENV["RACK_ENV"] = old_env
17
40
  end
41
+ end
42
+
43
+ describe "#initialize" do
44
+ describe "when root and environment are defined" do
45
+ before(:example) do
46
+ expect(YAML).to receive(:load_file).with("ROOT/config/section_a.yml").and_return({"ENV" => {a: {b: 1}}, "OTHER" => {aa: 3}})
47
+ expect(YAML).to receive(:load_file).with("ROOT/config/section-b.yml").and_return({"ENV" => {c: {d: 2}}, "OTHER" => {cc: 4}})
48
+ end
49
+
50
+ it "should load a list of sections" do
51
+ Ballast::Configuration.new("section_a", "section-b", root: "ROOT", environment: "ENV")
52
+ end
18
53
 
19
- it "should only load specific environment" do
20
- subject = Ballast::Configuration.new(sections: ["section_a", "section-b"], root: "ROOT", environment: "ENV")
21
- expect(subject["section_a"].keys).to eq(["a"])
22
- expect(subject["section_b"].keys).to eq(["c"])
54
+ it "should only load specific environment" do
55
+ subject = Ballast::Configuration.new("section_a", "section-b", root: "ROOT", environment: "ENV")
56
+ expect(subject["section_a"].keys).to eq(["a"])
57
+ expect(subject["section_b"].keys).to eq(["c"])
58
+ end
23
59
  end
24
60
 
25
- it "should enable dotted access" do
26
- subject = Ballast::Configuration.new(sections: ["section_a", "section-b"], root: "ROOT", environment: "ENV")
61
+ describe "when root and environment are NOT defined, it should autodetect root and environment" do
62
+ around(:example) do |example|
63
+ old_env = ENV["RACK_ENV"]
64
+ ENV["RACK_ENV"] = nil
65
+
66
+ example.call
67
+
68
+ ENV["RACK_ENV"] = old_env
69
+ end
70
+
71
+ it "should enable dotted access" do
72
+ expect(YAML).to receive(:load_file).with("#{Dir.pwd}/config/section_a.yml").and_return({"production" => {a: {b: 1}}, "OTHER" => {aa: 3}})
73
+ expect(YAML).to receive(:load_file).with("#{Dir.pwd}/config/section-b.yml").and_return({"production" => {c: {d: 2}}, "OTHER" => {cc: 4}})
74
+
75
+ subject = Ballast::Configuration.new("section_a", "section-b")
27
76
 
28
- expect(subject.section_a.a.b).to eq(1)
29
- expect(subject.section_b.c).to eq({"d" => 2})
30
- expect { subject.section_a.e }.to raise_error(NoMethodError)
31
- expect { subject.e }.to raise_error(NoMethodError)
77
+ expect(subject.section_a.a.b).to eq(1)
78
+ expect(subject.section_b.c).to eq({"d" => 2})
79
+ expect { subject.section_a.e }.to raise_error(NoMethodError)
80
+ expect { subject.e }.to raise_error(NoMethodError)
81
+ end
32
82
  end
33
83
  end
34
84
  end
@@ -0,0 +1,103 @@
1
+ require "spec_helper"
2
+
3
+ describe Ballast::Emoji do
4
+ before(:example) do
5
+ ::Emoji.instance_variable_set(:@replace_regex, nil)
6
+ end
7
+
8
+ shared_examples_for(:dummy_url) do
9
+ before(:example) do
10
+ ::Emoji.url_mapper = ->(url) { "URL/#{url}" }
11
+ end
12
+ end
13
+
14
+ describe ".replace_regex" do
15
+ it "should return the right regexp" do
16
+ allow(::Emoji).to receive(:unicodes_index).and_return({a: 1, b: 2})
17
+ expect(::Emoji.replace_regex).to eq(/(a|b)/)
18
+ end
19
+ end
20
+
21
+ describe ".replace" do
22
+ include_examples :dummy_url
23
+
24
+ it "should return unicode replaced using the requested method" do
25
+ expect(::Emoji.replace("Phone: \u{1F4F1}, Cat: \u{1F431}, #1: \u{0031}\u{20e3}, Invalid: \u{0000}.", mode: :markup)).to eq("Phone: :iphone:, Cat: :cat:, #1: 1⃣, Invalid: \u0000.")
26
+ expect(Emoji.replace("Phone: \u{1F4F1}, Cat: \u{1F431}, #1: \u{0031}\u{20e3}, Invalid: \u{0000}.", mode: :url)).to eq("Phone: URL/unicode/1f4f1.png, Cat: URL/unicode/1f431.png, #1: 1⃣, Invalid: \u0000.")
27
+ expect(Emoji.replace("Phone: \u{1F4F1}, Cat: \u{1F431}, #1: \u{0031}\u{20e3}, Invalid: \u{0000}.", mode: :image_tag, rel: :tooltip)).to eq("Phone: <img alt=\":iphone:\" class=\"emoji\" rel=\"tooltip\" src=\"URL/unicode/1f4f1.png\" title=\":iphone:\" />, Cat: <img alt=\":cat:\" class=\"emoji\" rel=\"tooltip\" src=\"URL/unicode/1f431.png\" title=\":cat:\" />, #1: 1⃣, Invalid: \u0000.")
28
+ end
29
+
30
+ it "should fallback to markup when the method is not valid" do
31
+ expect(Emoji.replace("Phone: \u{1F4F1}, Cat: \u{1F431}, #1: \u{0031}\u{20e3}, Invalid: \u{0000}.", mode: :invalid)).to eq("Phone: :iphone:, Cat: :cat:, #1: 1⃣, Invalid: \u0000.")
32
+ end
33
+ end
34
+
35
+ describe ".enumerate" do
36
+ include_examples :dummy_url
37
+
38
+ it "should enumerate all the available icons" do
39
+ expect(Emoji.enumerate(keys_method: :raw, values_method: :url)["\u{1f604}"]).to eq("URL/unicode/1f604.png")
40
+ expect(Emoji.enumerate(values_method: :image_tag, rel: :tooltip)[":smile:"]).to eq("<img alt=\":smile:\" class=\"emoji\" rel=\"tooltip\" src=\"URL/unicode/1f604.png\" title=\":smile:\" />")
41
+ end
42
+ end
43
+
44
+ describe ".url_mapper=" do
45
+ it "should set the new mapper" do
46
+ ::Emoji.url_mapper = "A"
47
+ expect(::Emoji.url_mapper).to eq("A")
48
+ ::Emoji.url_mapper = nil
49
+ end
50
+ end
51
+
52
+ describe ".url_mapper" do
53
+ it "should return a default mapper" do
54
+ ::Emoji.url_mapper = nil
55
+ expect(::Emoji.url_mapper.call("URL")).to eq("URL")
56
+ end
57
+
58
+ it "should use the specified mapper" do
59
+ ::Emoji.url_mapper = ->(url) { url * 2 }
60
+ expect(::Emoji.url_mapper.call("URL")).to eq("URLURL")
61
+ end
62
+ end
63
+
64
+ describe ".url_for" do
65
+ it "should return an absolute URL for an image, using the URL mapper" do
66
+ ::Emoji.url_mapper = nil
67
+ expect(::Emoji.url_for("URL")).to eq("URL")
68
+
69
+ ::Emoji.url_mapper = ->(url) { url * 2 }
70
+ expect(::Emoji.url_for("URL")).to eq("URLURL")
71
+
72
+ ::Emoji.url_mapper = nil
73
+ end
74
+ end
75
+
76
+ describe "#markup" do
77
+ it "should return the markup" do
78
+ expect(Emoji.find_by_alias("cat").markup).to eq(":cat:")
79
+ expect(Emoji.find_by_alias("dog").markup).to eq(":dog:")
80
+ expect(Emoji.find_by_unicode("\u{1F430}").markup).to eq(":rabbit:")
81
+ end
82
+ end
83
+
84
+ describe "#url" do
85
+ include_examples :dummy_url
86
+
87
+ it "should return the URL/" do
88
+ expect(Emoji.find_by_alias("cat").url).to eq("URL/unicode/1f431.png")
89
+ expect(Emoji.find_by_alias("dog").url).to eq("URL/unicode/1f436.png")
90
+ expect(Emoji.find_by_unicode("\u{1F430}").url).to eq("URL/unicode/1f430.png")
91
+ end
92
+ end
93
+
94
+ describe "#image_tag" do
95
+ include_examples :dummy_url
96
+
97
+ it "should return an image" do
98
+ expect(Emoji.find_by_alias("cat").image_tag).to eq("<img alt=\":cat:\" class=\"emoji\" rel=\"tooltip\" src=\"URL/unicode/1f431.png\" title=\":cat:\" />")
99
+ expect(Emoji.find_by_alias("cat").image_tag(class: "foo")).to eq("<img alt=\":cat:\" class=\"foo emoji\" rel=\"tooltip\" src=\"URL/unicode/1f431.png\" title=\":cat:\" />")
100
+ expect(Emoji.find_by_alias("cat").image_tag(rel: "tooltip1", class: "abc emoji")).to eq("<img alt=\":cat:\" class=\"abc emoji\" rel=\"tooltip1\" src=\"URL/unicode/1f431.png\" title=\":cat:\" />")
101
+ end
102
+ end
103
+ end
@@ -5,13 +5,13 @@
5
5
 
6
6
  require "spec_helper"
7
7
 
8
- describe Ballast::Errors::BaseError do
9
- subject { Ballast::Errors::BaseError.new("ERROR") }
8
+ describe Ballast::Errors::Base do
9
+ subject { Ballast::Errors::Base.new("ERROR") }
10
10
 
11
11
  describe ".initialize" do
12
- it "should propagate the message also as a response" do
13
- expect(subject.message).to eq("ERROR")
14
- expect(subject.response).to eq("ERROR")
12
+ it "should save the details" do
13
+ expect(subject.message).to eq("")
14
+ expect(subject.details).to eq("ERROR")
15
15
  end
16
16
  end
17
17
  end
@@ -17,15 +17,14 @@ describe Ballast::Middlewares::DefaultHost do
17
17
  end
18
18
 
19
19
  describe "#call" do
20
- before(:each) do
21
- @app = Proc.new { |env| env }
20
+ before(:example) do
22
21
  expect(YAML).to receive(:load_file).with("PATH").and_return({"production" => "HOST"})
23
- ENV["RACK_ENV"] = "production"
24
22
  end
25
23
 
26
- subject { Ballast::Middlewares::DefaultHost.new(@app, "PATH") }
24
+ subject { Ballast::Middlewares::DefaultHost.new(Proc.new { |env| env }, "PATH") }
27
25
 
28
26
  it "should correctly replace the IP if the environment is found" do
27
+ ENV["RACK_ENV"] = "production"
29
28
  expect(subject.call({"SERVER_NAME" => "10.0.0.1", "HTTP_HOST" => "10.0.0.1"})).to eq({"SERVER_NAME" => "HOST", "HTTP_HOST" => "HOST", "ORIG_SERVER_NAME" => "10.0.0.1", "ORIG_HTTP_HOST" => "10.0.0.1"})
30
29
  end
31
30
 
@@ -36,7 +35,6 @@ describe Ballast::Middlewares::DefaultHost do
36
35
 
37
36
  it "should not replace a non IP host" do
38
37
  expect(subject.call({"SERVER_NAME" => "abc", "HTTP_HOST" => "cde"})).to eq({"SERVER_NAME" => "abc", "HTTP_HOST" => "cde"})
39
-
40
38
  end
41
39
  end
42
40
  end
@@ -21,11 +21,11 @@ describe Ballast::RequestDomainMatcher do
21
21
  subject { Ballast::RequestDomainMatcher.new(["A", "ABB", "AC"], "B", "C") { |a| a * 2 } }
22
22
 
23
23
  it "should correctly match a request" do
24
- expect(subject.matches?(OpenStruct.new(host: "A"))).to be_true
25
- expect(subject.matches?(OpenStruct.new(host: "AB"))).to be_true
24
+ expect(subject.matches?(OpenStruct.new(host: "A"))).to be_truthy
25
+ expect(subject.matches?(OpenStruct.new(host: "AB"))).to be_truthy
26
26
  subject.replace_block = nil
27
- expect(subject.matches?(OpenStruct.new(host: "AB"))).to be_true
28
- expect(subject.matches?(OpenStruct.new(host: "DD"))).to be_false
27
+ expect(subject.matches?(OpenStruct.new(host: "AB"))).to be_truthy
28
+ expect(subject.matches?(OpenStruct.new(host: "DD"))).to be_falsey
29
29
  end
30
30
  end
31
31
  end
@@ -0,0 +1,137 @@
1
+ require "spec_helper"
2
+
3
+ describe Ballast::Service::Response do
4
+ describe "#initialize" do
5
+ it "should save attributes" do
6
+ subject = Ballast::Service::Response.new("STATUS", data: "DATA", errors: "ERRORS")
7
+ expect(subject.success).to be_falsey
8
+ expect(subject.data).to eq("DATA")
9
+ expect(subject.errors).to eq(["ERRORS"])
10
+ end
11
+ end
12
+
13
+ describe "#success?" do
14
+ it "should return if the operation succeeded" do
15
+ expect(Ballast::Service::Response.new(true).success?).to be_truthy
16
+ expect(Ballast::Service::Response.new(false).success?).to be_falsey
17
+ end
18
+ end
19
+
20
+ describe "#fail?" do
21
+ it "should return if the operation failed" do
22
+ expect(Ballast::Service::Response.new(true).fail?).to be_falsey
23
+ expect(Ballast::Service::Response.new(false).fail?).to be_truthy
24
+ end
25
+ end
26
+
27
+ describe "#error" do
28
+ it "should return the first error" do
29
+ expect(Ballast::Service::Response.new(true, errors: ["A", "B"]).error).to eq("A")
30
+ expect(Ballast::Service::Response.new(true).error).to be_nil
31
+ end
32
+ end
33
+
34
+ describe "as_ajax_response" do
35
+ it "should create the right response" do
36
+ expect(Ballast::AjaxResponse).to receive(:new).with(status: :ok, data: "DATA", error: nil)
37
+ Ballast::Service::Response.new(true, data: "DATA", errors: "ERRORS").as_ajax_response
38
+
39
+ expect(Ballast::AjaxResponse).to receive(:new).with(status: 403, data: "DATA", error: "ERROR")
40
+ Ballast::Service::Response.new(false, data: "DATA", errors: {status: 403, error: "ERROR"}).as_ajax_response
41
+
42
+ expect(Ballast::AjaxResponse).to receive(:new).with(status: :unknown, data: "DATA", error: "ERROR")
43
+ Ballast::Service::Response.new(false, data: "DATA", errors: "ERROR").as_ajax_response
44
+ end
45
+ end
46
+ end
47
+
48
+ describe Ballast::Service do
49
+ class DummyService < Ballast::Service
50
+ def self.class_success(owner: nil, params: nil, **kwargs)
51
+ "CLASS_DATA"
52
+ end
53
+
54
+ def self.class_failure(**_)
55
+ fail!("NO")
56
+ end
57
+
58
+ def instance_success(params: nil, **kwargs)
59
+ self
60
+ end
61
+
62
+ def instance_failure
63
+ fail!("NO")
64
+ end
65
+ end
66
+
67
+ describe ".call" do
68
+ it "should call the service and return its return value as data of a service response" do
69
+ expect(DummyService).to receive(:class_success).with({owner: "OWNER", params: {a: 1, b: 2}, other: 1}).and_call_original
70
+ response = DummyService.call(:class_success, owner: "OWNER", params: {a: 1, b: 2}, other: 1)
71
+ expect(response).to be_a(Ballast::Service::Response)
72
+ expect(response.success?).to be_truthy
73
+ expect(response.data).to eq("CLASS_DATA")
74
+ end
75
+
76
+ it "should fail when the operation is not supported" do
77
+ response = DummyService.call(:class_unknown)
78
+ expect(response).to be_a(Ballast::Service::Response)
79
+ expect(response.failed?).to be_truthy
80
+ expect(response.data).to be_nil
81
+ expect(response.error).to eq({status: 501, error: "Unsupported operation DummyService.class_unknown."})
82
+ end
83
+
84
+ it "should raise an exception when the invocation failed and it is asked to" do
85
+ expect { DummyService.call(:class_failure, raise_errors: true) }.to raise_error(Ballast::Errors::Failure)
86
+ end
87
+ end
88
+
89
+ describe ".fail!" do
90
+ it "should raise a failure" do
91
+ expect { DummyService.fail!("DETAILS") }.to raise_error(Ballast::Errors::Failure)
92
+ end
93
+
94
+ it "should raise a validation failure" do
95
+ expect { DummyService.fail!("DETAILS", on_validation: true) }.to raise_error(Ballast::Errors::ValidationFailure)
96
+ end
97
+ end
98
+
99
+ describe "#initialize" do
100
+ it "should save the onwer" do
101
+ expect(DummyService.new("OWNER").owner).to eq("OWNER")
102
+ end
103
+ end
104
+
105
+ describe "#call" do
106
+ it "should update the owner, then call the service and return its return value as data of a service response" do
107
+ expect_any_instance_of(DummyService).to receive(:instance_success).with({params: {a: 1, b: 2}, other: 1}).and_call_original
108
+ response = DummyService.new.call(:instance_success, owner: "OWNER", params: {a: 1, b: 2}, other: 1)
109
+ expect(response).to be_a(Ballast::Service::Response)
110
+ expect(response.success?).to be_truthy
111
+ expect(response.data).to be_a(DummyService)
112
+ expect(response.data.owner).to eq("OWNER")
113
+ end
114
+
115
+ it "should fail when the operation is not supported" do
116
+ response = DummyService.new.call(:class_unknown)
117
+ expect(response).to be_a(Ballast::Service::Response)
118
+ expect(response.failed?).to be_truthy
119
+ expect(response.data).to be_nil
120
+ expect(response.error).to eq({status: 501, error: "Unsupported operation DummyService#class_unknown."})
121
+ end
122
+
123
+ it "should raise an exception when the invocation failed and it is asked to" do
124
+ expect { DummyService.new.call(:class_failure, raise_errors: true) }.to raise_error(Ballast::Errors::Failure)
125
+ end
126
+ end
127
+
128
+ describe "#fail!" do
129
+ it "should forward to the class level implementation" do
130
+ allow_any_instance_of(DummyService).to receive(:perform)
131
+
132
+ expect(DummyService).to receive(:fail!).with("ERRORS", on_validation: "ON VALIDATION")
133
+
134
+ DummyService.new.fail!("ERRORS", on_validation: "ON VALIDATION")
135
+ end
136
+ end
137
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,17 +3,5 @@
3
3
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
4
4
  #
5
5
 
6
- require "rubygems"
7
- require "spork"
8
- require "rspec"
9
6
  require "bundler/setup"
10
- require "ballast"
11
-
12
- Spork.prefork do
13
- end
14
-
15
- RSpec.configure do |config|
16
- config.expect_with(:rspec) do |c|
17
- c.syntax = :expect
18
- end
19
- end
7
+ require File.dirname(__FILE__) + "/../lib/ballast"