Skip to content

Commit 72f4934

Browse files
committed
Add a SetInputFromCookie request mutator
1 parent 12e8e94 commit 72f4934

File tree

6 files changed

+102
-15
lines changed

6 files changed

+102
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.0.16] - 2025-03-28
2+
3+
-
4+
15
## [0.0.15] - 2025-03-28
26

37
- Implement MoveAttributeToCookie response mutator

Gemfile.lock

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
foobara-http-command-connector (0.0.15)
4+
foobara-http-command-connector (0.0.16)
55
foobara (~> 0.0.88)
66

77
GEM
@@ -54,7 +54,7 @@ GEM
5454
foobara-type-generator
5555
foobara-typescript-react-command-form-generator
5656
foobara-typescript-remote-command-generator
57-
foobara (0.0.88)
57+
foobara (0.0.90)
5858
bigdecimal
5959
foobara-lru-cache (~> 0.0.2)
6060
foobara-util (~> 0.0.11)
@@ -88,9 +88,8 @@ GEM
8888
foobara-rack-connector-generator (0.0.9)
8989
foobara
9090
foobara-files-generator
91-
foobara-redis-crud-driver-generator (0.0.3)
92-
foobara
93-
foobara-files-generator
91+
foobara-redis-crud-driver-generator (0.0.4)
92+
foobara-files-generator (~> 0.0.1)
9493
foobara-remote-imports-generator (0.0.3)
9594
foobara
9695
foobara-files-generator
@@ -103,11 +102,10 @@ GEM
103102
foobara-rubocop-rules (0.0.8)
104103
rubocop
105104
rubocop-rspec
106-
foobara-sh-cli-connector (0.0.12)
107-
foobara
108-
foobara-sh-cli-connector-generator (0.0.3)
109-
foobara
110-
foobara-files-generator
105+
foobara-sh-cli-connector (0.0.13)
106+
foobara (~> 0.0.88)
107+
foobara-sh-cli-connector-generator (0.0.4)
108+
foobara-files-generator (~> 0.0.1)
111109
foobara-spec-helpers (0.0.4)
112110
foobara-util
113111
foobara-type-generator (0.0.4)
@@ -182,7 +180,7 @@ GEM
182180
rb-fsevent (0.11.2)
183181
rb-inotify (0.11.1)
184182
ffi (~> 1.0)
185-
rdoc (6.13.0)
183+
rdoc (6.13.1)
186184
psych (>= 4.0.0)
187185
regexp_parser (2.10.0)
188186
reline (0.6.0)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
RSpec.describe Foobara::CommandConnectors::Http::SetInputFromCookie do
2+
let(:command_class) do
3+
stub_class(:SomeCommand, Foobara::Command) do
4+
inputs foo: :string, bar: :string
5+
result :duck
6+
7+
def execute
8+
inputs
9+
end
10+
end
11+
end
12+
13+
let(:request_mutator) do
14+
described_class.for(:foo, "foo_cookie")
15+
end
16+
17+
let(:command_connector) { Foobara::CommandConnectors::Http.new }
18+
let(:response) { command_connector.run(path:, query_string:, cookies:) }
19+
let(:query_string) { "bar=barvalue" }
20+
let(:path) { "/run/#{command_class.full_command_name}" }
21+
let(:cookies) do
22+
{ "foo_cookie" => "foovalue" }
23+
end
24+
25+
before do
26+
command_connector.connect(command_class, request_mutators: request_mutator)
27+
end
28+
29+
it "moves the cookie to an input" do
30+
expect(response.status).to eq(200)
31+
expect(JSON.parse(response.body)).to eq("bar" => "barvalue", "foo" => "foovalue")
32+
end
33+
34+
it "makes changes in the manifest" do
35+
manifest = command_connector.foobara_manifest
36+
37+
expect(manifest[:command][:SomeCommand][:inputs_type]).to eq(
38+
type: :attributes,
39+
element_type_declarations: {
40+
bar: { type: :string }
41+
}
42+
)
43+
end
44+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module Foobara
2+
module CommandConnectors
3+
class Http < CommandConnector
4+
class SetInputFromCookie < RequestMutator
5+
class << self
6+
attr_accessor :attribute_name, :cookie_name
7+
8+
def for(attribute_name, cookie_name = attribute_name)
9+
subclass = Class.new(self)
10+
11+
subclass.attribute_name = attribute_name
12+
subclass.cookie_name = cookie_name
13+
14+
subclass
15+
end
16+
end
17+
18+
attr_writer :attribute_name, :cookie_name
19+
20+
def inputs_type_from(inputs_type)
21+
new_declaration = TypeDeclarations::Attributes.reject(inputs_type.declaration_data, attribute_name)
22+
Domain.current.foobara_type_from_declaration(new_declaration)
23+
end
24+
25+
def mutate(request)
26+
cookie_value = request.cookies[cookie_name]
27+
request.inputs[attribute_name] = cookie_value
28+
end
29+
30+
def attribute_name
31+
@attribute_name ||= self.class.attribute_name
32+
end
33+
34+
def cookie_name
35+
@cookie_name ||= self.class.cookie_name
36+
end
37+
end
38+
end
39+
end
40+
end

src/http/response_mutators/move_attribute_to_cookie.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ def for(attribute_name, cookie_name = attribute_name, **cookie_opts)
1919
attr_writer :attribute_name, :cookie_name, :cookie_opts
2020

2121
def result_type_from(result_type)
22-
AttributesTransformers.reject(result_type.declaration_data)
2322
new_declaration = TypeDeclarations::Attributes.reject(result_type.declaration_data, attribute_name)
2423

2524
Domain.current.foobara_type_from_declaration(new_declaration)
2625
end
2726

2827
def mutate(response)
29-
cookie_value = response.body.delete(attribute_name)
30-
response.add_cookie(cookie_name, cookie_value, cookie_opts)
28+
if response.command.success?
29+
cookie_value = response.body.delete(attribute_name)
30+
response.add_cookie(cookie_name, cookie_value, cookie_opts)
31+
end
3132
end
3233

3334
def attribute_name

version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Foobara
22
module HttpCommandConnector
3-
VERSION = "0.0.15".freeze
3+
VERSION = "0.0.16".freeze
44

55
local_ruby_version = File.read("#{__dir__}/.ruby-version").chomp
66
local_ruby_version_minor = local_ruby_version[/\A(\d+\.\d+)\.\d+\z/, 1]

0 commit comments

Comments
 (0)