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) ⇒ Boolean
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.
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 85 |
# File 'lib/ridley/connection.rb', line 45 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 = .delete(:retries) @retry_interval = .delete(:retry_interval) [:builder] = Faraday::RackBuilder.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 :chef_response b.adapter :httpclient end uri_hash = Ridley::Helpers.(Addressable::URI.parse(server_url).to_hash, :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)
23 24 25 |
# File 'lib/ridley/connection.rb', line 23 def client_key @client_key end |
#client_name ⇒ String (readonly)
25 26 27 |
# File 'lib/ridley/connection.rb', line 25 def client_name @client_name end |
#organization ⇒ String (readonly)
21 22 23 |
# File 'lib/ridley/connection.rb', line 21 def organization @organization end |
#retries ⇒ Integer (readonly)
Returns how many retries to attempt on HTTP requests.
28 29 30 |
# File 'lib/ridley/connection.rb', line 28 def retries @retries end |
#retry_interval ⇒ Float (readonly)
Returns time to wait between retries.
31 32 33 |
# File 'lib/ridley/connection.rb', line 31 def retry_interval @retry_interval end |
Instance Method Details
#api_type ⇒ Symbol
88 89 90 |
# File 'lib/ridley/connection.rb', line 88 def api_type organization.nil? ? :foss : :hosted end |
#foss? ⇒ Boolean
98 99 100 |
# File 'lib/ridley/connection.rb', line 98 def foss? api_type == :foss end |
#hosted? ⇒ Boolean
93 94 95 |
# File 'lib/ridley/connection.rb', line 93 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.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ridley/connection.rb', line 104 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
116 117 118 |
# File 'lib/ridley/connection.rb', line 116 def server_url self.url_prefix.to_s end |
#stream(target, destination) ⇒ Boolean
Stream the response body of a remote URL to a file on the local file system
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 165 166 167 168 |
# File 'lib/ridley/connection.rb', line 128 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.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.cp(local.path, destination) File.exists?(destination) rescue OpenURI::HTTPError => ex abort(ex) ensure local.close(true) unless local.nil? end |