Class: DruidClient::Api::RestClient
- Inherits:
-
Object
- Object
- DruidClient::Api::RestClient
- Defined in:
- lib/druid_client/api/rest_client.rb
Instance Attribute Summary collapse
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #base_headers ⇒ Object
- #basic_auth? ⇒ Boolean
- #basic_password ⇒ Object
- #basic_username ⇒ Object
- #ca_file ⇒ Object
- #ca_path ⇒ Object
- #client ⇒ Object
- #client_options ⇒ Object
- #delete(path, params: {}, headers: {}) ⇒ Object
- #get(path, params: {}, headers: {}) ⇒ Object
-
#initialize(options = {}) ⇒ RestClient
constructor
A new instance of RestClient.
- #patch(path, body: nil, headers: {}) ⇒ Object
- #post(path, body: nil, headers: {}) ⇒ Object
- #put(path, body: nil, headers: {}) ⇒ Object
- #request(method, path, body: nil, params: {}, headers: {}) ⇒ Object
- #timeout ⇒ Object
- #user_agent ⇒ Object
- #verify_ssl ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RestClient
12 13 14 15 16 |
# File 'lib/druid_client/api/rest_client.rb', line 12 def initialize( = {}) = @url = [:url] @log = [:log] end |
Instance Attribute Details
#log ⇒ Object (readonly)
Returns the value of attribute log.
10 11 12 |
# File 'lib/druid_client/api/rest_client.rb', line 10 def log @log end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/druid_client/api/rest_client.rb', line 10 def end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
10 11 12 |
# File 'lib/druid_client/api/rest_client.rb', line 10 def url @url end |
Instance Method Details
#base_headers ⇒ Object
82 83 84 |
# File 'lib/druid_client/api/rest_client.rb', line 82 def base_headers [:headers] || {} end |
#basic_auth? ⇒ Boolean
86 87 88 |
# File 'lib/druid_client/api/rest_client.rb', line 86 def basic_auth? basic_username && basic_password end |
#basic_password ⇒ Object
94 95 96 |
# File 'lib/druid_client/api/rest_client.rb', line 94 def basic_password [:password] end |
#basic_username ⇒ Object
90 91 92 |
# File 'lib/druid_client/api/rest_client.rb', line 90 def basic_username [:username] end |
#ca_file ⇒ Object
106 107 108 |
# File 'lib/druid_client/api/rest_client.rb', line 106 def ca_file [:ca_file] end |
#ca_path ⇒ Object
110 111 112 |
# File 'lib/druid_client/api/rest_client.rb', line 110 def ca_path [:ca_path] end |
#client ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/druid_client/api/rest_client.rb', line 69 def client @client ||= Faraday.new() do |conn| conn.request :authorization, :basic, basic_username, basic_password if basic_auth? conn.request :json conn.response :json if log conn.response :logger, log, headers: [:log_headers], bodies: [:log_bodies], log_level: :debug end conn.adapter Faraday.default_adapter end end |
#client_options ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/druid_client/api/rest_client.rb', line 58 def { url: url, headers: { 'User-Agent' => user_agent }.compact, request: { timeout: timeout }.compact, ssl: { ca_file: ca_file, ca_path: ca_path, verify: verify_ssl }.compact } end |
#delete(path, params: {}, headers: {}) ⇒ Object
34 35 36 |
# File 'lib/druid_client/api/rest_client.rb', line 34 def delete(path, params: {}, headers: {}) request(:delete, path, params: params, headers: headers) end |
#get(path, params: {}, headers: {}) ⇒ Object
18 19 20 |
# File 'lib/druid_client/api/rest_client.rb', line 18 def get(path, params: {}, headers: {}) request(:get, path, params: params, headers: headers) end |
#patch(path, body: nil, headers: {}) ⇒ Object
30 31 32 |
# File 'lib/druid_client/api/rest_client.rb', line 30 def patch(path, body: nil, headers: {}) request(:patch, path, body: body, headers: headers) end |
#post(path, body: nil, headers: {}) ⇒ Object
22 23 24 |
# File 'lib/druid_client/api/rest_client.rb', line 22 def post(path, body: nil, headers: {}) request(:post, path, body: body, headers: headers) end |
#put(path, body: nil, headers: {}) ⇒ Object
26 27 28 |
# File 'lib/druid_client/api/rest_client.rb', line 26 def put(path, body: nil, headers: {}) request(:put, path, body: body, headers: headers) end |
#request(method, path, body: nil, params: {}, headers: {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/druid_client/api/rest_client.rb', line 38 def request(method, path, body: nil, params: {}, headers: {}) File.join(url, path) start_time = Time.now response = client.send(method) do |req| req.url path req.headers.merge!(headers) req.body = body.to_json if body end duration = Time.now - start_time Response.new( status_code: response.status, headers: response.headers, body: response.body, duration: duration ) rescue Faraday::Error => e duration = Time.now - start_time Response.new(status_code: 0, body: e., duration: duration) end |
#timeout ⇒ Object
102 103 104 |
# File 'lib/druid_client/api/rest_client.rb', line 102 def timeout [:timeout] end |
#user_agent ⇒ Object
98 99 100 |
# File 'lib/druid_client/api/rest_client.rb', line 98 def user_agent [:user_agent] end |
#verify_ssl ⇒ Object
114 115 116 |
# File 'lib/druid_client/api/rest_client.rb', line 114 def verify_ssl [:verify_ssl] end |