Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #13 from articulate/prod-funk
Browse files Browse the repository at this point in the history
Fixing the timestamp 🐛 once and for all
  • Loading branch information
plukevdh authored Aug 24, 2016
2 parents fb66950 + 84ab5c9 commit 27ad990
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 17 deletions.
14 changes: 14 additions & 0 deletions spec/configs/acl_config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ module Biplane
acl.member_route.should be_a(Route)
acl.member_route.to_s.should eq "/consumers/#{parent.lookup_key}/acls/:id"
end

it "outputs attrs for create" do
acl.for_create.should eq({
"group": acl.group,
"created_at": "now",
})
end

it "uses epoch time for update" do
acl.for_update.should eq({
"group": acl.group,
"created_at": Time.now.epoch,
})
end
end
end
20 changes: 20 additions & 0 deletions spec/configs/api_config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,25 @@ module Biplane
plugins.should be_a(Array(PluginConfig))
plugins.map { |p| p.name }.should eq ["acl", "jwt"]
end

it "outputs attrs for create" do
api.for_create.should eq({
"name": api.name,
"request_path": api.request_path,
"strip_request_path": api.strip_request_path,
"upstream_url": api.upstream_url,
"created_at": "now",
})
end

it "uses epoch time for update" do
api.for_update.should eq({
"name": api.name,
"request_path": api.request_path,
"strip_request_path": api.strip_request_path,
"upstream_url": api.upstream_url,
"created_at": Time.now.epoch,
})
end
end
end
14 changes: 14 additions & 0 deletions spec/configs/consumer_config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,19 @@ module Biplane
items.should be_a(Array(AclConfig | CredentialConfig))
items.size.should eq 2
end

it "outputs attrs for create" do
consumer.for_create.should eq({
"username": consumer.username,
"created_at": "now",
})
end

it "uses epoch time for update" do
consumer.for_update.should eq({
"username": consumer.username,
"created_at": Time.now.epoch,
})
end
end
end
10 changes: 9 additions & 1 deletion spec/configs/credential_config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ module Biplane
credential.parent = parent = yaml_fixture(ConsumerConfig)

it "can flatten for params" do
credential.as_params.should eq({
credential.for_create.should eq({
"key": "xxx",
"secret": "yyy",
"created_at": "now",
})
end

it "outputs epoch time for update" do
credential.for_update.should eq({
"key": "xxx",
"secret": "yyy",
"created_at": Time.now.epoch,
})
end

it "knows collection path" do
credential.collection_route.should be_a(Route)
credential.collection_route.to_s.should eq "/consumers/#{parent.lookup_key}/#{credential.name}"
Expand Down
14 changes: 12 additions & 2 deletions spec/configs/plugin_config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Biplane
end

it "can present config as params" do
plugin.as_params.should eq({
plugin.for_create.should eq({
"name": "acl",
"config": {
"whitelist": ["docs-auth", "google-auth"],
Expand All @@ -40,13 +40,23 @@ module Biplane
},
}))

odd.as_params.should eq({
odd.for_create.should eq({
"name": "what",
"config": {
"whitelist": ["name", "only"],
},
"created_at": "now",
})
end

it "outputs epoch time for update" do
plugin.for_update.should eq({
"name": "acl",
"config": {
"whitelist": ["docs-auth", "google-auth"],
},
"created_at": Time.now.epoch,
})
end
end
end
10 changes: 6 additions & 4 deletions src/biplane/configs/acl_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ module Biplane
group: String,
})

def as_params
normalize(serialize, {
created_at: pg_now,
})
def for_create
normalize(serialize, {created_at: pg_now})
end

def for_update
normalize(for_create, {created_at: epoch_int})
end

def serialize
Expand Down
6 changes: 5 additions & 1 deletion src/biplane/configs/api_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Biplane
ChildCollection.new(@plugins, self)
end

def as_params
def for_create
{
"name": name,
"request_path": request_path,
Expand All @@ -41,6 +41,10 @@ module Biplane
}
end

def for_update
for_create.merge({"created_at": epoch_int})
end

def serialize
{
"name": name,
Expand Down
6 changes: 5 additions & 1 deletion src/biplane/configs/consumer_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ module Biplane
credentials: {type: Array(CredentialConfig), default: Array(CredentialConfig).new},
})

def as_params
def for_create
{
"username": username,
"created_at": pg_now,
}
end

def for_update
for_create.merge({"created_at": epoch_int})
end

def acls
ChildCollection.new(@acls, self)
end
Expand Down
10 changes: 6 additions & 4 deletions src/biplane/configs/credential_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ module Biplane
route(collection_key, {name: name})
end

def as_params
normalize(attributes, {
created_at: pg_now,
})
def for_create
normalize(attributes, {created_at: pg_now})
end

def for_update
normalize(for_create, {created_at: epoch_int})
end

def serialize
Expand Down
6 changes: 5 additions & 1 deletion src/biplane/configs/plugin_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ module Biplane
@parsed_attrs ||= @attributes.nil? ? Hash(String, Type).new : normalize(to_hash(@attributes) as Hash)
end

def as_params
def for_create
normalize(attributes, {
name: name,
created_at: pg_now,
})
end

def for_update
normalize(for_create, {created_at: epoch_int})
end

def serialize
serial = Hash(String, Type).new
serial["name"] = name
Expand Down
4 changes: 2 additions & 2 deletions src/biplane/kong_client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module Biplane
headers = HTTP::Headers.new
headers.add("Content-Type", "application/json")

params = config.as_params
params = config.for_create
response = @client.post(config.collection_route.to_s, headers, params.to_json) as HTTP::Client::Response
@client.close # close immediately since we might make nested requests

Expand Down Expand Up @@ -139,7 +139,7 @@ module Biplane
headers = HTTP::Headers.new
headers.add("Content-Type", "application/json")

params = normalize(config.as_params, {"id": object.id})
params = normalize(config.for_update, {"id": object.id})
response = @client.put(config.collection_route.to_s, headers, params.to_json) as HTTP::Client::Response

case response.status_code
Expand Down
4 changes: 4 additions & 0 deletions src/biplane/mixins/timestamps.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ module Biplane::Mixins
def pg_now
"now"
end

def epoch_int
Time.now.epoch
end
end
end
2 changes: 1 addition & 1 deletion src/biplane/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Biplane
VERSION = "1.3.11"
VERSION = "1.3.12"
end

0 comments on commit 27ad990

Please sign in to comment.