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

adds LastModifed to object as last_modifed #47

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
3 changes: 2 additions & 1 deletion examples/list_objects.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ client = Awscr::S3::Client.new(
)

client.list_objects(bucket: BUCKET, max_keys: 10).each do |response|
p response.contents.map(&.key)
keys = response.contents.sort_by(&.last_modified).map(&.key)
p keys
end
19 changes: 11 additions & 8 deletions spec/awscr-s3/client_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "../spec_helper"

module Awscr::S3
TEST_TIME_1 = Time.new(2009, 9, 11, 17, 50, 30, location: Time::Location::UTC)
TEST_TIME_2 = Time.new(2010, 10, 12, 17, 50, 30, location: Time::Location::UTC)

describe Client do
it "allows signer version" do
Client.new("adasd", "adasd", "adad", signer: :v2)
Expand Down Expand Up @@ -238,7 +241,7 @@ module Awscr::S3
<NextContinuationToken>token</NextContinuationToken
<Contents>
<Key>my-image.jpg</Key>
<LastModified>2009-10-12T17:50:30.000Z</LastModified>
<LastModified>2009-09-11T17:50:30.000Z</LastModified>
<ETag>"fba9dede5f27731c9771645a39863328"</ETag>
<Size>434234</Size>
<StorageClass>STANDARD</StorageClass>
Expand All @@ -256,7 +259,7 @@ module Awscr::S3
<IsTruncated>false</IsTruncated>
<Contents>
<Key>key2</Key>
<LastModified>2009-10-12T17:50:30.000Z</LastModified>
<LastModified>2010-10-12T17:50:30.000Z</LastModified>
<ETag>"fba9dede5f27731c9771645a39863329"</ETag>
<Size>1337</Size>
<StorageClass>STANDARD</StorageClass>
Expand All @@ -279,9 +282,9 @@ module Awscr::S3

expected_objects = [
Object.new("my-image.jpg", 434_234,
"\"fba9dede5f27731c9771645a39863328\""),
"\"fba9dede5f27731c9771645a39863328\"", TEST_TIME_1),
Object.new("key2", 1337,
"\"fba9dede5f27731c9771645a39863329\""),
"\"fba9dede5f27731c9771645a39863329\"", TEST_TIME_2),
]

objects.should eq([
Expand All @@ -303,14 +306,14 @@ module Awscr::S3
<IsTruncated>false</IsTruncated>
<Contents>
<Key>my-image.jpg</Key>
<LastModified>2009-10-12T17:50:30.000Z</LastModified>
<LastModified>2009-09-11T17:50:30.000Z</LastModified>
<ETag>&quot;fba9dede5f27731c9771645a39863328&quot;</ETag>
<Size>434234</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
<Contents>
<Key>key2</Key>
<LastModified>2009-10-12T17:50:30.000Z</LastModified>
<LastModified>2010-10-12T17:50:30.000Z</LastModified>
<ETag>&quot;fba9dede5f27731c9771645a39863329&quot;</ETag>
<Size>1337</Size>
<StorageClass>STANDARD</StorageClass>
Expand All @@ -323,9 +326,9 @@ module Awscr::S3

expected_objects = [
Object.new("my-image.jpg", 434_234,
"\"fba9dede5f27731c9771645a39863328\""),
"\"fba9dede5f27731c9771645a39863328\"", TEST_TIME_1),
Object.new("key2", 1337,
"\"fba9dede5f27731c9771645a39863329\""),
"\"fba9dede5f27731c9771645a39863329\"", TEST_TIME_2),
]

client = Client.new("us-east-1", "key", "secret")
Expand Down
22 changes: 15 additions & 7 deletions spec/awscr-s3/object_spec.cr
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
require "../spec_helper"

module Awscr::S3
OBJECT_TEST_TIME = Time.new(2019, 3, 2, 3, 9, 4, location: Time::Location::UTC)

describe Object do
it "is equal to another object if key size and etag are same" do
object = Object.new("test", 123, "etag")
Object.new("test", 123, "etag").should eq(object)
object = Object.new("test", 123, "etag", OBJECT_TEST_TIME)
Object.new("test", 123, "etag", OBJECT_TEST_TIME).should eq(object)
end

it "not equal to another object key size and etag" do
object = Object.new("test2", 123, "etag")
(Object.new("test", 123, "asd") == object).should eq(false)
object = Object.new("test2", 123, "etag", OBJECT_TEST_TIME)
(Object.new("test", 123, "asd", OBJECT_TEST_TIME) == object).should eq(false)
end

it "has key" do
object = Object.new("test", 123, "etag")
object = Object.new("test", 123, "etag", OBJECT_TEST_TIME)
object.key.should eq("test")
end

it "has size" do
object = Object.new("test", 123, "etag")
object = Object.new("test", 123, "etag", OBJECT_TEST_TIME)
object.size.should eq(123)
end

it "has etag" do
object = Object.new("test", 123, "etag")
object = Object.new("test", 123, "etag", OBJECT_TEST_TIME)
object.etag.should eq("etag")
end

it "has last_modified" do
object = Object.new("test", 123, "etag", OBJECT_TEST_TIME)
object.last_modified.should eq(OBJECT_TEST_TIME)
object.last_modified.utc?.should be_true
end
end
end
7 changes: 5 additions & 2 deletions src/awscr-s3/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ module Awscr::S3
# The `Object` etag
getter etag

def initialize(@key : String, @size : Int32, @etag : String)
# The time string the `Object` was last modifed
getter last_modified

def initialize(@key : String, @size : Int32, @etag : String, @last_modified : Time)
end

def_equals @key, @size, @etag
def_equals @key, @size, @etag, @last_modified
end
end
6 changes: 5 additions & 1 deletion src/awscr-s3/responses/list_objects_v2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ require "uri"

module Awscr::S3::Response
class ListObjectsV2
# :nodoc:
DATE_FORMAT = "%FT%T"

# Create a `ListObjectsV2` response from an
# `HTTP::Client::Response` object
def self.from_response(response)
Expand All @@ -20,8 +23,9 @@ module Awscr::S3::Response
key = object.string("Key")
size = object.string("Size").to_i
etag = object.string("ETag")
last_modified = Time.parse(object.string("LastModified"), DATE_FORMAT, Time::Location::UTC)

objects << Object.new(key, size, etag)
objects << Object.new(key, size, etag, last_modified)
end

new(name, prefix, key_count.to_i? || 0, max_keys.to_i, truncated == "true", token, objects)
Expand Down