Class: ScientificProtocols::Client
- Inherits:
-
Object
- Object
- ScientificProtocols::Client
- Defined in:
- lib/scientificprotocols/client.rb
Constant Summary collapse
- URL =
'https://www.scientificprotocols.org/api/v1/'- REQUESTS =
[:get, :post, :put, :delete]
- HEADERS =
{'Accept' => 'application/json', 'Content-Type' => 'application/json'}
Instance Method Summary collapse
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#request(method, path, query = {}, headers = HEADERS) ⇒ Faraday::Response
Server response.
Methods included from DSL::Protocols
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 24 25 26 27 |
# File 'lib/scientificprotocols/client.rb', line 19 def initialize # Setup HTTP request connection to Zenodo. @connection ||= Faraday.new do |builder| builder.request :multipart builder.request :url_encoded builder.response :logger builder.adapter Faraday.default_adapter end end |
Instance Method Details
#request(method, path, query = {}, headers = HEADERS) ⇒ Faraday::Response
Returns server response.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/scientificprotocols/client.rb', line 37 def request(method, path, query = {}, headers = HEADERS) raise ArgumentError, "Unsupported method #{method.inspect}. Only :get, :post, :put, :delete are allowed" unless REQUESTS.include?(method) payload = nil if query.present? accept = headers.present? ? headers['Accept'] : nil if accept.present? && accept == 'application/json' payload = JSON.generate(query) else payload = query end end response = @connection.run_request(method, "#{URL}#{path}", payload, headers) case response.status.to_i when 200..299 return response when 404 raise ResourceNotFoundError.new(response: response) else raise ClientError.new(response: response) end end |