RS Tweet Builder

Sierra Trading Post affiliate link builder system.

by Adam Patridge

HTML

<script src="https://dl.dropboxusercontent.com/u/6593594/cdn/js/URI-1.14.1.js"></script>
<script src="https://github.com/downloads/jaubourg/jquery-jsonp/jquery.jsonp-2.4.0.min.js"></script>
<script src="https://cdn.rawgit.com/lodash/lodash/2.4.1/dist/lodash.min.js"></script>
<script src="https://cdn.rawgit.com/jeresig/jquery.hotkeys/818081bbcb04b1916f11472c51564e39561009ee/jquery.hotkeys.js"></script>
<div>
    <button id="createButton">Create</button>
    <p id="rawUrl"></p>
    <p id="affiliateUrl"></p>
    <div>
        <p id="shortUrl"></p>
        <textarea id="tweet" style="width:100%; height:50px;"></textarea>
        <img id="productImage" style="width:250px; height:250px;"/>
    </div>
</div>

CSS

.sold-out { border: 1px solid red; }

JavaScript

/*global document, $, prompt */

$(function () {
    "use strict";

    var askForUrl = function () {
        return prompt("Product URL to prep?") || "http://www.sierratradingpost.com/x~p~15000/?colorFamily=01";
    };
    var $affiliateUrl = $("#affiliateUrl"),
        $shortUrl = $("#shortUrl"),
        $tweet = $("#tweet"),
        $productImage = $("#productImage");
    var runThings = function (e) {
        var urlToProcess = askForUrl();
        $affiliateUrl.text("");
        $shortUrl.text("");
        $tweet.empty();
        $productImage.attr("src", "");
        getProductDetails(urlToProcess);
        console.warn("in: " + urlToProcess);
        e.preventDefault();
    };
    $("#createButton").on("click", runThings);
    $(document).on("keydown", null, "c", runThings);
    $tweet.focus(function () {
        var $this = $(this);
        $this.select();

        // Work around Chrome's little problem
        $this.mouseup(function () {
            // Prevent further mouseup intervention
            $this.unbind("mouseup");
            return false;
        });
    });
    var affiliatizeUrl = function (url) {
        var affiliateApiUrl = new URITemplate("http://classic.avantlink.com/api.php{?q*}").expand({
            "q": {
                "affiliate_id": "142758",
                "module": "CustomLink",
                "output": "xml",
                "merchant_id": "10921",
                "website_id": "177050",
                "merchant_url": url
            }
        });
        return $.Deferred(function (dfd) {
            $.ajax({
                url: affiliateApiUrl,
                dataType: "xml"
            })
            .done(function (xml) {
                var $xmlResponse = $(xml).find("response");
                var status = $xmlResponse.find("status").text();
                if (status !== "Success") {
                    dfd.reject();
                }
                else {
                    var url =...