Class: S3Light::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/s3-light/connection.rb,
lib/s3-light/connection/body.rb,
lib/s3-light/connection/response.rb

Defined Under Namespace

Classes: Body, HttpError, Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, access_key_id:, secret_access_key:, ssl_context:) ⇒ Connection

Returns a new instance of Connection.



19
20
21
22
23
24
25
# File 'lib/s3-light/connection.rb', line 19

def initialize(endpoint:, access_key_id:, secret_access_key:, ssl_context:)
  @endpoint = URI(endpoint)
  @access_key_id = access_key_id
  @secret_access_key = secret_access_key
  @ssl_context = ssl_context
  @opened = false
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



17
18
19
# File 'lib/s3-light/connection.rb', line 17

def endpoint
  @endpoint
end

Class Method Details

.close_connection(connection) ⇒ Object



84
85
86
87
88
# File 'lib/s3-light/connection.rb', line 84

def self.close_connection(connection)
  proc do
    connection.close
  end
end

Instance Method Details

#==(other_connection) ⇒ Object



76
77
78
# File 'lib/s3-light/connection.rb', line 76

def ==(other_connection)
  @endpoint == other_connection.endpoint
end

#closeObject



71
72
73
74
# File 'lib/s3-light/connection.rb', line 71

def close
  @persistent_connection&.close
  @opened = false
end

#download_file(path, output_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/s3-light/connection.rb', line 52

def download_file(path, output_path)
  @opened = true
  full_path = URI.join(@endpoint, path).to_s
  request_time = Time.now.utc

  headers = build_headers('GET', full_path, {}, Body.new(nil), request_time)

  File.open(output_path, 'wb') do |file|
    response = persistent_connection.headers(headers).get(full_path, ssl_context: @ssl_context)
    raise HttpError.new(response) if response.code > 399

    response.body.each do |chunk|
      file.write(chunk)
    end
  end

  output_path
end

#inspectObject



80
81
82
# File 'lib/s3-light/connection.rb', line 80

def inspect
  "#<#{self.class.name} @endpoint=#{@endpoint} @opened=#{@opened}>"
end

#make_request(method, path, headers: {}, body: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/s3-light/connection.rb', line 39

def make_request(method, path, headers: {}, body: nil)
  @opened = true
  full_path = URI.join(@endpoint, path).to_s
  request_time = Time.now.utc
  body = body.is_a?(Body) ? body : Body.new(body)

  headers = build_headers(method, full_path, headers, body, request_time)

  response = stream_request(method, full_path, headers, body)

  handle_response(response)
end

#persistent_connectionObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/s3-light/connection.rb', line 27

def persistent_connection
  return @persistent_connection if @persistent_connection

  @persistent_connection = HTTP.persistent(@endpoint).headers(
    'User-Agent' => "S3Light/#{S3Light::VERSION}",
    'Host' => @endpoint.host
  )
  ObjectSpace.define_finalizer(self, self.class.close_connection(@persistent_connection))

  @persistent_connection
end