Class: Ridley::Connection
- Inherits:
-
Faraday::Connection
- Object
- Faraday::Connection
- Ridley::Connection
- Includes:
- Celluloid
- Defined in:
- lib/ridley/connection.rb
Constant Summary collapse
- VALID_OPTIONS =
[ :retries, :retry_interval, :ssl, :proxy ]
Instance Attribute Summary collapse
- #client_key ⇒ String readonly
- #client_name ⇒ String readonly
- #organization ⇒ String readonly
-
#retries ⇒ Integer
readonly
How many retries to attempt on HTTP requests.
-
#retry_interval ⇒ Float
readonly
Time to wait between retries.
Instance Method Summary collapse
- #api_type ⇒ Symbol
- #foss? ⇒ Boolean
- #hosted? ⇒ Boolean
-
#initialize(server_url, client_name, client_key, options = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#run_request(*args) ⇒ Object
Override Faraday::Connection#run_request to catch exceptions from Middleware that we expect.
- #server_url ⇒ Object
-
#stream(target, destination) ⇒ Object
Stream the response body of a remote URL to a file on the local file system.
Constructor Details
#initialize(server_url, client_name, client_key, options = {}) ⇒ Connection
Returns a new instance of Connection.
43 44 45 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 |
# File 'lib/ridley/connection.rb', line 43 def initialize(server_url, client_name, client_key, = {}) = .reverse_merge(retries: 5, retry_interval: 0.5) @client_name = client_name @client_key = client_key @retries = [:retries] @retry_interval = [:retry_interval] [:builder] = Faraday::Builder.new do |b| b.request :retry, max: @retries, interval: @retry_interval, exceptions: [ Ridley::Errors::HTTP5XXError, Errno::ETIMEDOUT, Faraday::Error::TimeoutError ] b.request :chef_auth, client_name, client_key b.response :parse_json b.response :gzip b.response :chef_response b.adapter :net_http_persistent end uri_hash = Addressable::URI.parse(server_url).to_hash.slice(:scheme, :host, :port) unless uri_hash[:port] uri_hash[:port] = (uri_hash[:scheme] == "https" ? 443 : 80) end if org_match = server_url.match(/.*\/organizations\/(.*)/) @organization = org_match[1] end unless @organization.nil? uri_hash[:path] = "/organizations/#{@organization}" end super(Addressable::URI.new(uri_hash), ) @headers[:user_agent] = "Ridley v#{Ridley::VERSION}" end |
Instance Attribute Details
#client_key ⇒ String (readonly)
21 22 23 |
# File 'lib/ridley/connection.rb', line 21 def client_key @client_key end |
#client_name ⇒ String (readonly)
23 24 25 |
# File 'lib/ridley/connection.rb', line 23 def client_name @client_name end |
#organization ⇒ String (readonly)
19 20 21 |
# File 'lib/ridley/connection.rb', line 19 def organization @organization end |
#retries ⇒ Integer (readonly)
Returns how many retries to attempt on HTTP requests.
26 27 28 |
# File 'lib/ridley/connection.rb', line 26 def retries @retries end |
#retry_interval ⇒ Float (readonly)
Returns time to wait between retries.
29 30 31 |
# File 'lib/ridley/connection.rb', line 29 def retry_interval @retry_interval end |
Instance Method Details
#api_type ⇒ Symbol
87 88 89 |
# File 'lib/ridley/connection.rb', line 87 def api_type organization.nil? ? :foss : :hosted end |
#foss? ⇒ Boolean
97 98 99 |
# File 'lib/ridley/connection.rb', line 97 def foss? api_type == :foss end |
#hosted? ⇒ Boolean
92 93 94 |
# File 'lib/ridley/connection.rb', line 92 def hosted? api_type == :hosted end |
#run_request(*args) ⇒ Object
Override Faraday::Connection#run_request to catch exceptions from Middleware that we expect. Caught exceptions are re-raised with Celluloid#abort so we don’t crash the connection.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ridley/connection.rb', line 103 def run_request(*args) super rescue Errors::HTTPError => ex abort ex rescue Faraday::Error::ConnectionFailed => ex abort Errors::ConnectionFailed.new(ex) rescue Faraday::Error::TimeoutError => ex abort Errors::TimeoutError.new(ex) rescue Faraday::Error::ClientError => ex abort Errors::ClientError.new(ex) end |
#server_url ⇒ Object
115 116 117 |
# File 'lib/ridley/connection.rb', line 115 def server_url self.url_prefix.to_s end |
#stream(target, destination) ⇒ Object
Stream the response body of a remote URL to a file on the local file system
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ridley/connection.rb', line 125 def stream(target, destination) FileUtils.mkdir_p(File.dirname(destination)) target = Addressable::URI.parse(target) headers = Middleware::ChefAuth.authentication_headers( client_name, client_key, http_method: "GET", host: target.host, path: target.path ) unless ssl[:verify] headers.merge!(ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE) end local = Tempfile.new('ridley-stream') local.binmode retryable(tries: retries, on: OpenURI::HTTPError, sleep: retry_interval) do open(target, 'rb', headers) do |remote| body = remote.read case remote.content_encoding when ['gzip'] body = Zlib::GzipReader.new(StringIO.new(body), encoding: 'ASCII-8BIT').read when ['deflate'] body = Zlib::Inflate.inflate(body) end local.write(body) end end local.flush FileUtils.mv(local.path, destination) rescue OpenURI::HTTPError => ex abort(ex) ensure local.close(true) unless local.nil? end |