Class: Okapi::Client
- Inherits:
-
Object
- Object
- Okapi::Client
- Defined in:
- lib/okapi.rb
Direct Known Subclasses
Defined Under Namespace
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #get(path) ⇒ Object
- #headers ⇒ Object
-
#initialize(url = nil, tenant = nil, token = nil) ⇒ Client
constructor
A new instance of Client.
- #post(path, body) ⇒ Object
- #request(verb, path, body = nil, header_overrides = {}) ⇒ Object
- #settings ⇒ Object
- #tenant ⇒ Object
- #url ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(url = nil, tenant = nil, token = nil) ⇒ Client
Returns a new instance of Client.
11 12 13 |
# File 'lib/okapi.rb', line 11 def initialize(url = nil, tenant = nil, token = nil) @url, @tenant, @token = url, tenant, token end |
Instance Method Details
#delete(path) ⇒ Object
45 46 47 |
# File 'lib/okapi.rb', line 45 def delete(path) request(:delete, path, nil, 'Accept' => 'text/plain') end |
#get(path) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/okapi.rb', line 23 def get(path) request(:get, path) do |response| json = JSON.parse(response.body) if block_given? yield json, response else json end end end |
#headers ⇒ Object
66 67 68 |
# File 'lib/okapi.rb', line 66 def headers {'Content-Type' => 'application/json', 'Accept' => 'application/json'} end |
#post(path, body) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/okapi.rb', line 34 def post(path, body) request(:post, path, JSON.generate(body)) do |response| json = JSON.parse(response.body) if block_given? yield json, response else json end end end |
#request(verb, path, body = nil, header_overrides = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/okapi.rb', line 49 def request(verb, path, body = nil, header_overrides = {}) http = Net::HTTP.new(url.host, url.port) if url.scheme == "https" http.use_ssl = true end http.start do args = [body].compact response = http.send(verb, path, *args, headers.merge(header_overrides)) RequestError.maybe_fail! response if block_given? yield response else response end end end |
#settings ⇒ Object
15 16 17 |
# File 'lib/okapi.rb', line 15 def settings Settings.new(@url, @tenant, @token) end |
#tenant ⇒ Object
70 71 72 |
# File 'lib/okapi.rb', line 70 def tenant Tenant.new(@url, @tenant, @token) end |
#url ⇒ Object
19 20 21 |
# File 'lib/okapi.rb', line 19 def url settings.url end |