Class: Active::Services::Search

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

Constant Summary collapse

SEARCH_URL =

attr_accessor :location, :category, :channels, :daterange, :keywords, :radius, :limit, :sort, :page, :offset,

:asset_type_id, :api_key, :num_results, :view, :facet, :sort
"http://search.active.com"
DEFAULT_TIMEOUT =
5

Class Method Summary collapse

Class Method Details

.construct_url(arg_options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/services/search.rb', line 65

def self.construct_url(arg_options={})
  options = {
    :api_key => "",
    :view => "json",
    :facet => Facet.ACTIVITIES,
    :sort => Sort.DATE_ASC,
    :radius => "10",
    :meta => "",
    :num_results => "10",
    :page => "1",
    :location => "",
    :search => "",
    :keywords => [],
    :channels => nil,
    :start_date => "today",
    :end_date => "+"
  }
  options.merge!(arg_options)
  
  options[:location] = CGI.escape(options[:location]) if options[:location]
  
  if options[:keywords].class == String
    options[:keywords] = options[:keywords].split(",")
    options[:keywords].each { |k| k.strip! }
  end
  
  channels_str = "" 
         
  if options[:channels] != nil          
    channels_a = options[:channels].collect { |channel|
      "meta:channel=#{URI.escape(URI.escape(channel, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")),Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).gsub(/\-/,"%252D")}"            
    }
  end

    = ""
    = channels_a.join("+OR+") if channels_a

   += "+AND+" unless  == ""
  if options[:start_date].class == Date
    options[:start_date] = URI.escape(options[:start_date].strftime("%m/%d/%Y")).gsub(/\//,"%2F")
  end

  if options[:end_date].class == Date
    options[:end_date] = URI.escape(options[:end_date].strftime("%m/%d/%Y")).gsub(/\//,"%2F")
  end
   += "meta:startDate:daterange:#{options[:start_date]}..#{options[:end_date]}"

  # 
  # if @asset_type_id!=nil
  #   @meta = @meta + "+AND+" if @meta!=""
  #   @meta = @meta + "inmeta:assetTypeId=#{@asset_type_id}" 
  # end
  # 
  url = "#{SEARCH_URL}/search?api_key=#{options[:api_key]}&num=#{options[:num_results]}&page=#{options[:page]}&l=#{options[:location]}&f=#{options[:facet]}&v=#{options[:view]}&r=#{options[:radius]}&s=#{options[:sort]}&k=#{options[:keywords].join("+")}&m=#{}"
  puts url
  url
end

.search(data = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/services/search.rb', line 47

def self.search(data=nil)
  searchurl         = URI.parse(construct_url(data))
  req               = Net::HTTP::Get.new(searchurl.path)
  http              = Net::HTTP.new(searchurl.host, searchurl.port)
  http.read_timeout = DEFAULT_TIMEOUT

  res = http.start { |http|
    http.get("#{searchurl.path}?#{searchurl.query}")
  }
  
  if res.code == '200'
    parsed_json = JSON.parse(res.body)
    parsed_json['_results'].collect { |a| Activity.new(a) }          
  else
    raise RuntimeError, "Active Search responded with a #{res.code} for your query."
  end
end