Class: RedmineClient::Base
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- RedmineClient::Base
- Extended by:
- Forwardable
- Includes:
- HTTParty
- Defined in:
- lib/redmine_client/base.rb
Direct Known Subclasses
Class Method Summary collapse
- .bad_response(response, _params = {}) ⇒ Object
- .create(attrs = {}) ⇒ Object
- .find(id) ⇒ Object
- .resource_name ⇒ Object
- .resource_path ⇒ Object
- .setup ⇒ Object
Instance Method Summary collapse
Class Method Details
.bad_response(response, _params = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/redmine_client/base.rb', line 46 def bad_response(response, _params = {}) if response.class == HTTParty::Response case response.code when 403 fail Errors::AccessDeniedException when 404 fail Errors::ResourceNotFoundException when 422 fail Errors::UnprocessableEntityException, response when 500 fail Errors::InternalErrorException else fail HTTParty::ResponseError, response end end fail StandardError, 'Unknown error' end |
.create(attrs = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/redmine_client/base.rb', line 70 def create(attrs = {}) resource = post "#{resource_path}.json", body: { resource_name => attrs } if resource.success? data = attrs.merge resource[resource_name] new(data) else bad_response(resource, attrs) end end |
.find(id) ⇒ Object
65 66 67 68 |
# File 'lib/redmine_client/base.rb', line 65 def find(id) resource = get "#{resource_path}/#{id}.json" resource.ok? ? new(resource[resource_name]) : bad_response(resource, id) end |
.resource_name ⇒ Object
41 42 43 44 |
# File 'lib/redmine_client/base.rb', line 41 def resource_name klass = name.split('::').last klass.downcase end |
.resource_path ⇒ Object
35 36 37 38 39 |
# File 'lib/redmine_client/base.rb', line 35 def resource_path klass = name.split('::').last klass[0] = klass[0].chr.downcase klass.end_with?('y') ? "/#{klass.chop}ies" : "/#{klass}s" end |
.setup ⇒ Object
30 31 32 33 |
# File 'lib/redmine_client/base.rb', line 30 def setup base_uri RedmineClient::API.config.url headers HEADERS.merge('X-Redmine-API-Key' => RedmineClient::API.config.token) end |
Instance Method Details
#update(attrs = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/redmine_client/base.rb', line 19 def update(attrs = {}) resource = put "#{resource_path}/#{id}.json", body: { resource_name => attrs } if resource.success? data = Hash[attrs.map { |key, value| [key.to_sym, value] }] @table.merge!(data) else false end end |