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 =
'/'.freeze
Instance Attribute Summary collapse
- #base_path ⇒ String readonly
- #headers ⇒ Hash readonly
- #params ⇒ Hash readonly
-
#path ⇒ String
readonly
Return the dataset path.
- #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.
-
.param_encoder ⇒ Object
Return configured param encoder.
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.
-
#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(params) ⇒ 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(params) ⇒ Array<Hash>
Perform an update over HTTP Put.
-
#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.
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 } |
#headers ⇒ Hash (readonly)
109 |
# File 'lib/rom/http/dataset.rb', line 109 option :headers, type: Types::Hash, default: proc { EMPTY_HASH } |
#params ⇒ Hash (readonly)
104 |
# File 'lib/rom/http/dataset.rb', line 104 option :params, 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 } |
#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
119 |
# File 'lib/rom/http/dataset.rb', line 119 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 |
.param_encoder ⇒ Object
Return configured param encoder
74 |
# File 'lib/rom/http/dataset.rb', line 74 setting :param_encoder, URI.method(:encode_www_form), reader: true |
Instance Method Details
#absolute_path ⇒ String
Return the dataset path
194 195 196 |
# File 'lib/rom/http/dataset.rb', line 194 def absolute_path PATH_SEPARATOR + path end |
#add_header(header, value) ⇒ Dataset
Return a new dataset with additional header
230 231 232 |
# File 'lib/rom/http/dataset.rb', line 230 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
332 333 334 |
# File 'lib/rom/http/dataset.rb', line 332 def add_params(new_params) (params: ::ROM::HTTP::Transformer[:deep_merge][params, new_params]) end |
#append_path(append_path) ⇒ Dataset
Return a new dataset with a modified path
286 287 288 |
# File 'lib/rom/http/dataset.rb', line 286 def append_path(append_path) with_path(join_path([:path], append_path)) end |
#delete ⇒ Array<Hash>
Perform an delete over HTTP Delete
377 378 379 |
# File 'lib/rom/http/dataset.rb', line 377 def delete (request_method: :delete).response end |
#delete? ⇒ Boolean
Return true if request method is set to :delete
168 169 170 |
# File 'lib/rom/http/dataset.rb', line 168 def delete? request_method.equal?(:delete) end |
#each {|Hash| ... } ⇒ Enumerator, Array<Hash>
Iterate over each response value
344 345 346 347 |
# File 'lib/rom/http/dataset.rb', line 344 def each(&block) return to_enum unless block_given? response.each(&block) end |
#get? ⇒ Boolean
Return true if request method is set to :get
141 142 143 |
# File 'lib/rom/http/dataset.rb', line 141 def get? request_method.equal?(:get) end |
#insert(params) ⇒ Array<Hash>
Perform an insert over HTTP Post
356 357 358 |
# File 'lib/rom/http/dataset.rb', line 356 def insert(params) (request_method: :post, params: params).response end |
#post? ⇒ Boolean
Return true if request method is set to :post
150 151 152 |
# File 'lib/rom/http/dataset.rb', line 150 def post? request_method.equal?(:post) end |
#put? ⇒ Boolean
Return true if request method is set to :put
159 160 161 |
# File 'lib/rom/http/dataset.rb', line 159 def put? request_method.equal?(:put) end |
#response ⇒ Array<hash>
Execute the current dataset
386 387 388 |
# File 'lib/rom/http/dataset.rb', line 386 def response response_handler.call(request_handler.call(self), self) end |
#update(params) ⇒ Array<Hash>
Perform an update over HTTP Put
367 368 369 |
# File 'lib/rom/http/dataset.rb', line 367 def update(params) (request_method: :put, params: params).response end |
#with_base_path(base_path) ⇒ Dataset
Return a new dataset with a different base path
256 257 258 |
# File 'lib/rom/http/dataset.rb', line 256 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
213 214 215 |
# File 'lib/rom/http/dataset.rb', line 213 def with_headers(headers) (headers: headers) end |
#with_options(opts) ⇒ Dataset
Return a new dataset with additional options
241 242 243 |
# File 'lib/rom/http/dataset.rb', line 241 def (opts) __new__(.merge(opts)) end |
#with_params(params) ⇒ Dataset
Return a new dataset with replaced request parameters
316 317 318 |
# File 'lib/rom/http/dataset.rb', line 316 def with_params(params) (params: params) end |
#with_path(path) ⇒ Dataset
Return a new dataset with a different path
271 272 273 |
# File 'lib/rom/http/dataset.rb', line 271 def with_path(path) (path: path) end |
#with_request_method(request_method) ⇒ Dataset
Return a new dataset with a different request method
300 301 302 |
# File 'lib/rom/http/dataset.rb', line 300 def with_request_method(request_method) (request_method: request_method) end |