Class: WCC::API::RestClient::Resource
- Inherits:
-
Object
- Object
- WCC::API::RestClient::Resource
- Defined in:
- lib/wcc/api/rest_client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #create(body) ⇒ Object
- #destroy(id) ⇒ Object
- #find(id, query = {}) ⇒ Object
-
#initialize(client, endpoint, model, options) ⇒ Resource
constructor
A new instance of Resource.
- #list(**filters) ⇒ Object
- #update(id, body) ⇒ Object
Constructor Details
#initialize(client, endpoint, model, options) ⇒ Resource
Returns a new instance of Resource.
155 156 157 158 159 160 |
# File 'lib/wcc/api/rest_client.rb', line 155 def initialize(client, endpoint, model, ) @client = client @endpoint = endpoint @model = model @options = end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
153 154 155 |
# File 'lib/wcc/api/rest_client.rb', line 153 def client @client end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
153 154 155 |
# File 'lib/wcc/api/rest_client.rb', line 153 def endpoint @endpoint end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
153 154 155 |
# File 'lib/wcc/api/rest_client.rb', line 153 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
153 154 155 |
# File 'lib/wcc/api/rest_client.rb', line 153 def @options end |
Instance Method Details
#create(body) ⇒ Object
179 180 181 182 183 |
# File 'lib/wcc/api/rest_client.rb', line 179 def create(body) resp = client.post(endpoint, body) resp.assert_ok! maybe_model_from_response(resp) end |
#destroy(id) ⇒ Object
191 192 193 194 195 |
# File 'lib/wcc/api/rest_client.rb', line 191 def destroy(id) resp = client.delete("#{endpoint}/#{id}") resp.assert_ok! maybe_model_from_response(resp) end |
#find(id, query = {}) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/wcc/api/rest_client.rb', line 162 def find(id, query = {}) query = ([:query] || {}).merge(query) resp = client.get("#{endpoint}/#{id}", query) resp.assert_ok! body = [:key] ? resp.body[[:key]] : resp.body model.new(body, resp.headers.freeze) end |
#list(**filters) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/wcc/api/rest_client.rb', line 170 def list(**filters) query = extract_params(filters) query = ([:query] || {}).merge(query) query = query.merge!(apply_filters(filters, [:filters])) resp = client.get(endpoint, query) resp.assert_ok! resp.items.map { |s| model.new(s) } end |
#update(id, body) ⇒ Object
185 186 187 188 189 |
# File 'lib/wcc/api/rest_client.rb', line 185 def update(id, body) resp = client.put("#{endpoint}/#{id}", body) resp.assert_ok! maybe_model_from_response(resp) end |