Created
March 21, 2009 05:13
-
-
Save KeithHanson/82736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 78c6cfdc2212f45741f5b045ccfef1761d33c9f8 Mon Sep 17 00:00:00 2001 | |
From: Keith Hanson <[email protected][color]> | |
Date: Thu, 19 Mar 2009 19:29:23 -0500 | |
Subject: [PATCH] HAlf.com integration. | |
--- | |
app/views/shared/_affiliate.html.erb | 2 +- | |
lib/affiliates.rb | 2 +- | |
lib/affiliates/affiliate_result.rb | 6 ++-- | |
lib/affiliates/half.rb | 64 ++++++++++++++++++++++++++++++++++ | |
public/img/half.png | Bin 0 -> 1040 bytes | |
5 files changed, 69 insertions(+), 5 deletions(-) | |
create mode 100644 lib/affiliates/half.rb | |
create mode 100644 public/img/half.png | |
diff --git a/app/views/shared/_affiliate.html.erb b/app/views/shared/_affiliate.html.erb | |
index 5c6ce61..e4f2f6e 100644 | |
--- a/app/views/shared/_affiliate.html.erb | |
+++ b/app/views/shared/_affiliate.html.erb | |
@@ -1,3 +1,3 @@ | |
<a class="affiliate" href="<%= affiliate.purchase_link %>"> | |
- <img src="<%= affiliate.image_location %>" alt="" /> $<%= affiliate.lowest_price %> | |
+ <img src="<%= affiliate.image_location %>" alt="" /> $<%= affiliate.lowest_price %> | |
</a> | |
\ No newline at end of file | |
diff --git a/lib/affiliates.rb b/lib/affiliates.rb | |
index da90d23..a10e20e 100644 | |
--- a/lib/affiliates.rb | |
+++ b/lib/affiliates.rb | |
@@ -44,7 +44,7 @@ module Affiliates | |
end | |
end | |
- sleep 3 | |
+ sleep 5 | |
@threads.each {|thread| thread.terminate! if thread.alive?} | |
end | |
diff --git a/lib/affiliates/affiliate_result.rb b/lib/affiliates/affiliate_result.rb | |
index 77b6661..7615c4d 100644 | |
--- a/lib/affiliates/affiliate_result.rb | |
+++ b/lib/affiliates/affiliate_result.rb | |
@@ -19,12 +19,12 @@ class AffiliateResult | |
case @affiliate_type | |
when :purchase | |
if affiliate_data[:used_price].to_s.gsub("$", "").to_f < affiliate_data[:new_price].to_s.gsub("$", "").to_f || affiliate_data[:new_price].nil? | |
- return affiliate_data[:used_price].to_s.gsub("$", "") | |
+ return "%.2f" % affiliate_data[:used_price].to_s.gsub("$", "") unless affiliate_data[:used_price].to_s.gsub("$", "").blank? | |
else | |
- return affiliate_data[:new_price].to_s.gsub("$", "") | |
+ return "%.2f" % affiliate_data[:new_price].to_s.gsub("$", "") unless affiliate_data[:new_price].to_s.gsub("$", "").blank? | |
end | |
when :rent | |
- return affiliate_data[:rent_price].to_s.gsub("$", "") | |
+ return "%.2f" % affiliate_data[:rent_price].to_s.gsub("$", "") unless affiliate_data[:rent_price].to_s.gsub("$", "").blank? | |
end | |
end | |
diff --git a/lib/affiliates/half.rb b/lib/affiliates/half.rb | |
new file mode 100644 | |
index 0000000..040155c | |
--- /dev/null | |
+++ b/lib/affiliates/half.rb | |
@@ -0,0 +1,64 @@ | |
+class Half < AffiliateResult | |
+ def initialize(isbn) | |
+ super(isbn, "DarCours-3722-42f2-b992-7e6a886d8005") | |
+ end | |
+ | |
+ def set_type | |
+ @affiliate_type = :purchase #|| :rent || :ebook | |
+ end | |
+ | |
+ def set_image_location | |
+ @image_location = "/img/half.png" | |
+ end | |
+ | |
+ def execute_request | |
+ url = "http://open.api.ebay.com/shopping?" | |
+ | |
+ query_string = "appid=" + @affiliate_id | |
+ query_string += "&version=517&siteid=0&callname=FindHalfProducts" | |
+ query_string += "&QueryKeywords=" + @isbn | |
+ query_string += "&responseencoding=XML&callback=true" | |
+ | |
+ # debugger | |
+ | |
+ | |
+ response = HTTParty.get(url + query_string)["FindHalfProductsResponse"] | |
+ | |
+ unless response["Ack"] != "Success" | |
+ book = {} | |
+ book[:title] = response["Products"]["Product"]["Title"] | |
+ potential_author = response["Products"]["Product"]["ItemSpecifics"]["NameValueList"].select{|item| item["Name"] == "Author"} | |
+ unless potential_author.nil? || potential_author.empty? | |
+ book[:authors] = potential_author.first["Value"] | |
+ end | |
+ book[:used_price] = response["Products"]["Product"]["MinPrice"] | |
+ | |
+ book[:url] = response["ProductSearchURL"] | |
+ | |
+ if !book[:used_price].nil? && book[:used_price].to_f > 0.0 && !book[:url].nil? && book[:url] != "" | |
+ self.affiliate_data[:author] = book[:authors] | |
+ self.affiliate_data[:used_price] = book[:used_price] | |
+ self.affiliate_data[:purchase_link] = "http://rover.ebay.com/rover/1/8971-56017-19255-0/1?type=6&campid=5336246416&toolid=10001&customid=osu&ext=#{@isbn}&mpre=http%3A%2F%2Fsearch.half.ebay.com%2F#{@isbn}" | |
+ end | |
+ | |
+ return true | |
+ else | |
+ self.errors << "Half threw the following error: #{response["Ack"]} -- #{response["Errors.ErrorClassification"]}" | |
+ end | |
+ | |
+ | |
+ return false | |
+ end | |
+ | |
+ def collect | |
+ if valid_isbn? | |
+ if !execute_request | |
+ self.errors << "Could not find a book with both price and url" | |
+ end | |
+ else | |
+ self.errors << "Invalid ISBN" | |
+ end | |
+ | |
+ return self | |
+ end | |
+end | |
\ No newline at end of file | |
diff --git a/public/img/half.png b/public/img/half.png | |
new file mode 100644 | |
index 0000000000000000000000000000000000000000..9fe9a35f8806fee6daf30e5a9c895847ee88c4af | |
GIT binary patch | |
literal 1040 | |
zcmV+r1n>KaP)<h;3K|Lk000e1NJLTq005Z)001lq0{{R3L51p00000PbVXQnQ*UN; | |
zcVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBUyfly3TML<A6e}8}P@9*E=-(X;1ARr*0 | |
zpP%*5%@7a}kB^UVaB$Dh&rncM{PXfQ4-MdMXxT|Z@V2wwVOuaTF#6}_<9c+judmxx | |
zPV1hU-eOzjhJp9m+491`>YkeIs;BkN%<ZbC_uJX%l8!qlC+L!m+Q-IWCM3VVzt%l9 | |
z00030|Nk-$17-jK10_jBK~#9!?3mYfqc9LaRRB{%Gwx2?BH;f&a8Vyv0(L^yK6^Yb | |
zmhgZsqiG1W_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MnLSTZ+XZn8t | |
literal 0 | |
HcmV?d00001 | |
-- | |
1.5.5+GitX |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment