Class: Ridley::Connection

Inherits:
Faraday::Connection
  • Object
show all
Includes:
Celluloid
Defined in:
lib/ridley/connection.rb

Overview

Author:

Constant Summary collapse

VALID_OPTIONS =
[
  :retries,
  :retry_interval,
  :ssl,
  :proxy
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url, client_name, client_key, options = {}) ⇒ Connection

Returns a new instance of Connection

Parameters:

  • server_url (String)
  • client_name (String)
  • client_key (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :retries (Integer) — default: 5

    retry requests on 5XX failures

  • :retry_interval (Float) — default: 0.5

    how often we should pause between retries

  • :ssl (Hash)
    • :verify (Boolean) [true] set to false to disable SSL verification

  • :proxy (URI, String, Hash)

    URI, String, or Hash of HTTP proxy options


39
40
41
42
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
# File 'lib/ridley/connection.rb', line 39

def initialize(server_url, client_name, client_key, options = {})
  options         = options.reverse_merge(retries: 5, retry_interval: 0.5)
  @client_name    = client_name
  @client_key     = client_key
  @retries        = options[:retries]
  @retry_interval = options[:retry_interval]

  options[:builder] = Faraday::Builder.new do |b|
    b.response :json
    b.request :retry,
      max: @retries,
      interval: @retry_interval,
      exceptions: [
        Ridley::Errors::HTTP5XXError,
        Errno::ETIMEDOUT,
        Faraday::Error::TimeoutError
      ]
    b.response :chef_response
    b.request :chef_auth, client_name, client_key

    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), options)
  @headers[:user_agent] = "Ridley v#{Ridley::VERSION}"
end

Instance Attribute Details

#client_keyObject (readonly)

Returns the value of attribute client_key


18
19
20
# File 'lib/ridley/connection.rb', line 18

def client_key
  @client_key
end

#client_nameObject (readonly)

Returns the value of attribute client_name


19
20
21
# File 'lib/ridley/connection.rb', line 19

def client_name
  @client_name
end

#organizationObject (readonly)

Returns the value of attribute organization


17
18
19
# File 'lib/ridley/connection.rb', line 17

def organization
  @organization
end

#retriesInteger (readonly)

Returns how many retries to attempt on HTTP requests

Returns:

  • (Integer)

    how many retries to attempt on HTTP requests


22
23
24
# File 'lib/ridley/connection.rb', line 22

def retries
  @retries
end

#retry_intervalFloat (readonly)

Returns time to wait between retries

Returns:

  • (Float)

    time to wait between retries


25
26
27
# File 'lib/ridley/connection.rb', line 25

def retry_interval
  @retry_interval
end

Instance Method Details

#api_typeSymbol

Returns:

  • (Symbol)

81
82
83
# File 'lib/ridley/connection.rb', line 81

def api_type
  organization.nil? ? :foss : :hosted
end

#foss?Boolean

Returns:

  • (Boolean)

91
92
93
# File 'lib/ridley/connection.rb', line 91

def foss?
  api_type == :foss
end

#hosted?Boolean

Returns:

  • (Boolean)

86
87
88
# File 'lib/ridley/connection.rb', line 86

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.


97
98
99
100
101
# File 'lib/ridley/connection.rb', line 97

def run_request(*args)
  super
rescue Errors::HTTPError => ex
  abort(ex)
end

#server_urlObject


103
104
105
# File 'lib/ridley/connection.rb', line 103

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

Parameters:

  • target (String)

    a URL to stream the response body from

  • destination (String)

    a location on disk to stream the content of the response body to


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
# File 'lib/ridley/connection.rb', line 113

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|
      local.write(remote.read)
    end
  end

  local.flush
  FileUtils.mv(local.path, destination)
rescue OpenURI::HTTPError => ex
  abort(ex)
ensure
  local.close(true) unless local.nil?
end