Created
April 19, 2012 17:50
-
-
Save jess/2422579 to your computer and use it in GitHub Desktop.
text links ads, rails
This file contains hidden or 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
# text links ads stuff | |
# --------------------------------------------------------------------------------------------------------------- | |
#controller action | |
def some_action | |
text_link_ads "your_id_here" | |
.... | |
end | |
#controllers/application_controller.rb | |
def text_link_ads(id) | |
require 'net/http' | |
require 'cgi' | |
url = "http://www.text-link-ads.com/xml.php?k=#{id}&l=rails-tla-2.0.1&f=json" | |
data = Rails.cache.read(id) | |
if data.present? | |
if data[:time] > Time.now - 6.hours | |
@links = data[:links] | |
else | |
set_tla_cache(id,url) | |
if @links.empty? | |
@links = data[:links] | |
end | |
end | |
else | |
set_tla_cache(id,url) | |
end | |
end | |
def set_tla_cache(id,url) | |
links = requester(url) rescue nil | |
time = links.nil? ? Time.now - 5.hours : Time.now | |
Rails.cache.write id, {:time => time, :links => links} | |
@links = links | |
end | |
def requester(url) | |
ActiveSupport::JSON.decode(http_get(url)) | |
end | |
def http_get(url) | |
Net::HTTP.get_response(URI.parse(url)).body.to_s | |
end | |
#view/partial | |
.tla | |
- unless @links.nil? or @links.size.zero? | |
%ul | |
- @links.each do |link| | |
%li | |
= link['BeforeText'].empty? ? '' : "#{link['BeforeText']} " | |
%a{:href => link['URL']}= link['Text'] | |
= link['AfterText'].empty? ? '' : " #{link['AfterText']}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment