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



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

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.



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

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
22
23
24
25
26
27
# File 'lib/shutl/resource/rest.rb', line 16

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

  base.send :headers, {
    'Accept'        => 'application/json',
    'Content-Type'  => 'application/json'
  }

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

Instance Method Details

#as_json(_) ⇒ Object



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

def as_json(_)
  attributes
end

#attributesObject



91
92
93
94
95
# File 'lib/shutl/resource/rest.rb', line 91

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



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

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

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



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

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

#parsedObject



73
74
75
# File 'lib/shutl/resource/rest.rb', line 73

def parsed
  response.parsed_response
end

#resource_idObject



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

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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/shutl/resource/rest.rb', line 69

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

#save(options = {}) ⇒ Object



54
55
56
# File 'lib/shutl/resource/rest.rb', line 54

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

#statusObject



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

def status
  response.code
end

#to_json(options = nil) ⇒ Object



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

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

#update!(attrs) ⇒ Object



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

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

#update_attributes(attrs) ⇒ Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/shutl/resource/rest.rb', line 83

def valid?
  errors.blank?
end