Class: ROM::HTTP::Dataset

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_pathString (readonly)

Returns:

  • (String)

92
# File 'lib/rom/http/dataset.rb', line 92

option :base_path, type: Types::Path, default: proc { EMPTY_STRING }

#headersHash (readonly)

Returns:

  • (Hash)

107
# File 'lib/rom/http/dataset.rb', line 107

option :headers, type: Types::Hash, default: proc { EMPTY_HASH }

#paramsHash (readonly)

Returns:

  • (Hash)

102
# File 'lib/rom/http/dataset.rb', line 102

option :params, type: Types::Hash, default: proc { EMPTY_HASH }

#pathString (readonly)

Return the dataset path

Examples:

Dataset.new(path: '/users').path
# => 'users'

Returns:

  • (String)

    the dataset path, without a leading slash


97
# File 'lib/rom/http/dataset.rb', line 97

option :path, type: Types::Path, default: proc { EMPTY_STRING }

#request_handlerObject (readonly)

Returns:

  • (Object)

77
# File 'lib/rom/http/dataset.rb', line 77

option :request_handler, default: proc { self.class.default_request_handler }

#request_methodSymbol (readonly)

Returns:

  • (Symbol)

87
# File 'lib/rom/http/dataset.rb', line 87

option :request_method, type: Types::Symbol, default: proc { :get }

#response_handlerObject (readonly)

Returns:

  • (Object)

82
# File 'lib/rom/http/dataset.rb', line 82

option :response_handler, default: proc { self.class.default_response_handler }

#uriURI::HTTP (readonly)

Return the dataset's URI

Returns:

  • (URI::HTTP)

117
# File 'lib/rom/http/dataset.rb', line 117

option :uri, type: Types::String

Class Method Details

.default_request_handlerObject

Return configured default request handler

Examples:

class MyDataset < ROM::HTTP::Dataset
  configure do |config|
    config.default_request_handler = MyRequestHandler
  end
end

MyDataset.default_request_handler # MyRequestHandler
MyDataset.new(uri: "http://localhost").request_handler # MyRequestHandler

44
# File 'lib/rom/http/dataset.rb', line 44

setting :default_request_handler, reader: true

.default_response_handlerObject

Return configured default response handler

Examples:

class MyDataset < ROM::HTTP::Dataset
  configure do |config|
    config.default_response_handler = MyResponseHandler
  end
end

MyDataset.default_response_handler # MyResponseHandler
MyDataset.new(uri: "http://localhost").response_handler # MyResponseHandler

58
# File 'lib/rom/http/dataset.rb', line 58

setting :default_response_handler, reader: true

.param_encoderObject

Return configured param encoder

Examples:

class MyDataset < ROM::HTTP::Dataset
  configure do |config|
    config.param_encoder = MyParamEncoder
  end
end

MyDataset.param_encoder # MyParamEncoder
MyDataset.new(uri: "http://localhost").param_encoder # MyParamEncoder

72
# File 'lib/rom/http/dataset.rb', line 72

setting :param_encoder, URI.method(:encode_www_form), reader: true

Instance Method Details

#absolute_pathString

Return the dataset path

Examples:

Dataset.new(path: '/users').path
# => '/users'

Returns:

  • (String)

    the dataset path, with leading slash


192
193
194
# File 'lib/rom/http/dataset.rb', line 192

def absolute_path
  PATH_SEPARATOR + path
end

#add_header(header, value) ⇒ Dataset

Return a new dataset with additional header

Examples:

users = Dataset.new(headers: { Accept: 'application/json' })
users.add_header(:'X-Api-Key', '1234').headers
# => { :Accept => 'application/json', :'X-Api-Key' => '1234' }

Parameters:

  • header (Symbol)

    the HTTP header to add

  • value (String)

    the header value

Returns:


228
229
230
# File 'lib/rom/http/dataset.rb', line 228

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

Examples:

users = Dataset.new(params: { uid: 33 })
users.add_params(login: 'jdoe').params
# => { uid: 33, :login => 'jdoe' }

Parameters:

  • params (Hash)

    the new request parameters to add

Returns:


330
331
332
# File 'lib/rom/http/dataset.rb', line 330

def add_params(new_params)
  with_options(params: ::ROM::HTTP::Transformer[:deep_merge][params, new_params])
end

#append_path(append_path) ⇒ Dataset

Return a new dataset with a modified path

Examples:

users.append_path('profiles').path
# => users/profiles

Parameters:

  • path (String)

    new path fragment

Returns:


284
285
286
# File 'lib/rom/http/dataset.rb', line 284

def append_path(append_path)
  with_options(path: join_path(path, append_path))
end

#deleteArray<Hash>

Perform an delete over HTTP Delete

Returns:

  • (Array<Hash>)

375
376
377
# File 'lib/rom/http/dataset.rb', line 375

def delete
  with_options(request_method: :delete).response
end

#delete?Boolean

Return true if request method is set to :delete

Returns:

  • (Boolean)

166
167
168
# File 'lib/rom/http/dataset.rb', line 166

def delete?
  request_method.equal?(:delete)
end

#each {|Hash| ... } ⇒ Enumerator, Array<Hash>

Iterate over each response value

Yields:

  • (Hash)

    a dataset tuple

Returns:

  • (Enumerator)

    if no block is given

  • (Array<Hash>)

342
343
344
345
# File 'lib/rom/http/dataset.rb', line 342

def each(&block)
  return to_enum unless block_given?
  response.each(&block)
end

#get?Boolean

Return true if request method is set to :get

Returns:

  • (Boolean)

139
140
141
# File 'lib/rom/http/dataset.rb', line 139

def get?
  request_method.equal?(:get)
end

#insert(params) ⇒ Array<Hash>

Perform an insert over HTTP Post

Returns:

  • (Array<Hash>)

354
355
356
# File 'lib/rom/http/dataset.rb', line 354

def insert(params)
  with_options(request_method: :post, params: params).response
end

#post?Boolean

Return true if request method is set to :post

Returns:

  • (Boolean)

148
149
150
# File 'lib/rom/http/dataset.rb', line 148

def post?
  request_method.equal?(:post)
end

#put?Boolean

Return true if request method is set to :put

Returns:

  • (Boolean)

157
158
159
# File 'lib/rom/http/dataset.rb', line 157

def put?
  request_method.equal?(:put)
end

#responseArray<hash>

Execute the current dataset

Returns:

  • (Array<hash>)

384
385
386
# File 'lib/rom/http/dataset.rb', line 384

def response
  response_handler.call(request_handler.call(self), self)
end

#update(params) ⇒ Array<Hash>

Perform an update over HTTP Put

Returns:

  • (Array<Hash>)

365
366
367
# File 'lib/rom/http/dataset.rb', line 365

def update(params)
  with_options(request_method: :put, params: params).response
end

#with_base_path(base_path) ⇒ Dataset

Return a new dataset with a different base path

Examples:

users.with_base_path('/profiles').base_path
# => 'profiles'

Parameters:

  • base_path (String)

    the new base request path

Returns:


254
255
256
# File 'lib/rom/http/dataset.rb', line 254

def with_base_path(base_path)
  with_options(base_path: base_path)
end

#with_headers(headers) ⇒ Dataset

Note:

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

Examples:

users = Dataset.new(headers: { Accept: 'application/json' })
users.with_headers(:'X-Api-Key' => '1234').headers
# => { :'X-Api-Key' => '1234' }

Parameters:

  • headers (Hash)

    The new headers

Returns:


211
212
213
# File 'lib/rom/http/dataset.rb', line 211

def with_headers(headers)
  with_options(headers: headers)
end

#with_options(opts) ⇒ Dataset

Return a new dataset with additional options

Parameters:

  • opts (Hash)

    the new options to add

Returns:


239
240
241
# File 'lib/rom/http/dataset.rb', line 239

def with_options(opts)
  __new__(options.merge(opts))
end

#with_params(params) ⇒ Dataset

Return a new dataset with replaced request parameters

Examples:

users = Dataset.new(params: { uid: 33 })
users.with_params(login: 'jdoe').params
# => { :login => 'jdoe' }

Parameters:

  • params (Hash)

    the new request parameters

Returns:


314
315
316
# File 'lib/rom/http/dataset.rb', line 314

def with_params(params)
  with_options(params: params)
end

#with_path(path) ⇒ Dataset

Return a new dataset with a different path

Examples:

users.with_path('/profiles').path
# => 'profiles'

Parameters:

  • path (String)

    the new request path

Returns:


269
270
271
# File 'lib/rom/http/dataset.rb', line 269

def with_path(path)
  with_options(path: path)
end

#with_request_method(request_method) ⇒ Dataset

Return a new dataset with a different request method

Examples:

users.request_method(:put)

Parameters:

  • request_method (Symbol)

    the new HTTP verb

Returns:


298
299
300
# File 'lib/rom/http/dataset.rb', line 298

def with_request_method(request_method)
  with_options(request_method: request_method)
end