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



116
117
118
# File 'lib/rest/api/client.rb', line 116

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



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

def self.path
  PATH
end

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



108
109
110
# File 'lib/rest/api/client.rb', line 108

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

.service_keyObject

default service_key to class methods



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

def self.service_key
  SERVICE_KEY
end

Instance Method Details

#==(other) ⇒ Object



140
141
142
# File 'lib/rest/api/client.rb', line 140

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

#deleteObject



68
69
70
# File 'lib/rest/api/client.rb', line 68

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

#get_model_nameObject



112
113
114
# File 'lib/rest/api/client.rb', line 112

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

#pathObject

default path to instance methods



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

def path
  PATH
end

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



104
105
106
# File 'lib/rest/api/client.rb', line 104

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

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



92
93
94
# File 'lib/rest/api/client.rb', line 92

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

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



96
97
98
# File 'lib/rest/api/client.rb', line 96

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

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



100
101
102
# File 'lib/rest/api/client.rb', line 100

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

#saveObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rest/api/client.rb', line 56

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/rest/api/client.rb', line 45

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



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

def service_key
  SERVICE_KEY
end

#updateObject



72
73
74
75
76
77
78
79
80
# File 'lib/rest/api/client.rb', line 72

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



82
83
84
85
86
87
88
89
90
# File 'lib/rest/api/client.rb', line 82

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