Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add frequently used accessors to HeadObjectOutput #91

Merged
merged 2 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/awscr-s3/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ module Awscr::S3
# p resp.size # => 123
# p resp.status # => HTTP::Status::OK
# p resp.last_modified # => "Wed, 19 Jun 2019 11:55:33 GMT"
# p resp.etag # => ""
# p resp.meta # => {"my_tag" => "my_value"}
# ```
def head_object(bucket, object : String, headers : Hash(String, String) = Hash(String, String).new)
resp = http.head("/#{bucket}/#{Util.encode(object)}", headers: headers)
Expand Down
33 changes: 0 additions & 33 deletions src/awscr-s3/head_object_output.cr

This file was deleted.

54 changes: 54 additions & 0 deletions src/awscr-s3/responses/head_object_output.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Awscr::S3::Response
class HeadObjectOutput
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S %Z"

# The body of the request object
getter status : HTTP::Status
getter status_message : String | Nil
getter headers : HTTP::Headers

# Create a `GetObjectOutput` response from an
# `HTTP::Client::Response` object
def self.from_response(response : HTTP::Client::Response)
new(
status: response.status,
status_message: response.status_message,
headers: response.headers
)
end

def initialize(@status, @status_message, @headers)
end

{% for f in ["Cache-Control", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Type",] %}
def {{ f.id.stringify.underscore.gsub(/-/, "_").id }} : String?
headers["{{ f.id }}"]?
end
{% end %}

def last_modified : Time
parse_date(headers["Last-Modified"])
end

def size : UInt64
headers["Content-Length"].to_u64
end

def etag : String?
headers["ETag"].try{|v| v.strip('"')}
end

def meta : Hash(String, String)
meta = {} of String => String
response.headers.each do |k, v|
next unless k.starts_with?("x-amz-meta-")
meta[k.lchop("x-amz-meta-")] = v.first
end
meta
end

private def parse_date(date : String)
Time.parse!(date.gsub(/\s{2,}/, ' '), DATE_FORMAT)
end
end
end