Class: Okapi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/okapi.rb

Direct Known Subclasses

Tenant

Defined Under Namespace

Classes: Tenant, User

Instance Method Summary collapse

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



58
59
60
# File 'lib/okapi.rb', line 58

def delete(path)
  request(:delete, path, nil, 'Accept' => 'text/plain')
end

#get(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/okapi.rb', line 36

def get(path)
  request(:get, path) do |response|
    json = JSON.parse(response.body)
    if block_given?
      yield json, response
    else
      json
    end
  end
end

#has_interface?(interface_name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/okapi.rb', line 26

def has_interface?(interface_name)
  get("/_/proxy/tenants/#{settings.tenant}/interfaces/#{interface_name}") do |json|
    json.length > 0
  end
end

#headersObject



79
80
81
# File 'lib/okapi.rb', line 79

def headers
  {'Content-Type' => 'application/json', 'Accept' => 'application/json'}
end

#modulesObject



19
20
21
22
23
24
# File 'lib/okapi.rb', line 19

def modules
  endpoint = "#{settings.url}/_/proxy/modules"
  open(endpoint) do |response|
    JSON.parse(response.read)
  end
end

#post(path, body) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/okapi.rb', line 47

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/okapi.rb', line 62

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

#settingsObject



15
16
17
# File 'lib/okapi.rb', line 15

def settings
  Settings.new(@url, @tenant, @token)
end

#tenantObject



83
84
85
# File 'lib/okapi.rb', line 83

def tenant
  Tenant.new(@url, @tenant, @token)
end

#urlObject



32
33
34
# File 'lib/okapi.rb', line 32

def url
  settings.url
end

#userObject



87
88
89
# File 'lib/okapi.rb', line 87

def user
  User.new(@url, @tenant, @token)
end