Class: Redminerb::Client
- Inherits:
-
Object
- Object
- Redminerb::Client
- Defined in:
- lib/redminerb/client.rb
Overview
HTTP client to communicate w/ the Redmine server.
Defined Under Namespace
Classes: UnprocessableEntity
Class Method Summary collapse
-
.raise_error!(res) ⇒ Object
It raises an exception giving the validation messages for 422 responses.
Instance Method Summary collapse
-
#get_collection(name, params = { 'limit': 100 }) ⇒ Object
Uses pagination (limit&offset) params to retreive all the resources of the collection “name” using “params” in each request.
-
#get_json(path, params = {}) ⇒ Object
Makes a GET request of the given ‘path’ param and returns the body of the response parsed as JSON.
-
#initialize(cfg) ⇒ Client
constructor
A new instance of Client.
-
#post_json(path, params) ⇒ Object
Makes a POST request to ‘path’ with ‘params’ in JSON format.
Constructor Details
#initialize(cfg) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 |
# File 'lib/redminerb/client.rb', line 10 def initialize(cfg) @connection = Faraday.new(url: cfg.url) do |f| f.adapter Faraday.default_adapter end @connection.basic_auth(cfg.api_key, cfg.api_key) end |
Class Method Details
.raise_error!(res) ⇒ Object
It raises an exception giving the validation messages for 422 responses.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/redminerb/client.rb', line 59 def self.raise_error!(res) if res.status == 422 begin errors = JSON.parse(res.body)['errors'] rescue JSON::ParserError errors = [res.body] end fail UnprocessableEntity, errors.join("\n") else fail StandardError, "ERROR (status code #{res.status})" end end |
Instance Method Details
#get_collection(name, params = { 'limit': 100 }) ⇒ Object
Uses pagination (limit&offset) params to retreive all the resources of the collection “name” using “params” in each request. Returns an array with all the resources.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/redminerb/client.rb', line 20 def get_collection(name, params = { 'limit': 100 }) offset = 0 limit = params['limit'].to_i [].tap do |resources| loop do params['offset'] = offset response = get_json("/#{name}.json", params) resources << response[name.to_s] offset += limit break unless offset < response['total_count'] end end.flatten end |
#get_json(path, params = {}) ⇒ Object
Makes a GET request of the given ‘path’ param and returns the body of the response parsed as JSON.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/redminerb/client.rb', line 36 def get_json(path, params = {}) Redminerb.init_required! res = _get(path, params) if res.status == 404 fail Redminerb::NotFoundError, path else JSON.parse(res.body) end rescue JSON::ParserError => e raise e, "HTTP status code #{res.status}" end |
#post_json(path, params) ⇒ Object
Makes a POST request to ‘path’ with ‘params’ in JSON format.
49 50 51 52 53 54 55 56 |
# File 'lib/redminerb/client.rb', line 49 def post_json(path, params) Redminerb.init_required! @connection.post do |req| req.url path req.headers['Content-Type'] = 'application/json' req.body = params.to_json end end |