Skip to content

Commit

Permalink
Merge pull request #1 from gettaxi/feature/message_metadata
Browse files Browse the repository at this point in the history
Feature: first parameter in publish method can be a hash specifying mess...
  • Loading branch information
Yarin Goldman committed Sep 14, 2014
2 parents 21fead4 + 48ce347 commit b008d9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/hub_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
require "rest-client"

module HubClient
def self.publish(type, content, env = nil)
def self.publish(metadata, content, env = nil)
raise ConfigArgumentMissing, "endpoint_url missing" unless HubClient.configuration.endpoint_url

payload = { type: type, content: content.to_json }
payload[:env] = HubClient.configuration.env if HubClient.configuration.env
payload[:env] = env if env
payload = (metadata.is_a?(String) || metadata.is_a?(Symbol)) ? { type: metadata.to_s } : metadata
payload[:content] = content
payload[:env] ||= env || payload[:env] || payload['env'] || HubClient.configuration.env

hub_url = build_hub_url(HubClient.configuration.endpoint_url)

RestClient.post(hub_url, payload, content_type: :json) do |response, request, result|
RestClient.post(hub_url, payload.to_json, content_type: :json, accept: :json) do |response, request, result|
handle_response(response, request, result)
end
end
Expand Down
26 changes: 22 additions & 4 deletions spec/hub_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

it "publishes a message to hub" do
stub_request(:post, HubClient.build_hub_url(HubClient.configuration.endpoint_url)).
with(body: { env: "il-qa2", type: "order_created", content: { some: "content" }.to_json }).
with(body: { env: "il-qa2", type: "order_created", content: { some: "content" } }).
to_return(status: 204)

HubClient.publish(:order_created, { some: "content" })
Expand All @@ -31,7 +31,7 @@

it "logs the request when hub didn't return success code" do
stub_request(:post, HubClient.build_hub_url(HubClient.configuration.endpoint_url)).
with(body: { env: "il-qa2", type: "order_created", content: { some: "content" }.to_json }).
with(body: { env: "il-qa2", type: "order_created", content: { some: "content" } }).
to_return(status: 500)

expect(HubClient.logger).to receive(:info)
Expand All @@ -41,13 +41,31 @@

it "overrides the env when supplied" do
stub_request(:post, HubClient.build_hub_url(HubClient.configuration.endpoint_url)).
with(body: { env: "batman-env", type: "order_created", content: { some: "content" }.to_json }).
with(body: { env: "batman-env", type: "order_created", content: { some: "content" } }).
to_return(status: 204)

HubClient.publish(:order_created, { some: "content" }, "batman-env")
assert_requested :post, HubClient.build_hub_url(HubClient.configuration.endpoint_url)
end

it 'uses metadata if provided' do
url = HubClient.build_hub_url(HubClient.configuration.endpoint_url)
stub_request(:post, url)
.with(body: {
type: 'order_created',
env: 'test-env',
execute_after_sec: 60,
content: {some: 'content'}
}.to_json)
.to_return(status: 204)
HubClient.publish({
type: 'order_created',
env: 'test-env',
execute_after_sec: 60
}, {some: "content"})
assert_requested :post, url
end

after(:all) { HubClient.reset_configuration }
end
end
Expand All @@ -60,4 +78,4 @@
expect(HubClient.send(:build_hub_url, "http://google.com/")).to eq("http://google.com/api/v10/messages")
end
end
end
end

0 comments on commit b008d9a

Please sign in to comment.