Class: ROM::HTTP::Dataset

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rom/http/dataset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ Dataset

Returns a new instance of Dataset.



9
10
11
12
13
14
15
16
# File 'lib/rom/http/dataset.rb', line 9

def initialize(config, options = {})
  @config = config
  @options = {
    request_method: :get,
    path: '',
    params: {}
  }.merge(options)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/rom/http/dataset.rb', line 7

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/rom/http/dataset.rb', line 7

def options
  @options
end

Instance Method Details

#absolute_pathObject



34
35
36
# File 'lib/rom/http/dataset.rb', line 34

def absolute_path
  '/' + path
end

#add_header(header, value) ⇒ Object



50
51
52
# File 'lib/rom/http/dataset.rb', line 50

def add_header(header, value)
  with_headers(headers.merge(header => value))
end

#append_path(path) ⇒ Object



62
63
64
# File 'lib/rom/http/dataset.rb', line 62

def append_path(path)
  with_options(path: options[:path] + '/' + path)
end

#deleteObject



93
94
95
96
97
# File 'lib/rom/http/dataset.rb', line 93

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

#each(&block) ⇒ Object



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

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

#headersObject



22
23
24
# File 'lib/rom/http/dataset.rb', line 22

def headers
  config.fetch(:headers, {}).merge(options.fetch(:headers, {}))
end

#insert(params) ⇒ Object



79
80
81
82
83
84
# File 'lib/rom/http/dataset.rb', line 79

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

#nameObject



26
27
28
# File 'lib/rom/http/dataset.rb', line 26

def name
  config[:name].to_s
end

#paramsObject



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

def params
  options[:params]
end

#pathObject



30
31
32
# File 'lib/rom/http/dataset.rb', line 30

def path
  options[:path].to_s.sub(%r{\A/}, '')
end

#request_methodObject



38
39
40
# File 'lib/rom/http/dataset.rb', line 38

def request_method
  options[:request_method]
end

#responseObject



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

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

#update(params) ⇒ Object



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

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

#uriObject



18
19
20
# File 'lib/rom/http/dataset.rb', line 18

def uri
  config[:uri]
end

#with_headers(headers) ⇒ Object



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

def with_headers(headers)
  self.class.new(config, options.merge(headers: headers))
end

#with_options(opts) ⇒ Object



54
55
56
# File 'lib/rom/http/dataset.rb', line 54

def with_options(opts)
  self.class.new(config, options.merge(opts))
end

#with_params(params) ⇒ Object



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

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

#with_path(path) ⇒ Object



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

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

#with_request_method(request_method) ⇒ Object



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

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