Class: WCC::API::RestClient
- Inherits:
-
Object
- Object
- WCC::API::RestClient
- Defined in:
- lib/wcc/api/rest_client.rb,
lib/wcc/api/rest_client/builder.rb,
lib/wcc/api/rest_client/response.rb,
lib/wcc/api/rest_client/api_error.rb,
lib/wcc/api/rest_client/http_adapter.rb
Defined Under Namespace
Classes: AbstractResponse, ApiError, Builder, DefaultResponse, HttpAdapter, NotFoundError, PaginatingEnumerable, Resource, TyphoeusAdapter
Constant Summary collapse
- ADAPTERS =
{ faraday: ['faraday', '~> 0.9'], typhoeus: ['typhoeus', '~> 1.0'] }.freeze
Class Attribute Summary collapse
-
.params ⇒ Object
readonly
Returns the value of attribute params.
-
.resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Attribute Summary collapse
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
Class Method Summary collapse
Instance Method Summary collapse
- #delete(path) ⇒ Object
-
#get(path, query = {}) ⇒ Object
performs an HTTP GET request to the specified path within the configured space and environment.
-
#initialize(api_url:, headers: nil, **options) ⇒ RestClient
constructor
A new instance of RestClient.
- #post(path, body = {}) ⇒ Object
- #put(path, body = {}) ⇒ Object
Constructor Details
#initialize(api_url:, headers: nil, **options) ⇒ RestClient
Returns a new instance of RestClient.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wcc/api/rest_client.rb', line 22 def initialize(api_url:, headers: nil, **) # normalizes a URL to have a slash on the end @api_url = api_url.gsub(/\/+$/, '') + '/' @adapter = RestClient.load_adapter([:adapter]) @options = @query_defaults = {} @headers = { 'Accept' => 'application/json' }.merge(headers || {}).freeze @response_class = [:response_class] || DefaultResponse end |
Class Attribute Details
.params ⇒ Object (readonly)
Returns the value of attribute params.
13 14 15 |
# File 'lib/wcc/api/rest_client.rb', line 13 def params @params end |
.resources ⇒ Object (readonly)
Returns the value of attribute resources.
13 14 15 |
# File 'lib/wcc/api/rest_client.rb', line 13 def resources @resources end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
10 11 12 |
# File 'lib/wcc/api/rest_client.rb', line 10 def api_url @api_url end |
Class Method Details
.load_adapter(adapter) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/wcc/api/rest_client.rb', line 80 def self.load_adapter(adapter) case adapter when nil ADAPTERS.each do |a, spec| begin gem(*spec) return load_adapter(a) rescue Gem::LoadError next end end raise ArgumentError, 'Unable to load adapter! Please install one of '\ "#{ADAPTERS.values.map(&:join).join(',')}" when :faraday require 'faraday' ::Faraday.new do |faraday| faraday.response :logger, (Rails.logger if defined?(Rails)), { headers: false, bodies: false } faraday.adapter :net_http end when :typhoeus require_relative 'rest_client/typhoeus_adapter' TyphoeusAdapter.new else unless adapter.respond_to?(:get) raise ArgumentError, "Adapter #{adapter} is not invokeable! Please "\ "pass use one of #{ADAPTERS.keys} or create a Faraday-compatible adapter" end adapter end end |
Instance Method Details
#delete(path) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/wcc/api/rest_client.rb', line 67 def delete(path) url = URI.join(@api_url, path) @response_class.new(self, { url: url }, delete_http(url)) end |
#get(path, query = {}) ⇒ Object
performs an HTTP GET request to the specified path within the configured space and environment. Query parameters are merged with the defaults and appended to the request.
39 40 41 42 43 44 45 |
# File 'lib/wcc/api/rest_client.rb', line 39 def get(path, query = {}) url = URI.join(@api_url, path) @response_class.new(self, { url: url, query: query }, get_http(url, query)) end |
#post(path, body = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/wcc/api/rest_client.rb', line 47 def post(path, body = {}) url = URI.join(@api_url, path) @response_class.new(self, { url: url }, post_http(url, body.to_json, headers: { 'Content-Type': 'application/json' })) end |
#put(path, body = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/wcc/api/rest_client.rb', line 57 def put(path, body = {}) url = URI.join(@api_url, path) @response_class.new(self, { url: url }, put_http(url, body.to_json, headers: { 'Content-Type': 'application/json' })) end |