Module: LogicalModel::RESTActions::InstanceMethods

Defined in:
lib/logical_model/rest_actions.rb

Instance Method Summary collapse

Instance Method Details

#_create(params = {}) ⇒ Object

creates model.

returns:

Examples:

Usage:

@person = Person.new(params[:person])
@person.create( non_attribute_param: "value" )


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/logical_model/rest_actions.rb', line 54

def _create(params = {})
  return false unless valid?

  params = { self.json_root => self.attributes }.merge(params)
  params = self.class.merge_key(params)

  response = nil
  Timeout::timeout(self.class.timeout/1000) do
    response = Typhoeus::Request.post( self.class.resource_uri, :body => params, :timeout => self.class.timeout )
  end
  self.last_response_code = response.code
  if response.code == 201 || response.code == 202
    log_ok(response)
    if self.respond_to?('id=')
      self.id = ActiveSupport::JSON.decode(response.body)["id"]
    else
      true
    end
  elsif response.code == 400
    log_failed(response)
    ws_errors = ActiveSupport::JSON.decode(response.body)["errors"]
    ws_errors.each_key do |k|
      self.errors.add k, ws_errors[k]
    end
    return false
  else
    log_failed(response)
    return nil
  end
rescue Timeout::Error
  self.class.logger.warn "timeout"
  return nil
end

#_destroy(params = {}) ⇒ Object

Destroy object

Usage:

@person.destroy


164
165
166
# File 'lib/logical_model/rest_actions.rb', line 164

def _destroy(params={})
  self.class.delete(self.id,params)
end

#_saveObject

Saves Objects attributes

Returns false if Object#valid? is false. Returns updated object if successfull. Returns nil if update failed

Usage:

@person.save


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/logical_model/rest_actions.rb', line 134

def _save
  self.attributes = attributes

  return false unless valid?

  sending_params = self.attributes
  sending_params.delete(:id)

  params = { self.json_root => sending_params }
  params = self.class.merge_key(params)
  response = nil
  Timeout::timeout(self.class.timeout/1000) do
    response = Typhoeus::Request.put( self.class.resource_uri(id), :params => params, :timeout => self.class.timeout )
  end
  if response.code == 200
    log_ok(response)
    return self
  else
    log_failed(response)
    return nil
  end
rescue Timeout::Error
  self.class.logger.warn "timeout"
  return nil
end

#_update(params) ⇒ Object

Updates Objects attributes, this will only send attributes passed as arguments

Returns false if Object#valid? is false. Returns updated object if successfull. Returns nil if update failed

Usage:

@person.update(params[:person])


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/logical_model/rest_actions.rb', line 97

def _update(params)

  self.attributes = params[self.json_root]

  return false unless valid?

  params = self.class.merge_key(params)

  response = nil
  Timeout::timeout(self.class.timeout/1000) do
    response = Typhoeus::Request.put( self.class.resource_uri(id),
                                      :params => params,
                                      :timeout => self.class.timeout )
  end

  if response.code == 200
    log_ok(response)
    return self
  else
    log_failed(response)
    return nil
  end

rescue Timeout::Error
  self.class.logger.warn("request timed out")
  return nil
end

#create(params = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/logical_model/rest_actions.rb', line 11

def create(params={})
  run_callbacks :save do
    run_callbacks :create do
      _create(params)
    end
  end
end

#destroy(params = {}) ⇒ Object



27
28
29
30
31
# File 'lib/logical_model/rest_actions.rb', line 27

def destroy(params={})
  run_callbacks :destroy do
    _destroy(params)
  end
end

#saveObject



19
20
21
22
23
24
25
# File 'lib/logical_model/rest_actions.rb', line 19

def save
  run_callbacks :save do
    run_callbacks new_record?? :create : :update do
      _save
    end
  end
end

#update(params) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/logical_model/rest_actions.rb', line 34

def update(params)
  run_callbacks :save do
    run_callbacks :update do
      _update(params)
    end
  end

end