From 9821a3d32f844e203d75f87288476e32baae0b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Coelho?= Date: Wed, 20 Jun 2018 15:58:37 +1200 Subject: [PATCH 1/3] Update push_content helper specs to show how to post using the new API - old example was confusing as price is now a main parameter and should be sent as options instead of vars - old examples didn't test the parameters being sent --- test/sailthru/content_test.rb | 89 +++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 20 deletions(-) diff --git a/test/sailthru/content_test.rb b/test/sailthru/content_test.rb index 7b4ab01..60bf116 100644 --- a/test/sailthru/content_test.rb +++ b/test/sailthru/content_test.rb @@ -10,27 +10,76 @@ class ContentTest < Minitest::Test @api_call_url = sailthru_api_call_url(api_url, 'content') end - it "can push content with title, url, *array* tags and vars" do - title = 'unix is awesome' - url = 'http://example.com/hello-world' - date = nil - tags = ['unix', 'linux'] - vars = {:price => 55, :description => 'Hello World'} - stub_post(@api_call_url, 'content_valid.json') - response = @sailthru_client.push_content(title, url, date = nil, tags = tags, vars = vars) - refute_nil response['content'] - end - it "can push content with title, url, *string* tags and vars" do - title = 'unix is awesome' - url = 'http://example.com/hello-world' - date = nil - tags = 'unix, linux' - vars = {:price => 55, :description => 'Hello World'} - stub_post(@api_call_url, 'content_valid.json') - response = @sailthru_client.push_content(title, url, date = nil, tags = tags, vars = vars) - refute_nil response['content'] - end + describe '#push_content: DEPRECATED IN FAVOUR OF save_content' do + describe 'create content item' do + before do + title = 'Product Name here' + url = "http://example.com/product" + tags = "blue, jeans, size-m" + date = nil + vars = { + var1: 'var 1 value' + } + options = { + keys: { + sku: "123abc" + }, + description: "Product info text goes here.", + price: 2099, + inventory: 42, + images: { + full: { + url: "http://example.com/images/product.jpg" + } + }, + site_name: "Store" + } + + stub_post(@api_call_url, 'content_valid.json') + @response = @sailthru_client.push_content(title, url, date, tags, vars, options) + + @last_request_params = CGI::parse(FakeWeb.last_request.body) + + @expected_form_params = options.merge({ + title: title, + url: url, + vars: vars, + tags: tags, + }) + end + + it 'POST to the correct url' do + refute_nil @response['content'] + end + it 'POST with the correct parameters' do + form_data = JSON.parse(@last_request_params["json"][0], symbolize_names: true) + assert_equal(form_data, @expected_form_params) + end + end + + describe 'create content item with tags as array' do + before do + title = 'Product Name here' + url = "http://example.com/product" + tags = ['blue', 'jeans', 'size-m'] + + stub_post(@api_call_url, 'content_valid.json') + @response = @sailthru_client.push_content(title, url, nil, tags) + + @last_request_params = CGI::parse(FakeWeb.last_request.body) + end + + it 'POST to the correct url' do + refute_nil @response['content'] + end + + it 'POST form_data tags as string separated by ","' do + form_data = JSON.parse(@last_request_params["json"][0], symbolize_names: true) + assert_equal(form_data[:tags], 'blue,jeans,size-m') + end + end + end end end From 5eea1488f401ff7317e1eea499895a6ecfc98f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Coelho?= Date: Wed, 20 Jun 2018 16:15:14 +1200 Subject: [PATCH 2/3] Add helper save_content supporting all available options from the content api - Mark push_content as deprecated --- lib/sailthru/client.rb | 20 +++++++- test/sailthru/content_test.rb | 91 +++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/lib/sailthru/client.rb b/lib/sailthru/client.rb index 78073d1..5290cf2 100644 --- a/lib/sailthru/client.rb +++ b/lib/sailthru/client.rb @@ -235,10 +235,10 @@ def change_email(new_email, old_email, options = {}) data[:change_email] = old_email api_post(:email, data) end - + # returns: # Hash of response data. - # + # # Get all templates def get_templates(templates = {}) api_get(:template, templates) @@ -468,6 +468,7 @@ def stats_send(template = nil, start_date = nil, end_date = nil, options = {}) api_get(:stats, data) end + # DEPRECATED: Please use save_content # params # title, String # url, String @@ -497,6 +498,21 @@ def push_content(title, url, date = nil, tags = nil, vars = {}, options = {}) api_post(:content, data) end + # params + # id, String – An identifier for the item (by default, the item’s URL). + # options, Hash - Containing any of the parameters described on + # https://getstarted.sailthru.com/developers/api/content/#POST_Mode + # + # Push a new piece of content to Sailthru, triggering any applicable alerts. + # http://docs.sailthru.com/api/content + def save_content(id, options) + data = options + data[:id] = id + data[:tags] = data[:tags].join(',') if data[:tags].respond_to?(:join) + + api_post(:content, data) + end + # params # list, String # diff --git a/test/sailthru/content_test.rb b/test/sailthru/content_test.rb index 60bf116..7b5cf83 100644 --- a/test/sailthru/content_test.rb +++ b/test/sailthru/content_test.rb @@ -10,6 +10,97 @@ class ContentTest < Minitest::Test @api_call_url = sailthru_api_call_url(api_url, 'content') end + describe '#save_content' do + describe 'creating a content' do + before do + id = 'http://example.com/hello-world' + options = { + keys: { + sku: "123abc" + }, + title: "Product Name Here", + description: "Product info text goes here.", + price: 2099, + inventory: 42, + date: "2016-06-20 14:30:00 -0400", + tags: "blue, jeans, size-m", + vars: { + var1: "var 1 value" + }, + images: { + full: { + url: "http://example.com/images/product.jpg" + } + }, + site_name: "Store" + } + + stub_post(@api_call_url, 'content_valid.json') + + @response = @sailthru_client.save_content(id, options) + @last_request_params = CGI::parse(FakeWeb.last_request.body) + @expected_form_params = options.merge({id: id}) + end + + it 'POST to the correct url' do + refute_nil @response['content'] + end + + it 'POST with the correct parameters' do + form_data = JSON.parse(@last_request_params["json"][0], symbolize_names: true) + assert_equal(form_data, @expected_form_params) + end + end + + describe 'updating content by url and sending tags as array' do + before do + id = 'http://example.com/hello-world' + options = { + tags: ['tag1', 'tag2'], + } + + stub_post(@api_call_url, 'content_valid.json') + + @response = @sailthru_client.save_content(id, options) + @last_request_params = CGI::parse(FakeWeb.last_request.body) + @expected_form_params = options.merge({id: id}) + end + + it 'POST to the correct url' do + refute_nil @response['content'] + end + + it 'POST form_data tags as string separated by ","' do + form_data = JSON.parse(@last_request_params["json"][0], symbolize_names: true) + assert_equal(form_data[:tags], 'tag1,tag2') + end + end + + describe 'updating content searching by sku key instead of url' do + before do + id = '123abc' + options = { + key: 'sku', + title: "New Product Name Here", + } + + stub_post(@api_call_url, 'content_valid.json') + + @response = @sailthru_client.save_content(id, options) + @last_request_params = CGI::parse(FakeWeb.last_request.body) + @expected_form_params = options.merge({id: id}) + end + + it 'POST to the correct url' do + refute_nil @response['content'] + end + + it 'POST with the correct parameters' do + form_data = JSON.parse(@last_request_params["json"][0], symbolize_names: true) + assert_equal(form_data, @expected_form_params) + end + end + end describe '#push_content: DEPRECATED IN FAVOUR OF save_content' do describe 'create content item' do From 64fc67890a56f5c777c46396cd423f2d6764af20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Coelho?= Date: Wed, 20 Jun 2018 16:15:35 +1200 Subject: [PATCH 3/3] Bump version to 4.2.0 --- CHANGELOG.md | 4 ++++ README.md | 4 ++++ lib/sailthru/version.rb | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f13c2de..ab20fb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.2.0 + - added save_content supporting all fields from updated api + - push_content is now deprecated in favour of save_content + ## 4.1.0 - added get_last_rate_limit_info method call diff --git a/README.md b/README.md index b1076c0..d9fa16d 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,7 @@ An [API client library](https://getstarted.sailthru.com/developers/api-client/li For installation instructions, documentation, and examples please visit: [https://getstarted.sailthru.com/developers/api-client/ruby/](https://getstarted.sailthru.com/developers/api-client/ruby/) +## Running tests + +1. `bundle install` +2. `bundle exec rake test` diff --git a/lib/sailthru/version.rb b/lib/sailthru/version.rb index 7b83568..bf8faf5 100644 --- a/lib/sailthru/version.rb +++ b/lib/sailthru/version.rb @@ -1,3 +1,3 @@ module Sailthru - VERSION = '4.1.0' + VERSION = '4.2.0' end