Class: ROM::HTTP::Dataset
- Inherits:
-
Object
- Object
- ROM::HTTP::Dataset
- Extended by:
- Dry::Configurable, Initializer
- Includes:
- Enumerable, Memoizable
- Defined in:
- lib/rom/http/dataset.rb
Overview
HTTP Dataset
Represents a specific HTTP collection resource. This class can be subclassed in a specialized HTTP adapter to provide its own response/request handlers or any other configuration that should differ from the defaults.
Constant Summary collapse
- PATH_SEPARATOR =
"/"
Instance Attribute Summary collapse
- #base_path ⇒ String readonly
- #body_params ⇒ Hash readonly
- #headers ⇒ Hash readonly
-
#path ⇒ String
readonly
Return the dataset path.
- #query_params ⇒ Hash readonly
- #request_handler ⇒ Object readonly
- #request_method ⇒ Symbol readonly
- #response_handler ⇒ Object readonly
-
#uri ⇒ URI::HTTP
readonly
Return the dataset’s URI.
Class Method Summary collapse
-
.default_request_handler ⇒ Object
Return configured default request handler.
-
.default_response_handler ⇒ Object
Return configured default response handler.
-
.query_param_encoder ⇒ Object
Return configured query param encoder.
Instance Method Summary collapse
-
#absolute_path ⇒ String
Return the dataset path.
-
#add_body_params(new_body_params) ⇒ Dataset
Return a new dataset with merged request body parameters.
-
#add_header(header, value) ⇒ Dataset
Return a new dataset with additional header.
-
#add_query_params(new_query_params) ⇒ Dataset
Return a new dataset with merged request query parameters.
-
#append_path(append_path) ⇒ Dataset
Return a new dataset with a modified path.
-
#delete ⇒ Array<Hash>
Perform an delete over HTTP Delete.
-
#delete? ⇒ Boolean
Return true if request method is set to :delete.
-
#each {|Hash| ... } ⇒ Enumerator, Array<Hash>
Iterate over each response value.
-
#get? ⇒ Boolean
Return true if request method is set to :get.
-
#insert(attributes) ⇒ Array<Hash>
Perform an insert over HTTP Post.
-
#post? ⇒ Boolean
Return true if request method is set to :post.
-
#put? ⇒ Boolean
Return true if request method is set to :put.
-
#response ⇒ Array<hash>
Execute the current dataset.
-
#update(attributes) ⇒ Array<Hash>
Perform an update over HTTP Put.
-
#with_base_path(base_path) ⇒ Dataset
Return a new dataset with a different base path.
-
#with_body_params(body_params) ⇒ Dataset
Return a new dataset with replaced request body parameters.
-
#with_headers(headers) ⇒ Dataset
Return a new dataset with given headers.
-
#with_options(opts) ⇒ Dataset
Return a new dataset with additional options.
-
#with_path(path) ⇒ Dataset
Return a new dataset with a different path.
-
#with_query_params(query_params) ⇒ Dataset
Return a new dataset with replaced request query parameters.
-
#with_request_method(request_method) ⇒ Dataset
Return a new dataset with a different request method.
Instance Attribute Details
#base_path ⇒ String (readonly)
94 |
# File 'lib/rom/http/dataset.rb', line 94 option :base_path, type: Types::Path, default: proc { EMPTY_STRING } |
#body_params ⇒ Hash (readonly)
109 |
# File 'lib/rom/http/dataset.rb', line 109 option :body_params, type: Types::Hash, default: proc { EMPTY_HASH } |
#headers ⇒ Hash (readonly)
114 |
# File 'lib/rom/http/dataset.rb', line 114 option :headers, type: Types::Hash, default: proc { EMPTY_HASH } |
#path ⇒ String (readonly)
Return the dataset path
99 |
# File 'lib/rom/http/dataset.rb', line 99 option :path, type: Types::Path, default: proc { EMPTY_STRING } |
#query_params ⇒ Hash (readonly)
104 |
# File 'lib/rom/http/dataset.rb', line 104 option :query_params, type: Types::Hash, default: proc { EMPTY_HASH } |
#request_handler ⇒ Object (readonly)
79 |
# File 'lib/rom/http/dataset.rb', line 79 option :request_handler, default: proc { self.class.default_request_handler } |
#request_method ⇒ Symbol (readonly)
89 |
# File 'lib/rom/http/dataset.rb', line 89 option :request_method, type: Types::Symbol, default: proc { :get } |
#response_handler ⇒ Object (readonly)
84 |
# File 'lib/rom/http/dataset.rb', line 84 option :response_handler, default: proc { self.class.default_response_handler } |
#uri ⇒ URI::HTTP (readonly)
Return the dataset’s URI
124 |
# File 'lib/rom/http/dataset.rb', line 124 option :uri, type: Types::String |
Class Method Details
.default_request_handler ⇒ Object
Return configured default request handler
46 |
# File 'lib/rom/http/dataset.rb', line 46 setting :default_request_handler, reader: true |
.default_response_handler ⇒ Object
Return configured default response handler
60 |
# File 'lib/rom/http/dataset.rb', line 60 setting :default_response_handler, reader: true |
.query_param_encoder ⇒ Object
Return configured query param encoder
74 |
# File 'lib/rom/http/dataset.rb', line 74 setting :query_param_encoder, default: URI.method(:encode_www_form), reader: true |
Instance Method Details
#absolute_path ⇒ String
Return the dataset path
199 200 201 |
# File 'lib/rom/http/dataset.rb', line 199 def absolute_path PATH_SEPARATOR + path end |
#add_body_params(new_body_params) ⇒ Dataset
Return a new dataset with merged request body parameters
370 371 372 373 |
# File 'lib/rom/http/dataset.rb', line 370 def add_body_params(new_body_params) (body_params: ::ROM::HTTP::Transformer[:deep_merge][body_params, new_body_params]) end |
#add_header(header, value) ⇒ Dataset
Return a new dataset with additional header
235 236 237 |
# File 'lib/rom/http/dataset.rb', line 235 def add_header(header, value) with_headers(headers.merge(header => value)) end |
#add_query_params(new_query_params) ⇒ Dataset
Return a new dataset with merged request query parameters
337 338 339 340 |
# File 'lib/rom/http/dataset.rb', line 337 def add_query_params(new_query_params) (query_params: ::ROM::HTTP::Transformer[:deep_merge][query_params, new_query_params]) end |
#append_path(append_path) ⇒ Dataset
Return a new dataset with a modified path
291 292 293 |
# File 'lib/rom/http/dataset.rb', line 291 def append_path(append_path) with_path(join_path([:path], append_path)) end |
#delete ⇒ Array<Hash>
Perform an delete over HTTP Delete
423 424 425 |
# File 'lib/rom/http/dataset.rb', line 423 def delete (request_method: :delete).response end |
#delete? ⇒ Boolean
Return true if request method is set to :delete
173 174 175 |
# File 'lib/rom/http/dataset.rb', line 173 def delete? request_method.equal?(:delete) end |
#each {|Hash| ... } ⇒ Enumerator, Array<Hash>
Iterate over each response value
383 384 385 386 387 |
# File 'lib/rom/http/dataset.rb', line 383 def each(&block) return to_enum unless block_given? response.each(&block) end |
#get? ⇒ Boolean
Return true if request method is set to :get
146 147 148 |
# File 'lib/rom/http/dataset.rb', line 146 def get? request_method.equal?(:get) end |
#insert(attributes) ⇒ Array<Hash>
Perform an insert over HTTP Post
396 397 398 399 400 401 |
# File 'lib/rom/http/dataset.rb', line 396 def insert(attributes) ( request_method: :post, body_params: attributes ).response end |
#post? ⇒ Boolean
Return true if request method is set to :post
155 156 157 |
# File 'lib/rom/http/dataset.rb', line 155 def post? request_method.equal?(:post) end |
#put? ⇒ Boolean
Return true if request method is set to :put
164 165 166 |
# File 'lib/rom/http/dataset.rb', line 164 def put? request_method.equal?(:put) end |
#response ⇒ Array<hash>
Execute the current dataset
432 433 434 |
# File 'lib/rom/http/dataset.rb', line 432 def response response_handler.call(request_handler.call(self), self) end |
#update(attributes) ⇒ Array<Hash>
Perform an update over HTTP Put
410 411 412 413 414 415 |
# File 'lib/rom/http/dataset.rb', line 410 def update(attributes) ( request_method: :put, body_params: attributes ).response end |
#with_base_path(base_path) ⇒ Dataset
Return a new dataset with a different base path
261 262 263 |
# File 'lib/rom/http/dataset.rb', line 261 def with_base_path(base_path) (base_path: base_path) end |
#with_body_params(body_params) ⇒ Dataset
Return a new dataset with replaced request body parameters
354 355 356 |
# File 'lib/rom/http/dataset.rb', line 354 def with_body_params(body_params) (body_params: body_params) 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
218 219 220 |
# File 'lib/rom/http/dataset.rb', line 218 def with_headers(headers) (headers: headers) end |
#with_options(opts) ⇒ Dataset
Return a new dataset with additional options
246 247 248 |
# File 'lib/rom/http/dataset.rb', line 246 def (opts) __new__(**.merge(opts)) end |
#with_path(path) ⇒ Dataset
Return a new dataset with a different path
276 277 278 |
# File 'lib/rom/http/dataset.rb', line 276 def with_path(path) (path: path) end |
#with_query_params(query_params) ⇒ Dataset
Return a new dataset with replaced request query parameters
321 322 323 |
# File 'lib/rom/http/dataset.rb', line 321 def with_query_params(query_params) (query_params: query_params) end |
#with_request_method(request_method) ⇒ Dataset
Return a new dataset with a different request method
305 306 307 |
# File 'lib/rom/http/dataset.rb', line 305 def with_request_method(request_method) (request_method: request_method) end |