Class: ApiaClient::API

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, **options) ⇒ API

Returns a new instance of API.



20
21
22
23
24
# File 'lib/apia_client/api.rb', line 20

def initialize(host, **options)
  @host = host
  @headers = options[:headers] || {}
  @options = options
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



17
18
19
# File 'lib/apia_client/api.rb', line 17

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host.



16
17
18
# File 'lib/apia_client/api.rb', line 16

def host
  @host
end

#schemaObject (readonly)

Returns the value of attribute schema.



18
19
20
# File 'lib/apia_client/api.rb', line 18

def schema
  @schema
end

Class Method Details

.load(*args, **options) ⇒ Object



127
128
129
130
131
# File 'lib/apia_client/api.rb', line 127

def load(*args, **options)
  api = new(*args, **options)
  api.load_schema
  api
end

Instance Method Details

#create_request(method, path) ⇒ Object



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

def create_request(method, path)
  unless schema?
    raise SchemaNotLoadedError, 'No schema has been loaded for this API instance'
  end

  route = schema.api.route_set.routes.find do |r|
    r.request_method == method.to_s.upcase &&
      r.path == path
  end

  return if route.nil?

  RequestProxy.new(self, route)
end

#load_schemaObject



42
43
44
45
46
# File 'lib/apia_client/api.rb', line 42

def load_schema
  response = request(Get.new('schema'))
  @schema = ApiaSchemaParser::Schema.new(response.hash)
  true
end

#namespaceObject



34
35
36
# File 'lib/apia_client/api.rb', line 34

def namespace
  (@options[:namespace] || '').sub(/\A\/*/, '').sub(/\/*\z/, '')
end

#perform(*args) {|request| ... } ⇒ Object

Yields:



55
56
57
58
59
60
61
# File 'lib/apia_client/api.rb', line 55

def perform(*args)
  request = create_request(*args)
  return if request.nil?

  yield request if block_given?
  request.perform
end

#portObject



30
31
32
# File 'lib/apia_client/api.rb', line 30

def port
  @options[:port] || (ssl? ? 443 : 80)
end

#request(request) ⇒ Object



48
49
50
51
52
53
# File 'lib/apia_client/api.rb', line 48

def request(request)
  status, headers, body = make_http_request(request) do |req|
    request.add_arguments_to_request(req)
  end
  Response.new(self, request, body, headers, status)
end

#schema?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/apia_client/api.rb', line 38

def schema?
  !!@schema
end

#ssl?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/apia_client/api.rb', line 26

def ssl?
  @options[:ssl].nil? || @options[:ssl] == true
end