Skip to content

Instantly share code, notes, and snippets.

@gerhard
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerhard/7203f3bb51d1ad59c886 to your computer and use it in GitHub Desktop.
Save gerhard/7203f3bb51d1ad59c886 to your computer and use it in GitHub Desktop.
Servers from hash - ordering, match by name
Server = Struct.new(:ami, :name, :created_at) do
def self.from_aws(ami_name_created_at)
ami, name, created_at_string = ami_name_created_at[0], *ami_name_created_at[1].split(" ")
new(ami, name, Time.parse(created_at_string))
end
end
class Servers
def self.from_aws_hash(hash)
new(hash.map(&Server.method(:from_aws)))
end
attr_reader :all
def initialize(servers)
@all = servers
end
def order_by(property)
Servers.new(
all.sort_by(&property)
)
end
def where_name_matches(name)
Servers.new(
all.select { |server| server.name.index(name.to_s) }
)
end
end
if defined?(RSpec)
describe Server do
describe ".from_aws" do
let(:server) {
Server.from_aws(["ami-600925fe", "sns-example 1983-01-05T16:21:08Z"])
}
it "#ami" do
expect(server.ami).to eql("ami-600925fe")
end
it "#name" do
expect(server.name).to eql("sns-example")
end
it "#created_at" do
expect(server.created_at).to eql(Time.utc(1983, 1, 5, 16, 21, 8))
end
end
end
describe Servers do
describe ".from_aws_hash" do
it "builds list of Server objects" do
expect(
Servers.from_aws_hash("ami-fe6af742"=>"tn-example 1998-11-22T09:54:51Z").all
).to eq([
Server.from_aws(["ami-fe6af742", "tn-example 1998-11-22T09:54:51Z"])
])
end
end
let(:servers_hash) { {
"ami-fe6af742"=>"sns-example 1998-11-22T09:54:51Z",
"ami-98d4ddef"=>"tn-example 2002-10-27T11:52:26Z",
"ami-600925fe"=>"sns-example 1983-01-05T16:21:08Z",
} }
let(:servers) { Servers.from_aws_hash(servers_hash) }
describe "#all" do
it "list of Server objects" do
servers.all.each do |server|
expect(server).to be_an_instance_of(Server)
end
end
end
describe "#ordered_by" do
it "orders ascendingly by created_at" do
expect(servers.order_by(:created_at).all.map(&:ami)).to eq(%w[
ami-600925fe ami-fe6af742 ami-98d4ddef
])
end
it "orders ascendingly by name" do
expect(servers.order_by(:name).all.map(&:ami)).to eq(%w[
ami-fe6af742 ami-600925fe ami-98d4ddef
])
end
end
describe "#where_name_matches" do
it "matches sns servers" do
expect(servers.where_name_matches("sns").all.map(&:ami)).to eq(%w[
ami-fe6af742 ami-600925fe
])
end
it "matches tn servers" do
expect(servers.where_name_matches("tn").all.map(&:ami)).to eq(%w[
ami-98d4ddef
])
end
end
describe "match name and order" do
it "an ordered subset of servers" do
expect(
servers.where_name_matches("sns").order_by(:created_at).all.map(&:ami)
).to eq(%w[
ami-600925fe ami-fe6af742
])
end
end
end
end
@allolex
Copy link

allolex commented Jun 18, 2014

With tests! You've taken this quite seriously. :)

@nodunayo
Copy link

Gerhard always does things seriously!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment