Class: Sitethree::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/sitethree.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Search

Returns a new instance of Search.



16
17
18
19
# File 'lib/sitethree.rb', line 16

def initialize(options = {})
  self.class.publisher_id = options[:publisher_id]
  self.class.feed_id = options[:feed_id]
end

Class Attribute Details

.errorsObject

Returns the value of attribute errors.



12
13
14
# File 'lib/sitethree.rb', line 12

def errors
  @errors
end

.feed_idObject

Returns the value of attribute feed_id.



12
13
14
# File 'lib/sitethree.rb', line 12

def feed_id
  @feed_id
end

.keywordsObject

Returns the value of attribute keywords.



12
13
14
# File 'lib/sitethree.rb', line 12

def keywords
  @keywords
end

.publisher_idObject

Returns the value of attribute publisher_id.



12
13
14
# File 'lib/sitethree.rb', line 12

def publisher_id
  @publisher_id
end

.sourceObject

Returns the value of attribute source.



12
13
14
# File 'lib/sitethree.rb', line 12

def source
  @source
end

.user_agentObject

Returns the value of attribute user_agent.



12
13
14
# File 'lib/sitethree.rb', line 12

def user_agent
  @user_agent
end

.user_ipObject

Returns the value of attribute user_ip.



12
13
14
# File 'lib/sitethree.rb', line 12

def user_ip
  @user_ip
end

Instance Method Details

#get_ads(params = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sitethree.rb', line 21

def get_ads(params = {})
  ads = []
  url = "#{API_URL}?partner_id=#{self.class.publisher_id}&feed_id=#{self.class.feed_id}&source=#{CGI::escape(params[:source])}&user_ip=#{params[:user_ip]}&user_agent=#{CGI::escape(params[:user_agent])}&keywords=#{CGI::escape(params[:keywords])}#{params[:testapi] ? "&test=testapi" : ""}"
  uri = URI.parse(url)
  response = Net::HTTP.get_response(uri)
  doc = Nokogiri::XML.parse(response.body)
  response = doc.xpath("/response").first
  if response.attr("valid_request") == "true" && response.attr("num_results") != '0'
    doc.xpath("//ad").each do |ad|
      ads << {'title' => ad.attr("title"), 'description' => ad.attr("description"), 'display_url' => ad.attr("display_url"), 'click_url' => ad.text.strip}
    end
  else
    Rails.logger.info url
  end
  ads
end