Class: SmoScottishLidar::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/smo_scottish_lidar/client.rb

Overview

Low-level S3 client. Uses only Ruby stdlib net/http. Accesses the public (unsigned) srsp-open-data bucket directly over HTTPS.

Constant Summary collapse

MAX_KEYS =
1000
MSG_TOO_MANY_REDIRECTS =
"Too many redirects"
MSG_NO_LOCATION_HEADER =
"Redirect with no Location header"

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false) ⇒ Client

Returns a new instance of Client.



14
15
16
# File 'lib/smo_scottish_lidar/client.rb', line 14

def initialize(verbose: false)
  @verbose = verbose
end

Instance Method Details

#download_object(key, local_path, &progress_block) ⇒ Object

Downloads a single S3 object key to a local file path. Follows redirects, streams in chunks to avoid loading into memory. Returns true on success, raises on error.



41
42
43
44
45
46
47
48
# File 'lib/smo_scottish_lidar/client.rb', line 41

def download_object(key, local_path, &progress_block)
  url = "#{BASE_URL}/#{key}"
  log "Downloading: #{url} -> #{local_path}"

  uri = URI.parse(url)
  fetch_with_redirect(uri, local_path, &progress_block)
  true
end

#list_objects(prefix) ⇒ Object

Lists all object keys under a given S3 prefix. Handles S3 pagination (continuation tokens) automatically. Returns an Array of Hashes: [{ key:, size:, last_modified: }, …]



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smo_scottish_lidar/client.rb', line 21

def list_objects(prefix)
  objects = []
  continuation_token = nil

  loop do
    xml = fetch_list_page(prefix, continuation_token)
    page_objects, next_token, truncated = parse_list_response(xml)
    objects.concat(page_objects)

    break unless truncated

    continuation_token = next_token
  end

  objects
end