Class: ROM::HTTP::Dataset

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

Methods included from ClassInterface

default_request_handler, default_response_handler

Instance Method Details

#absolute_pathstring

Return the dataset path

Examples:

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


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

Examples:

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


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

Examples:

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


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

def add_params(new_params)
  # TODO: Should we merge arrays?
  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


206
207
208
# File 'lib/rom/http/dataset.rb', line 206

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

#base_pathString

Return the base path

Examples:

Dataset.new(config, base_path: '/users').base_path
# => 'users'


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

def base_path
  strip_path(super)
end

#deleteArray<Hash>

Perform an delete over HTTP Delete



306
307
308
309
310
# File 'lib/rom/http/dataset.rb', line 306

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

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

Iterate over each response value

Yields:

  • (Hash)

    a dataset tuple



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

#headersHash

Return request headers

Merges default headers from the Gateway configuration and the current Dataset

Examples:

config = { Accepts: 'application/json' }
users  = Dataset.new(config, headers: { 'Cache-Control': 'no-cache' }
users.headers
# => {:Accepts => "application/json", :'Cache-Control' => 'no-cache'}


66
67
68
# File 'lib/rom/http/dataset.rb', line 66

def headers
  config.fetch(:headers, {}).merge(options.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)
  with_options(
    request_method: :post,
    params: params
  ).response
end

#nameString

Return the dataset name



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

def name
  config[:name].to_s
end

#pathString

Return the dataset path

Examples:

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


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

def path
  join_path(base_path, strip_path(options[:path].to_s))
end

#responseArray<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)
  with_options(
    request_method: :put,
    params: params
  ).response
end

#uriString

Return the gateway’s URI

Raises:

  • (Error)

    if the configuration does not contain a 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

Examples:

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


176
177
178
# File 'lib/rom/http/dataset.rb', line 176

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(config, headers: { Accept: 'application/json' })
users.with_headers(:'X-Api-Key' => '1234').headers
# => { :'X-Api-Key' => '1234' }


133
134
135
# File 'lib/rom/http/dataset.rb', line 133

def with_headers(headers)
  __new__(config, options.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 with_options(opts)
  __new__(config, options.merge(opts))
end

#with_params(params) ⇒ Dataset

Return a new dataset with replaced request parameters

Examples:

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


236
237
238
# File 'lib/rom/http/dataset.rb', line 236

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'


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

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)


220
221
222
# File 'lib/rom/http/dataset.rb', line 220

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