Class: ApiaClient::API
- Inherits:
-
Object
- Object
- ApiaClient::API
- Defined in:
- lib/apia_client/api.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
Instance Method Summary collapse
- #create_request(method, path) ⇒ Object
-
#initialize(host, **options) ⇒ API
constructor
A new instance of API.
- #load_schema ⇒ Object
- #namespace ⇒ Object
- #perform(*args) {|request| ... } ⇒ Object
- #port ⇒ Object
- #request(request) ⇒ Object
- #schema? ⇒ Boolean
- #ssl? ⇒ Boolean
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, **) @host = host @headers = [:headers] || {} @options = end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
17 18 19 |
# File 'lib/apia_client/api.rb', line 17 def headers @headers end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
16 17 18 |
# File 'lib/apia_client/api.rb', line 16 def host @host end |
#schema ⇒ Object (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, **) api = new(*args, **) 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_schema ⇒ Object
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 |
#namespace ⇒ Object
34 35 36 |
# File 'lib/apia_client/api.rb', line 34 def namespace (@options[:namespace] || '').sub(/\A\/*/, '').sub(/\/*\z/, '') end |
#perform(*args) {|request| ... } ⇒ Object
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 |
#port ⇒ Object
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
38 39 40 |
# File 'lib/apia_client/api.rb', line 38 def schema? !!@schema end |
#ssl? ⇒ Boolean
26 27 28 |
# File 'lib/apia_client/api.rb', line 26 def ssl? @options[:ssl].nil? || @options[:ssl] == true end |