Class: ROM::HTTP::Dataset
- Inherits:
-
Object
- Object
- ROM::HTTP::Dataset
- Extended by:
- Dry::Configurable, ClassInterface, Initializer
- Includes:
- Enumerable
- Defined in:
- lib/rom/http/dataset.rb,
lib/rom/http/dataset/class_interface.rb
Overview
HTTP Dataset
Represents a specific HTTP collection resource
Defined Under Namespace
Modules: ClassInterface
Constant Summary collapse
- PATH_SEPARATOR =
'/'.freeze
Instance Method Summary collapse
-
#absolute_path ⇒ string
Return the dataset path.
-
#add_header(header, value) ⇒ Dataset
Return a new dataset with additional header.
-
#add_params(new_params) ⇒ Dataset
Return a new dataset with merged request parameters.
-
#append_path(append_path) ⇒ Dataset
Return a new dataset with a modified path.
-
#base_path ⇒ String
Return the base path.
-
#delete ⇒ Array<Hash>
Perform an delete over HTTP Delete.
-
#each {|Hash| ... } ⇒ Enumerator, Array<Hash>
Iterate over each response value.
-
#headers ⇒ Hash
Return request headers.
-
#insert(params) ⇒ Array<Hash>
Perform an insert over HTTP Post.
-
#name ⇒ String
Return the dataset name.
-
#path ⇒ String
Return the dataset path.
-
#response ⇒ Array<hash>
Execute the current dataset.
-
#update(params) ⇒ Array<Hash>
Perform an update over HTTP Put.
-
#uri ⇒ String
Return the gateway’s URI.
-
#with_base_path(base_path) ⇒ Dataset
Return a new dataset with a different base path.
-
#with_headers(headers) ⇒ Dataset
Return a new dataset with given headers.
-
#with_options(opts) ⇒ Dataset
Return a new dataset with additional options.
-
#with_params(params) ⇒ Dataset
Return a new dataset with replaced request parameters.
-
#with_path(path) ⇒ Dataset
Return a new dataset with a different path.
-
#with_request_method(request_method) ⇒ Dataset
Return a new dataset with a different request method.
Methods included from ClassInterface
default_request_handler, default_response_handler
Instance Method Details
#absolute_path ⇒ string
Return the dataset path
114 115 116 |
# File 'lib/rom/http/dataset.rb', line 114 def absolute_path PATH_SEPARATOR + path end |
#add_header(header, value) ⇒ Dataset
Return a new dataset with additional header
150 151 152 |
# File 'lib/rom/http/dataset.rb', line 150 def add_header(header, value) with_headers(headers.merge(header => value)) end |
#add_params(new_params) ⇒ Dataset
Return a new dataset with merged request parameters
252 253 254 255 256 257 |
# File 'lib/rom/http/dataset.rb', line 252 def add_params(new_params) # TODO: Should we merge arrays? ( params: ::ROM::HTTP::Transformer[:deep_merge][params, new_params] ) end |
#append_path(append_path) ⇒ Dataset
Return a new dataset with a modified path
206 207 208 |
# File 'lib/rom/http/dataset.rb', line 206 def append_path(append_path) (path: join_path(path, append_path)) end |
#base_path ⇒ String
Return the base path
88 89 90 |
# File 'lib/rom/http/dataset.rb', line 88 def base_path strip_path(super) end |
#delete ⇒ Array<Hash>
Perform an delete over HTTP Delete
306 307 308 309 310 |
# File 'lib/rom/http/dataset.rb', line 306 def delete ( request_method: :delete ).response end |
#each {|Hash| ... } ⇒ Enumerator, Array<Hash>
Iterate over each response value
267 268 269 270 |
# File 'lib/rom/http/dataset.rb', line 267 def each(&block) return to_enum unless block_given? response.each(&block) end |
#headers ⇒ Hash
Return request headers
Merges default headers from the Gateway configuration and the current Dataset
66 67 68 |
# File 'lib/rom/http/dataset.rb', line 66 def headers config.fetch(:headers, {}).merge(.fetch(:headers, {})) end |
#insert(params) ⇒ Array<Hash>
Perform an insert over HTTP Post
279 280 281 282 283 284 |
# File 'lib/rom/http/dataset.rb', line 279 def insert(params) ( request_method: :post, params: params ).response end |
#name ⇒ String
Return the dataset name
75 76 77 |
# File 'lib/rom/http/dataset.rb', line 75 def name config[:name].to_s end |
#path ⇒ String
Return the dataset path
101 102 103 |
# File 'lib/rom/http/dataset.rb', line 101 def path join_path(base_path, strip_path([:path].to_s)) end |
#response ⇒ Array<hash>
Execute the current dataset
317 318 319 |
# File 'lib/rom/http/dataset.rb', line 317 def response response_handler.call(request_handler.call(self), self) end |
#update(params) ⇒ Array<Hash>
Perform an update over HTTP Put
293 294 295 296 297 298 |
# File 'lib/rom/http/dataset.rb', line 293 def update(params) ( request_method: :put, params: params ).response end |
#uri ⇒ String
Return the gateway’s URI
42 43 44 45 46 47 48 49 50 |
# File 'lib/rom/http/dataset.rb', line 42 def uri uri = config.fetch(:uri) { fail Error, '+uri+ configuration missing' } uri = URI(join_path(uri, path)) if request_method == :get && params.any? uri.query = self.class.config.param_encoder.call(params) end uri end |
#with_base_path(base_path) ⇒ Dataset
Return a new dataset with a different base path
176 177 178 |
# File 'lib/rom/http/dataset.rb', line 176 def with_base_path(base_path) (base_path: base_path) end |
#with_headers(headers) ⇒ Dataset
this replaces the dataset’s currently configured headers. To non-destructively add a new header, use #add_header
Return a new dataset with given headers
133 134 135 |
# File 'lib/rom/http/dataset.rb', line 133 def with_headers(headers) __new__(config, .merge(headers: headers)) end |
#with_options(opts) ⇒ Dataset
Return a new dataset with additional options
161 162 163 |
# File 'lib/rom/http/dataset.rb', line 161 def (opts) __new__(config, .merge(opts)) end |
#with_params(params) ⇒ Dataset
Return a new dataset with replaced request parameters
236 237 238 |
# File 'lib/rom/http/dataset.rb', line 236 def with_params(params) (params: params) end |
#with_path(path) ⇒ Dataset
Return a new dataset with a different path
191 192 193 |
# File 'lib/rom/http/dataset.rb', line 191 def with_path(path) (path: path) end |
#with_request_method(request_method) ⇒ Dataset
Return a new dataset with a different request method
220 221 222 |
# File 'lib/rom/http/dataset.rb', line 220 def with_request_method(request_method) (request_method: request_method) end |