Skip to content

Commit 4eb3844

Browse files
committed
Simplify apps API contract
1 parent 28db2dd commit 4eb3844

9 files changed

Lines changed: 56 additions & 41 deletions

File tree

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ Style/Documentation:
1212

1313
Style/StringLiterals:
1414
EnforcedStyle: double_quotes
15+
16+
Style/SymbolArray:
17+
EnforcedStyle: brackets

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,23 @@ List available apps:
3939

4040
```ruby
4141
apps = PreflightQA::Apps.list
42-
apps.first.name
42+
apps["apple_mail_mac"]["name"]
43+
apps["apple_mail_mac"]["modes"]
4344
```
4445

4546
Create a screenshot submission:
4647

4748
```ruby
49+
apps = PreflightQA::Apps.list
50+
4851
submission = PreflightQA::Screenshots.create(
4952
subject: "My Test Email",
5053
html: "<html><body>Hello world</body></html>",
51-
apps: ["apple_mail", "outlook_mac"],
52-
webhook_url: "https://example.com/preflight/webhook",
53-
mode: "dark"
54+
apps: {
55+
apple_mail_mac: [:light, :dark],
56+
outlook_mac: [:light]
57+
},
58+
webhook_url: "https://example.com/preflight/webhook"
5459
)
5560

5661
submission.uuid
@@ -67,7 +72,7 @@ screenshots.each do |screenshot|
6772
end
6873
```
6974

70-
`PreflightQA::Apps.list` returns `Array<PreflightQA::App>`.
75+
`PreflightQA::Apps.list` returns the raw `data["apps"]` hash from the API.
7176

7277
`PreflightQA::Screenshots.create` returns `PreflightQA::Submission`.
7378

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ require "rubocop/rake_task"
99

1010
RuboCop::RakeTask.new
1111

12-
task default: %i[spec rubocop]
12+
task default: [:spec, :rubocop]

lib/preflight_qa.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
require_relative "preflight_qa/configuration"
55
require_relative "preflight_qa/errors"
66
require_relative "preflight_qa/client"
7-
require_relative "preflight_qa/app"
87
require_relative "preflight_qa/submission"
98
require_relative "preflight_qa/screenshot"
109
require_relative "preflight_qa/apps"

lib/preflight_qa/app.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/preflight_qa/apps.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Apps
77

88
def list
99
payload = PreflightQA.client.get("/apps")
10-
apps_from(payload).map { |slug, app| App.from_payload(slug, app) }
10+
apps_from(payload)
1111
end
1212

1313
def apps_from(payload)

lib/preflight_qa/screenshots.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,30 @@ def build_create_payload(subject:, html:, options:)
3838
payload = { subject: subject, html: html }
3939
append_optional_field(payload, :apps, options)
4040
append_optional_field(payload, :webhook_url, options)
41-
append_optional_field(payload, :mode, options)
42-
payload.merge(options.except(:apps, :webhook_url, :mode))
41+
payload.merge(options.except(:apps, :webhook_url))
4342
end
4443
private_class_method :build_create_payload
4544

4645
def append_optional_field(payload, key, options)
4746
return unless options.key?(key)
4847
return if options[key].nil?
4948

50-
payload[key] = options[key]
49+
payload[key] = key == :apps ? normalize_apps(options[key]) : options[key]
5150
end
5251
private_class_method :append_optional_field
52+
53+
def normalize_apps(apps)
54+
raise ArgumentError, "apps must be a hash of app keys to mode arrays" unless apps.is_a?(Hash)
55+
56+
apps.each_with_object({}) do |(app, modes), normalized|
57+
normalized[app.to_s] = normalize_modes(modes)
58+
end
59+
end
60+
private_class_method :normalize_apps
61+
62+
def normalize_modes(modes)
63+
Array(modes).map(&:to_s)
64+
end
65+
private_class_method :normalize_modes
5366
end
5467
end

spec/preflight_qa/apps_spec.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
RSpec.describe PreflightQA::Apps do
4-
it "returns typed app objects" do
4+
it "returns the raw app hash" do
55
client = instance_double(PreflightQA::Client)
66
allow(PreflightQA).to receive(:client).and_return(client)
77
allow(client).to receive(:get).with("/apps").and_return(
@@ -20,17 +20,18 @@
2020
result = described_class.list
2121

2222
expect(result).to eq(
23-
[
24-
PreflightQA::App.new(
25-
app: "apple_mail_mac",
26-
name: "Apple Mail (Mac)",
27-
modes: %w[light dark],
28-
raw: {
29-
"name" => "Apple Mail (Mac)",
30-
"modes" => %w[light dark]
31-
}
32-
)
33-
]
23+
{
24+
"apple_mail_mac" => {
25+
"name" => "Apple Mail (Mac)",
26+
"modes" => %w[light dark]
27+
}
28+
}
29+
)
30+
expect(result["apple_mail_mac"]).to eq(
31+
{
32+
"name" => "Apple Mail (Mac)",
33+
"modes" => %w[light dark]
34+
}
3435
)
3536
end
3637
end

spec/preflight_qa/screenshots_spec.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,17 @@
4545
body: {
4646
subject: "My Test Email",
4747
html: "<html>test</html>",
48-
apps: ["apple_mail"],
48+
apps: { "apple_mail_mac" => %w[light dark] },
4949
webhook_url: "https://example.test/webhook",
50-
mode: "dark",
5150
locale: "en-GB"
5251
}
5352
).and_return(submission_payload)
5453

5554
result = described_class.create(
5655
subject: "My Test Email",
5756
html: "<html>test</html>",
58-
apps: ["apple_mail"],
57+
apps: { apple_mail_mac: [:light, :dark] },
5958
webhook_url: "https://example.test/webhook",
60-
mode: "dark",
6159
locale: "en-GB"
6260
)
6361

@@ -71,6 +69,16 @@
7169
)
7270
end
7371

72+
it "raises when apps is not a hash" do
73+
expect do
74+
described_class.create(
75+
subject: "My Test Email",
76+
html: "<html>test</html>",
77+
apps: ["apple_mail_mac"]
78+
)
79+
end.to raise_error(ArgumentError, /apps must be a hash/)
80+
end
81+
7482
it "finds screenshots by uuid" do
7583
allow(client).to receive(:get)
7684
.with("/screenshots/1b153496-f702-4f89-85b4-cd1a9516cbbe")

0 commit comments

Comments
 (0)