From 48ce3477a333cf973c04769ac7dd6f72e6983448 Mon Sep 17 00:00:00 2001 From: Igor Shapiro Date: Sun, 14 Sep 2014 19:34:31 +0300 Subject: [PATCH] Feature: first parameter in publish method can be a hash specifying message metadata --- lib/hub_client.rb | 10 +++++----- spec/hub_client_spec.rb | 26 ++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/hub_client.rb b/lib/hub_client.rb index 8756ead..5cb32cc 100644 --- a/lib/hub_client.rb +++ b/lib/hub_client.rb @@ -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 diff --git a/spec/hub_client_spec.rb b/spec/hub_client_spec.rb index 07fc150..e380037 100644 --- a/spec/hub_client_spec.rb +++ b/spec/hub_client_spec.rb @@ -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" }) @@ -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) @@ -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 @@ -60,4 +78,4 @@ expect(HubClient.send(:build_hub_url, "http://google.com/")).to eq("http://google.com/api/v10/messages") end end -end \ No newline at end of file +end