Class: OpenprojectApi::Api
- Inherits:
-
Object
- Object
- OpenprojectApi::Api
- Defined in:
- lib/openproject_api/api.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#apikey ⇒ Object
Returns the value of attribute apikey.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
Instance Method Summary collapse
- #delete(resource, *args) ⇒ Object
- #get(resource, *args) ⇒ Object
-
#initialize(endpoint:, apikey:) ⇒ Api
constructor
A new instance of Api.
- #patch(resource, body, *args) ⇒ Object
- #post(resource, body, *args) ⇒ Object
- #request(resource, query: {}, &block) ⇒ Object
Constructor Details
#initialize(endpoint:, apikey:) ⇒ Api
Returns a new instance of Api.
11 12 13 14 |
# File 'lib/openproject_api/api.rb', line 11 def initialize(endpoint:, apikey:) self.endpoint = endpoint self.apikey = apikey end |
Instance Attribute Details
#apikey ⇒ Object
Returns the value of attribute apikey.
9 10 11 |
# File 'lib/openproject_api/api.rb', line 9 def apikey @apikey end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
8 9 10 |
# File 'lib/openproject_api/api.rb', line 8 def endpoint @endpoint end |
Instance Method Details
#delete(resource, *args) ⇒ Object
61 62 63 64 65 |
# File 'lib/openproject_api/api.rb', line 61 def delete(resource, *args) request(resource, *args) do |uri| Net::HTTP::Delete.new(uri.request_uri) end end |
#get(resource, *args) ⇒ Object
35 36 37 38 39 |
# File 'lib/openproject_api/api.rb', line 35 def get(resource, *args) request(resource, *args) do |uri| Net::HTTP::Get.new(uri.request_uri) end end |
#patch(resource, body, *args) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/openproject_api/api.rb', line 51 def patch(resource, body, *args) request(resource, *args) do |uri| Net::HTTP::Patch.new(uri.request_uri).tap do |request| request['Content-Type'] = 'application/json' request['Accept' ] = 'application/json' request.body = body.to_json end end end |
#post(resource, body, *args) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/openproject_api/api.rb', line 41 def post(resource, body, *args) request(resource, *args) do |uri| Net::HTTP::Post.new(uri.request_uri).tap do |request| request['Content-Type'] = 'application/json' request['Accept' ] = 'application/json' request.body = body.to_json end end end |
#request(resource, query: {}, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/openproject_api/api.rb', line 16 def request(resource, query: {}, &block) uri = URI.join(endpoint, resource) uri.query = URI.encode_www_form(query) response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme=='https') do |http| request = yield(uri) request.basic_auth('apikey', apikey) http.request(request) end if response.is_a?(Net::HTTPSuccess) ObjectifiedHash.new(JSON.parse(response.body)) else response.error! end end |