Class: RestApiClient::RestModel
- Inherits:
-
Object
- Object
- RestApiClient::RestModel
- 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
- .count(params = {}) ⇒ Object
- .find(id) ⇒ Object
- .get(id) ⇒ Object
- .get_model_name ⇒ Object
- .list(params = {}) ⇒ Object
-
.path ⇒ Object
default path to class methods.
- .perform_get(path, args = {}, headers = {}) ⇒ Object
-
.service_key ⇒ Object
default service_key to class methods.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #delete ⇒ Object
- #get_model_name ⇒ Object
-
#path ⇒ Object
default path to instance methods.
- #perform_delete(path, args = {}, headers = {}) ⇒ Object
- #perform_get(path, args = {}, headers = {}) ⇒ Object
- #perform_post(path, args = {}, headers = {}) ⇒ Object
- #perform_put(path, args = {}, headers = {}) ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
-
#service_key ⇒ Object
default service_key to instance methods.
- #update ⇒ Object
- #update! ⇒ Object
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_name ⇒ Object
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 |
.path ⇒ Object
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_key ⇒ Object
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 |
#delete ⇒ Object
68 69 70 |
# File 'lib/rest/api/client.rb', line 68 def delete perform_delete "#{path}/#{id}" end |
#get_model_name ⇒ Object
112 113 114 |
# File 'lib/rest/api/client.rb', line 112 def get_model_name self.class.name.split('::').last.downcase end |
#path ⇒ Object
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 |
#save ⇒ Object
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_key ⇒ Object
default service_key to instance methods
121 122 123 |
# File 'lib/rest/api/client.rb', line 121 def service_key SERVICE_KEY end |
#update ⇒ Object
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 |