pragma-operation 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1a35deac8b18a13c8f5e7cd9b4ee1372afb6321
4
- data.tar.gz: 6f2abba3db47e143d06c407fe33a1416f44cffac
3
+ metadata.gz: a892b513da3edcb6fd3fbf84964cb4773671d4f5
4
+ data.tar.gz: 2dd06285a00dcb6ed694d8050efde1c346e985c6
5
5
  SHA512:
6
- metadata.gz: 1af71177cbec88e40273ebc84fd5118d2bf2d894df7823791850e50d12dcd5ca0e2ba649b6c4509ff71708bfdafb3bd13ba78064cdc78709f9a3e59267c1626d
7
- data.tar.gz: fd674718c93267c02e48e6ce035ff0595a1cb6db79ed047938a762ae4e555ed35e9be6348ec02d5b0aed038fe428f64c738fc10edd7c62498b9c24bec37976bb
6
+ metadata.gz: 545a8a3f2db0168d4bfa8b1ebfd14e212dc1f44de3408c2f65ca0320e8dff52cefa274abbd9e16701af7755c14b55983989759cc60ebf093e181fea83718824a
7
+ data.tar.gz: 0ca767a0c19c14961a2b78be730b39d568d777890156982e67bf98354d56a77541e045145766798343e38358074167f2d242d75dde56f5e87df4af4a621e0f1f
@@ -9,7 +9,14 @@ module API
9
9
  module Operation
10
10
  class Create < Pragma::Operation::Base
11
11
  def call
12
- respond_with status: :ok, resource: { pong: params[:pong] }
12
+ # The `status` parameter is optional (the default is `:ok`).
13
+ respond_with(
14
+ status: :ok,
15
+ resource: { pong: params[:pong] },
16
+ headers: {
17
+ 'X-Ping-Time' => Time.now.to_i
18
+ }
19
+ )
13
20
  end
14
21
  end
15
22
  end
@@ -25,6 +32,7 @@ result = API::V1::Ping::Operation::Create.call(params: { pong: 'HELLO' })
25
32
 
26
33
  result.status # => :ok
27
34
  result.resource # => { pong: 'HELLO' }
35
+ result.headers # => { 'X-Ping-Time' => 1482927872 }
28
36
  ```
29
37
 
30
38
  As you can see, an operation takes parameters as input and responds with:
@@ -114,27 +114,26 @@ module Pragma
114
114
 
115
115
  # Sets the status and resource to respond with.
116
116
  #
117
- # You can achieve the same result by setting +context.status+ and +context.resource+ wherever
118
- # you want in {#call}.
117
+ # You can achieve the same result by setting +context.status+, +context.headers+ and
118
+ # +context.resource+ wherever you want in {#call}.
119
119
  #
120
120
  # Note that calling this method doesn't halt the execution of the operation and that this
121
121
  # method can be called multiple times, overriding the previous context.
122
122
  #
123
123
  # @param status [Integer|Symbol] an HTTP status code
124
+ # @param headers [Hash] HTTP headers
124
125
  # @param resource [Object] an object responding to +#to_json+
125
- def respond_with(status:, resource:)
126
+ def respond_with(status: :ok, headers: {}, resource:)
126
127
  context.status = status
128
+ context.headers = headers.to_h
127
129
  context.resource = resource
128
130
  end
129
131
 
130
132
  # Same as {#respond_with}, but also halts the execution of the operation.
131
133
  #
132
- # @param status [Integer|Symbol] an HTTP status code
133
- # @param resource [Object] an object responding to +#to_json+
134
- #
135
134
  # @see #respond_with
136
- def respond_with!(status:, resource:)
137
- respond_with status: status, resource: resource
135
+ def respond_with!(*args)
136
+ respond_with *args
138
137
  fail Halt
139
138
  end
140
139
 
@@ -187,6 +186,7 @@ module Pragma
187
186
 
188
187
  def setup_context
189
188
  context.params ||= {}
189
+ context.headers = {}
190
190
  end
191
191
 
192
192
  def handle_halt(interactor)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Pragma
2
3
  module Operation
3
4
  # Provides integration with {https://github.com/pragmarb/pragma-decorator Pragma::Decorator}.
@@ -59,7 +59,6 @@ module Pragma
59
59
  #
60
60
  # @return [Boolean] whether the operation is valid
61
61
  def validate(validatable)
62
- # rubocop:disable Metrics/LineLength
63
62
  contract = if validatable.is_a?(self.class.contract_klass)
64
63
  validatable
65
64
  else
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Pragma
3
3
  module Operation
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pragma-operation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Desantis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-27 00:00:00.000000000 Z
11
+ date: 2016-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: interactor