LinkShare API

Ruby wrapper for LinkShare Publisher Web Services.
Supported web services:
If you need services that are not yet supported, feel free to contribute.
For questions or bugs please create an issue.
Requirements
Ruby 1.9 or above.
Installation
Add this line to your application's Gemfile:
gem 'linkshare_api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install linkshare_api
Configuration
Before using LinkShare API you need to set up your publisher token first. If you use Ruby on Rails, the token can be set in a configuration file (i.e. app/config/initializers/linkshare_api.rb
), otherwise just set it in your script. The token can be found on LinkShare's Web Services page under the Links tab.
LinkshareAPI.token = ENV["LINKSHARE_TOKEN"]
Affiliate ID is required for using Deep Linking.
LinkshareAPI.affiliate_id = ENV["LINKSHARE_AFFILIATE_ID"]
By default linkshare_api logs to STDOUT. To use your own logger implementation you have to specify it using LinkshareAPI.logger
Configuration example
Would apply for Ruby on Rails. Create app/config/initializers/linkshare_api.rb
and add the following content:
LinkshareAPI.token = ENV["LINKSHARE_TOKEN"]
LinkshareAPI.affiliate_id = ENV["LINKSHARE_AFFILIATE_ID"]
LinkshareAPI.logger = Rails.logger
Usage
Deep Linking
Generate affiliate URLs using Deep Linking service.
Below is an example of generating an affiliate URL for Walmart. Walmart merchant code is 2149
.
require "linkshare_api"
LinkshareAPI.affiliate_id = ENV["LINKSHARE_AFFILIATE_ID"]
url = "http://www.walmart.com/cp/Electronics/3944?povid=P1171-C1093.2766-L33"
affiliate_url = LinkshareAPI.link_generator(2149, url)
Note: The link is generated manually, therefore you must ensure that the Affiliate ID provided is valid and that merchant supports deeplinking.
Automated Link Generator
Deprecation Notice
As of October 2014, Automated LinkGenerator is discontinued in favor of Deep Linking. To use Deep Linking instead, you only have to set LinkshareAPI.affiliate_id
. Everything else remains the same.
Generate affiliate URLs using Automated LinkGenerator service.
Below is an example of generating an affiliate URL for Walmart. Walmart merchant code is 2149
.
url = "http://www.walmart.com/cp/Electronics/3944?povid=P1171-C1093.2766-L33"
affiliate_url = LinkshareAPI.link_generator(2149, url)
Merchandiser Query Tool
Search for products using Merchandiser Query Tool service.
response = LinkshareAPI.product_search(keyword: "laptop")
puts response.total_matches
puts response.total_pages
puts response.page_number
puts response.request.uri
response.data.each do |item|
puts "Title: #{item.productname}"
puts "Price: #{item.price.__content__} #{item.price.currency}"
puts "URL: #{item.linkurl}"
end
product_search
accepts a hash as argument, and can include all available options. For a complete list of options please visit https://rakutenlinkshare.zendesk.com/hc/en-us/articles/201483905-Merchandiser-Query-Tool-API-Guidelines.
options = {
keyword: "laptop",
mid: 2149,
cat: "Electronics",
max: 10,
sort: :retailprice,
sorttype: :asc
}
response = LinkshareAPI.product_search(options)
response.data.each do |item|
end
If there are multiple pages, you can retrieve all pages by using the all
method, as follows:
response.all.each do |item|
end
When using the all
method, response
object is updated with the data returned by the last API request (last page). response.all
returns the data
array.
Coupon Web Service
Easy access to coupons and promotional link data for your advertisers using Coupon Web Service
options = {
promotiontype: 3|5,
mid: 2149,
cat: 3,
network: 1
}
response = LinkshareAPI.coupon_web_service(options)
response.data.each do |item|
# Do stuff
end
LinkshareAPI.api_timeout
- the timeout set when initiating requests to LinkShare Web Services (default value is 30 seconds)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
)
- Commit your changes (
git commit -am 'Add some feature'
)
- Push to the branch (
git push origin my-new-feature
)
- Create new Pull Request
License
MIT