Skip to content

Instantly share code, notes, and snippets.

@saranyan
Created March 13, 2013 21:22
Show Gist options
  • Save saranyan/5156416 to your computer and use it in GitHub Desktop.
Save saranyan/5156416 to your computer and use it in GitHub Desktop.
Sincerely API + Bigcommerce
require 'bigcommerce'
require 'json'
require 'rest-client'
require 'pp'
store_url = 'https://store-bwvr466.mybigcommerce.com'
api_key = 'apikey'
api_user = 'username'
api = Bigcommerce::Api.new({
:store_url => store_url,
:username => api_user,
:api_key => api_key
})
sender_info = {
:name => "Saranyan Vigraham",
:email => "sv@bigcommerce.com",
:street1 => "2711 W Anderson Ln",
:city => "Austin",
:state => "TX",
:postalcode => "78757",
:country => "UNITED STATES"
}
sincerely_create_url = 'https://snapi.sincerely.com/shiplib/create'
sincerely_upload_url = 'https://snapi.sincerely.com/shiplib/upload'
sincerely_app_key = 'appkey'
front_photo = "iVBORw0KGgoAAAANSUhEUgAAAUAAAAFACAIAAABC8jL9AAAAG....="
#upload the image to sincerely
photo_id = JSON.parse(RestClient.post sincerely_upload_url, {:photo => front_photo, :appkey => sincerely_app_key,:multipart => true})["id"]
customers = api.get_customers
addresses = []
customers.each do |c|
address = api.get_customer_addresses(c["id"])
#check if addresses exist for the customer. if they do, pick the first one for the sake of this example to send the thank you card to.
if address.is_a? Array
a = address.first
addresses << {:name => "#{a["first_name"]} #{a["last_name"]}",
:street1 => a["street_1"],
:city => a["city"],
:state => a["state"],
:postalcode => a["zip"],
:country => a["country"]
}
end
end
#we are going to use the same photoid for front and profile/brand images for brevity
#create a request envelope to create a card
#define test_mode => true for a test app. this will not create a card in real. refer to the sincerely api
card_request = {
:appkey => sincerely_app_key,
:testMode => true,
:frontPhotoId => photo_id,
:recipients => addresses.to_json,
:sender => sender_info.to_json
}
resp = RestClient.post(sincerely_create_url,card_request,:accept=>:json)
pp JSON.parse(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment