Class: ROM::HTTP::Dataset

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Options
Defined in:
lib/rom/http/dataset.rb,
lib/rom/http/dataset/response_transformers/schemad.rb,
lib/rom/http/dataset/response_transformers/schemaless.rb

Defined Under Namespace

Modules: ResponseTransformers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Dataset.



31
32
33
34
35
# File 'lib/rom/http/dataset.rb', line 31

def initialize(config, options = {})
  @config = config
  @response_transformer = ResponseTransformers::Schemaless.new
  super(options)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/rom/http/dataset.rb', line 11

def config
  @config
end

Class Method Details

.default_request_handler(handler = Undefined) ⇒ Object



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

def default_request_handler(handler = Undefined)
  return @default_request_handler if Undefined === handler
  @default_request_handler = handler
end

.default_response_handler(handler = Undefined) ⇒ Object



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

def default_response_handler(handler = Undefined)
  return @default_response_handler if Undefined === handler
  @default_response_handler = handler
end

Instance Method Details

#absolute_pathObject



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

def absolute_path
  '/' + path
end

#add_header(header, value) ⇒ Object



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

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

#append_path(path) ⇒ Object



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

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

#deleteObject



117
118
119
120
121
# File 'lib/rom/http/dataset.rb', line 117

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

#each(&block) ⇒ Object



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

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

#headersObject



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

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

#insert(params) ⇒ Object



103
104
105
106
107
108
# File 'lib/rom/http/dataset.rb', line 103

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

#nameObject



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

def name
  config[:name].to_s
end

#pathObject



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

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

#project(*args) ⇒ Object



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

def project(*args)
  projections = args.first.is_a?(::Array) ? args.first : args

  with_options(
    projections: (self.projections + projections)
  )
end

#responseObject



123
124
125
126
127
128
# File 'lib/rom/http/dataset.rb', line 123

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

#response_transformer(transformer = Undefined) ⇒ Object



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

def response_transformer(transformer = Undefined)
  return @response_transformer if Undefined === transformer
  @response_transformer = transformer
end

#update(params) ⇒ Object



110
111
112
113
114
115
# File 'lib/rom/http/dataset.rb', line 110

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

#uriObject



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

def uri
  config.fetch(:uri) { fail Error, ':uri configuration missing' }
end

#with_headers(headers) ⇒ Object



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

def with_headers(headers)
  __new__(config, options.merge(headers: headers))
end

#with_options(opts) ⇒ Object



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

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

#with_params(params) ⇒ Object



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

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

#with_path(path) ⇒ Object



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

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

#with_request_method(request_method) ⇒ Object



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

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