Module: Resto

Defined in:
lib/resto.rb,
lib/resto.rb,
lib/resto.rb,
lib/resto/format.rb,
lib/resto/version.rb,
lib/resto/property.rb,
lib/resto/validate.rb,
lib/resto/attributes.rb,
lib/resto/extra/copy.rb,
lib/resto/format/xml.rb,
lib/resto/format/json.rb,
lib/resto/request/uri.rb,
lib/resto/format/plain.rb,
lib/resto/request/base.rb,
lib/resto/property/time.rb,
lib/resto/response/base.rb,
lib/resto/format/default.rb,
lib/resto/request/header.rb,
lib/resto/request/option.rb,
lib/resto/property/string.rb,
lib/resto/request/factory.rb,
lib/resto/validate/length.rb,
lib/resto/property/handler.rb,
lib/resto/property/integer.rb,
lib/resto/extra/assert_hash.rb,
lib/resto/validate/presence.rb,
lib/resto/validate/inclusion.rb,
lib/resto/translator/request_factory.rb,
lib/resto/translator/response_factory.rb

Defined Under Namespace

Modules: ClassMethods, Copy, Format, Property, Request, Response, Translator, Validate Classes: AssertHash, Attributes

Constant Summary collapse

VERSION =
"0.1.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#responseObject

Returns the value of attribute response.



208
209
210
# File 'lib/resto.rb', line 208

def response
  @response
end

Class Method Details

.included(klass) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/resto.rb', line 10

def self.included(klass)
  klass.extend ClassMethods
  klass.class_eval do
    @request  = Request::Base.new
    @response = Response::Base.new.klass(klass)
  end
end

.url(url) ⇒ Object



18
19
20
# File 'lib/resto.rb', line 18

def self.url(url)
  Request::Base.new.url(url)
end

Instance Method Details

#add_error(key, value) ⇒ Object



246
247
248
# File 'lib/resto.rb', line 246

def add_error(key, value)
  @attributes.add_error(key, value)
end

#add_to_requestObject



226
227
228
# File 'lib/resto.rb', line 226

def add_to_request
  lambda { |request| request }
end

#attributesObject



260
261
262
# File 'lib/resto.rb', line 260

def attributes
  @attributes.to_hash
end

#deleteObject



221
222
223
224
# File 'lib/resto.rb', line 221

def delete
  id = attributes.fetch(self.class.resource_id)
  self.class.delete(id, request_path_options) { add_to_request }
end

#errorsObject



250
251
252
# File 'lib/resto.rb', line 250

def errors
  @attributes.errors
end

#getObject Also known as: reload



210
211
212
213
# File 'lib/resto.rb', line 210

def get
  id = attributes.fetch(self.class.resource_id)
  self.class.get(id, request_path_options) { add_to_request }
end

#initialize(attributes) ⇒ Object



202
203
204
205
206
# File 'lib/resto.rb', line 202

def initialize(attributes)
  raise "Must be a hash" unless attributes.is_a?(Hash)

  @attributes = Attributes.new(attributes, self)
end

#putObject



217
218
219
# File 'lib/resto.rb', line 217

def put
  self.class.put(attributes, request_path_options) { add_to_request }
end

#request_path_optionsObject



230
231
232
# File 'lib/resto.rb', line 230

def request_path_options
  {}
end

#update_attributes(attributes) ⇒ Object Also known as: body



254
255
256
# File 'lib/resto.rb', line 254

def update_attributes(attributes)
  tap { @attributes.update_attributes(attributes) }
end

#valid?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/resto.rb', line 234

def valid?
  @attributes.valid? and valid_response?(false)
end

#valid_response?(must_have_a_response_variable = true) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
241
242
243
244
# File 'lib/resto.rb', line 238

def valid_response?(must_have_a_response_variable=true)
  if must_have_a_response_variable
    response ? response.valid? : false
  else
    response ? response.valid? : true
  end
end