Module: Shutl::Resource::Rest

Extended by:
HTTParty
Includes:
ActiveModel::Serialization
Defined in:
lib/shutl/resource/rest.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



56
57
58
59
60
61
# File 'lib/shutl/resource/rest.rb', line 56

def method_missing(method, *args, &block)
  if self.instance_variables.include?(:"@#{method}")
    return self.instance_variable_get(:"@#{method}")
  end
  super
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



75
76
77
# File 'lib/shutl/resource/rest.rb', line 75

def errors
  @errors
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/shutl/resource/rest.rb', line 14

def response
  @response
end

Class Method Details

.included(base) ⇒ Object



16
17
18
19
20
21
# File 'lib/shutl/resource/rest.rb', line 16

def self.included(base)
  base.send :extend,  Shutl::Resource::RestClassMethods

  base.send :resource_name, base.name.split('::').last.underscore
  base.send :resource_id,   :id
end

Instance Method Details

#as_json(_) ⇒ Object



28
29
30
# File 'lib/shutl/resource/rest.rb', line 28

def as_json(_)
  attributes
end

#attributesObject



85
86
87
88
89
# File 'lib/shutl/resource/rest.rb', line 85

def attributes
  (instance_variables - [:@response]).inject({}.with_indifferent_access) do |h, var|
    h.merge( { var.to_s.gsub('@','').to_sym => instance_variable_get(var)})
  end
end

#destroy(options) ⇒ Object



52
53
54
# File 'lib/shutl/resource/rest.rb', line 52

def destroy options
  self.class.destroy self, options
end

#initialize(args = {}, response = nil) ⇒ Object



23
24
25
26
# File 'lib/shutl/resource/rest.rb', line 23

def initialize(args = {}, response=nil)
  update_attributes args
  @response = response
end

#parsedObject



67
68
69
# File 'lib/shutl/resource/rest.rb', line 67

def parsed
  response.parsed_response
end

#resource_idObject



81
82
83
# File 'lib/shutl/resource/rest.rb', line 81

def resource_id
  instance_variable_get :"@#{self.class.resource_id_name}"
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/shutl/resource/rest.rb', line 63

def respond_to? method
  self.instance_variables.include?(:"@#{method}") ? true : super
end

#save(options = {}) ⇒ Object



48
49
50
# File 'lib/shutl/resource/rest.rb', line 48

def save options={}
  self.class.save self, options
end

#statusObject



71
72
73
# File 'lib/shutl/resource/rest.rb', line 71

def status
  response.code
end

#to_json(options = nil) ⇒ Object



32
33
34
35
36
# File 'lib/shutl/resource/rest.rb', line 32

def to_json(options = nil)
  {
    :"#{prefix}" => attributes
  }.to_json(options)
end

#update!(attrs) ⇒ Object



42
43
44
45
46
# File 'lib/shutl/resource/rest.rb', line 42

def update!(attrs)
  new_attributes = attributes.merge attrs
  update_attributes(self.class.add_resource_id_to new_attributes)
  save
end

#update_attributes(attrs) ⇒ Object



38
39
40
# File 'lib/shutl/resource/rest.rb', line 38

def update_attributes(attrs)
  attrs.each { |a, v| instance_variable_set(:"@#{a}", v) }
end

#valid?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/shutl/resource/rest.rb', line 77

def valid?
  errors.blank?
end