Class: RestApiClient::RestModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rest/api/client.rb

Constant Summary collapse

PATH =
''
SERVICE_KEY =
''

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/rest/api/client.rb', line 19

def errors
  @errors
end

Class Method Details

.count(params = {}) ⇒ Object



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

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

.find(id) ⇒ Object



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

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

.get(id) ⇒ Object



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

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

.get_model_nameObject



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

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

.list(params = {}) ⇒ Object



25
26
27
# File 'lib/rest/api/client.rb', line 25

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

.pathObject

default path to class methods



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

def self.path
  PATH
end

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



83
84
85
# File 'lib/rest/api/client.rb', line 83

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

.service_keyObject

default service_key to class methods



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

def self.service_key
  SERVICE_KEY
end

Instance Method Details

#==(other) ⇒ Object



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

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

#deleteObject



52
53
54
# File 'lib/rest/api/client.rb', line 52

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

#get_model_nameObject



87
88
89
# File 'lib/rest/api/client.rb', line 87

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

#pathObject

default path to instance methods



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

def path
  PATH
end

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



79
80
81
# File 'lib/rest/api/client.rb', line 79

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

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



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

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

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



71
72
73
# File 'lib/rest/api/client.rb', line 71

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

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



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

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

#save!Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/rest/api/client.rb', line 41

def save!
  begin
    klazz = self.class
    response = perform_post path, {:type => klazz, :params => {get_model_name => self.attributes}}
    self.attributes = response && response.is_a?(klazz) ? response.attributes : {}
  rescue RestApiClient::ModelErrorsException => e
    @errors = e.errors
    return false
  end
end

#service_keyObject

default service_key to instance methods



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

def service_key
  SERVICE_KEY
end

#update!Object



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

def update!
  begin
    klazz = self.class
    response = perform_put "#{path}/#{id}", {:type => klazz, :params => {get_model_name => self.attributes}}
    self.attributes = response && response.is_a?(klazz) ? response.attributes : {}
  rescue RestApiClient::ModelErrorsException => e
    @errors = e.errors
    return false
  end
end