Class: RestApiClient::RestModel

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/rest/api/client.rb

Constant Summary collapse

PATH =
''
SERVICE_KEY =
''

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.count(params = {}) ⇒ Object



33
34
35
# File 'lib/rest/api/client.rb', line 33

def self.count(params = {})
  perform_get "#{path}/count", {:params => params}
end

.find(id) ⇒ Object



37
38
39
# File 'lib/rest/api/client.rb', line 37

def self.find(id)
  perform_get "#{path}/#{id}", {:type => self}
end

.get(id) ⇒ Object



41
42
43
# File 'lib/rest/api/client.rb', line 41

def self.get(id)
  self.find id
end

.get_model_nameObject



122
123
124
# File 'lib/rest/api/client.rb', line 122

def self.get_model_name
  self.name.split('::').last.downcase
end

.list(params = {}) ⇒ Object



29
30
31
# File 'lib/rest/api/client.rb', line 29

def self.list(params = {})
  perform_get path, {:type => self, :params => params}
end

.pathObject

default path to class methods



142
143
144
# File 'lib/rest/api/client.rb', line 142

def self.path
  PATH
end

.perform_get(path, args = {}, headers = {}) ⇒ Object



114
115
116
# File 'lib/rest/api/client.rb', line 114

def self.perform_get(path, args = {}, headers = {})
  RequestsHandler.perform_get(service_key, path, args, headers)
end

.service_keyObject

default service_key to class methods



132
133
134
# File 'lib/rest/api/client.rb', line 132

def self.service_key
  SERVICE_KEY
end

Instance Method Details

#==(other) ⇒ Object



146
147
148
# File 'lib/rest/api/client.rb', line 146

def ==(other)
  id == other.id
end

#deleteObject



74
75
76
# File 'lib/rest/api/client.rb', line 74

def delete
  perform_delete "#{path}/#{id}"
end

#get_model_nameObject



118
119
120
# File 'lib/rest/api/client.rb', line 118

def get_model_name
  self.class.name.split('::').last.downcase
end

#pathObject

default path to instance methods



137
138
139
# File 'lib/rest/api/client.rb', line 137

def path
  PATH
end

#perform_delete(path, args = {}, headers = {}) ⇒ Object



110
111
112
# File 'lib/rest/api/client.rb', line 110

def perform_delete(path, args = {}, headers = {})
  RequestsHandler.perform_delete(service_key, path, args, headers)
end

#perform_get(path, args = {}, headers = {}) ⇒ Object



98
99
100
# File 'lib/rest/api/client.rb', line 98

def perform_get(path, args = {}, headers = {})
  RequestsHandler.perform_get(service_key, path, args, headers)
end

#perform_post(path, args = {}, headers = {}) ⇒ Object



102
103
104
# File 'lib/rest/api/client.rb', line 102

def perform_post(path, args = {}, headers = {})
  RequestsHandler.perform_post(service_key, path, args, headers)
end

#perform_put(path, args = {}, headers = {}) ⇒ Object



106
107
108
# File 'lib/rest/api/client.rb', line 106

def perform_put(path, args = {}, headers = {})
  RequestsHandler.perform_put(service_key, path, args, headers)
end

#refreshObject



45
46
47
48
49
# File 'lib/rest/api/client.rb', line 45

def refresh
  response = perform_get "#{path}/#{id}", {:type => self.class}
  update_attributes(response)
  self
end

#saveObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rest/api/client.rb', line 62

def save
  return false unless valid?

  begin
    update_attributes(perform_post path, get_params)
    self
  rescue RestApiClient::ModelErrorsException => e
    errors.add(:rest_api_error, e.errors)
    return false
  end
end

#save!Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rest/api/client.rb', line 51

def save!
  return false unless valid?
  begin
    update_attributes(perform_post path, get_params)
    self
  rescue RestApiClient::ModelErrorsException => e
    errors.add(:rest_api_error, e.errors)
    raise e
  end
end

#service_keyObject

default service_key to instance methods



127
128
129
# File 'lib/rest/api/client.rb', line 127

def service_key
  SERVICE_KEY
end

#updateObject



78
79
80
81
82
83
84
85
86
# File 'lib/rest/api/client.rb', line 78

def update
  begin
    update_attributes(perform_put "#{path}/#{id}", get_params)
    self
  rescue RestApiClient::ModelErrorsException => e
    errors.add(:rest_api_error, e.errors)
    return false
  end
end

#update!Object



88
89
90
91
92
93
94
95
96
# File 'lib/rest/api/client.rb', line 88

def update!
  begin
    update_attributes(perform_put "#{path}/#{id}", get_params)
    self
  rescue RestApiClient::ModelErrorsException => e
    errors.add(:rest_api_error, e.errors)
    raise e
  end
end