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)


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

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

#headersHash (readonly)

Returns:

  • (Hash)


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

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

#paramsHash (readonly)

Returns:

  • (Hash)


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

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



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

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

#request_handlerObject (readonly)

Returns:

  • (Object)


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

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

#request_methodSymbol (readonly)

Returns:

  • (Symbol)


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

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

#response_handlerObject (readonly)

Returns:

  • (Object)


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

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

#uriURI::HTTP (readonly)

Return the dataset's URI

Returns:

  • (URI::HTTP)


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

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


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

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


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

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


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

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



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

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:



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

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:



332
333
334
# File 'lib/rom/http/dataset.rb', line 332

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:



286
287
288
# File 'lib/rom/http/dataset.rb', line 286

def append_path(append_path)
  with_path(join_path(options[:path], append_path))
end

#deleteArray<Hash>

Perform an delete over HTTP Delete

Returns:

  • (Array<Hash>)


377
378
379
# File 'lib/rom/http/dataset.rb', line 377

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

#delete?Boolean

Return true if request method is set to :delete

Returns:

  • (Boolean)


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

Yields:

  • (Hash)

    a dataset tuple

Returns:

  • (Enumerator)

    if no block is given

  • (Array<Hash>)


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

Returns:

  • (Boolean)


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

Returns:

  • (Array<Hash>)


356
357
358
# File 'lib/rom/http/dataset.rb', line 356

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)


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

Returns:

  • (Boolean)


159
160
161
# File 'lib/rom/http/dataset.rb', line 159

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

#responseArray<hash>

Execute the current dataset

Returns:

  • (Array<hash>)


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

Returns:

  • (Array<Hash>)


367
368
369
# File 'lib/rom/http/dataset.rb', line 367

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:



256
257
258
# File 'lib/rom/http/dataset.rb', line 256

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:



213
214
215
# File 'lib/rom/http/dataset.rb', line 213

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:



241
242
243
# File 'lib/rom/http/dataset.rb', line 241

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:



316
317
318
# File 'lib/rom/http/dataset.rb', line 316

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:



271
272
273
# File 'lib/rom/http/dataset.rb', line 271

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:



300
301
302
# File 'lib/rom/http/dataset.rb', line 300

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