Class: Antbird::Client
- Inherits:
-
Object
- Object
- Antbird::Client
- Defined in:
- lib/antbird/client.rb,
lib/antbird/client/errors.rb
Defined Under Namespace
Classes: ApiError, Error, RequestError, ServerError
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#api_specs ⇒ Object
readonly
Returns the value of attribute api_specs.
-
#last_request ⇒ Object
readonly
Returns the value of attribute last_request.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#read_timeout ⇒ Object
readonly
Returns the value of attribute read_timeout.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#write_timeout ⇒ Object
readonly
Returns the value of attribute write_timeout.
Instance Method Summary collapse
- #connection ⇒ Object
- #extract_body(params) ⇒ Object
- #extract_method(params) ⇒ Object
- #extract_scopes(params, path_params = [:index, :type, :id]) ⇒ Object
-
#initialize(scope: {}, url: "http://localhost:9200", version: nil, read_timeout: 5, open_timeout: 2, write_timeout: nil, adapter: ::Faraday.default_adapter, &block) ⇒ Client
constructor
A new instance of Client.
- #request(api_name, api_spec, params, path_params = [:index, :type, :id]) ⇒ Object
- #scoped(new_scope = {}) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(scope: {}, url: "http://localhost:9200", version: nil, read_timeout: 5, open_timeout: 2, write_timeout: nil, adapter: ::Faraday.default_adapter, &block) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/antbird/client.rb', line 6 def initialize( scope: {}, url: "http://localhost:9200", version: nil, read_timeout: 5, open_timeout: 2, write_timeout: nil, adapter: ::Faraday.default_adapter, &block) @read_timeout = read_timeout @open_timeout = open_timeout @write_timeout = write_timeout @adapter = adapter @block = block @url = url @scope = scope.transform_keys(&:to_sym) @version = version @api_specs = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
347 348 349 350 351 352 353 354 |
# File 'lib/antbird/client.rb', line 347 def method_missing(name, *args, &block) return super if api_specs_loaded? ensure_api_spec_loaded return super unless respond_to?(name) __send__(name, *args, &block) end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
30 31 32 |
# File 'lib/antbird/client.rb', line 30 def adapter @adapter end |
#api_specs ⇒ Object (readonly)
Returns the value of attribute api_specs.
31 32 33 |
# File 'lib/antbird/client.rb', line 31 def api_specs @api_specs end |
#last_request ⇒ Object (readonly)
Returns the value of attribute last_request.
31 32 33 |
# File 'lib/antbird/client.rb', line 31 def last_request @last_request end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
30 31 32 |
# File 'lib/antbird/client.rb', line 30 def open_timeout @open_timeout end |
#read_timeout ⇒ Object (readonly)
Returns the value of attribute read_timeout.
30 31 32 |
# File 'lib/antbird/client.rb', line 30 def read_timeout @read_timeout end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
29 30 31 |
# File 'lib/antbird/client.rb', line 29 def scope @scope end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
29 30 31 |
# File 'lib/antbird/client.rb', line 29 def url @url end |
#write_timeout ⇒ Object (readonly)
Returns the value of attribute write_timeout.
30 31 32 |
# File 'lib/antbird/client.rb', line 30 def write_timeout @write_timeout end |
Instance Method Details
#connection ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/antbird/client.rb', line 184 def connection @connection ||= Faraday.new(url) do |conn| @block&.call(conn) conn.request :json conn.response :json, content_type: /\bjson$/ conn.[:timeout] = read_timeout conn.[:open_timeout] = open_timeout conn.[:write_timeout] = write_timeout if write_timeout conn.adapter adapter end end |
#extract_body(params) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/antbird/client.rb', line 161 def extract_body(params) body = params.delete :body return if body.nil? body = case body when String body when Array if body.all? { |b| b.is_a?(Hash) } body.map { |b| JSON.dump(b) }.join("\n") + "\n" else body << nil unless body.last.nil? body.join "\n" end else JSON.dump(body) end # Prevent excon from changing the encoding (see https://github.com/github/elastomer-client/issues/138) body.freeze end |
#extract_method(params) ⇒ Object
147 148 149 |
# File 'lib/antbird/client.rb', line 147 def extract_method(params) params.delete(:method)&.to_sym end |
#extract_scopes(params, path_params = [:index, :type, :id]) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/antbird/client.rb', line 151 def extract_scopes(params, path_params = [:index, :type, :id]) scopes = {} path_params.each do |s| scope = params.delete(s) next unless scope scopes[s] = scope end scopes end |
#request(api_name, api_spec, params, path_params = [:index, :type, :id]) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/antbird/client.rb', line 46 def request(api_name, api_spec, params, path_params = [:index, :type, :id]) validate_params(api_spec, params, path_params) body = extract_body(params) scopes = extract_scopes(params, path_params) forced_method = extract_method(params) # Greedy match api_path = nil api_path_template = nil path_methods = nil sort_url_paths(api_spec['url']['paths']).each do |path| if path.is_a?(Hash) path_methods = path['methods'] path = path['path'] end = path.gsub(/{([a-z_\-}]+)}/) do |match| scopes[$1.to_sym] || @scope[$1.to_sym] || match end unless .include?('{') api_path_template = path api_path = break end end unless api_path raise "API path not found: paths: #{api_spec['url']['paths']}, scope: #{@scope}" end methods = (path_methods || api_spec['methods']).map { |m| m.downcase.to_sym } method = if forced_method forced_method elsif methods.include?(:put) :put elsif methods.include?(:post) :post else methods.first end = (params) params.reject! { |_, v| v.nil? } @last_request = { method: method, forced_method: forced_method, api_path: api_path, api_path_template: api_path_template, params: params } response = case method when :head connection.head(api_path) do |req| req.params = params unless params.empty? apply_timeouts(req, ) end when :get connection.get(api_path) do |req| req.params = params unless params.empty? req.body = body if body apply_timeouts(req, ) end when :put connection.put(api_path, body) do |req| req.params = params unless params.empty? apply_timeouts(req, ) end when :post connection.post(api_path, body) do |req| req.params = params unless params.empty? apply_timeouts(req, ) end when :delete connection.delete(api_path) do |req| req.params = params unless params.empty? req.body = body if body apply_timeouts(req, ) end else raise ArgumentError, "Unknown HTTP request method: #{method.inspect}" end if method == :head case response.status when 200 return true when 404 return false end end handle_errors!(response) response.body end |
#scoped(new_scope = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/antbird/client.rb', line 33 def scoped(new_scope = {}) Client.new( scope: new_scope, url: url, version: version, read_timeout: read_timeout, open_timeout: open_timeout, write_timeout: write_timeout, adapter: adapter, &@block ) end |
#version ⇒ Object
199 200 201 202 203 204 |
# File 'lib/antbird/client.rb', line 199 def version return @version if @version ensure_version @version end |