Skip to content

Commit

Permalink
Merge pull request #77 from sailthru/simplify-push-content
Browse files Browse the repository at this point in the history
Simplify push content and support updating by custom key
  • Loading branch information
Lindsay Neubauer authored Jun 26, 2018
2 parents 00181bb + 64fc678 commit 978deed
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
20 changes: 18 additions & 2 deletions lib/sailthru/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -468,6 +468,7 @@ def stats_send(template = nil, start_date = nil, end_date = nil, options = {})
api_get(:stats, data)
end

# <b>DEPRECATED:</b> Please use save_content
# params
# title, String
# url, String
Expand Down Expand Up @@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion lib/sailthru/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Sailthru
VERSION = '4.1.0'
VERSION = '4.2.0'
end
178 changes: 159 additions & 19 deletions test/sailthru/content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,167 @@ 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
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 "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']
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
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

0 comments on commit 978deed

Please sign in to comment.