Class: Active::Services::Search

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

Constant Summary collapse

SEARCH_URL =
"http://search.active.com"
DEFAULT_TIMEOUT =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Search

Returns a new instance of Search.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/services/search.rb', line 41

def initialize(data={})
  self.asset_type_id    = data[:asset_type_id]
  self.url              = data[:url]
  self.api_key          = data[:api_key] || ""
  self.location         = data[:location]
  self.zips             = data[:zips] || []
  self.channels         = data[:channels] || []
  self.split_media_type = data[:split_media_type] || []
  self.keywords         = data[:keywords] || []
  self.radius           = data[:radius] || nil
  self.limit            = data[:limit] || "10"
  self.sort             = data[:sort] || Sort.DATE_ASC
  self.page             = data[:page] || "1"
  self.offset           = data[:offset] || "0"
  self.view             = data[:view] || "json"
  self.facet            = data[:facet] || Facet.ACTIVITIES       
  self.num_results      = data[:num_results] || "10"
  self.search           = data[:search] || ""
  self.start_date       = data[:start_date] || "today"
  self.end_date         = data[:end_date] || "+"        
  self.asset_ids        = data[:asset_ids] || []
  self.asset_id         = data[:asset_id] || ""
  self.latitude         = data[:latitude]
  self.longitude        = data[:longitude]
  self.dma              = data[:dma]
  self.city             = data[:city]
  self.state            = data[:state]
  self.country          = data[:country]
  self.bounding_box     = data[:bounding_box]
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



32
33
34
# File 'lib/services/search.rb', line 32

def api_key
  @api_key
end

#asset_idsObject

Returns the value of attribute asset_ids.



32
33
34
# File 'lib/services/search.rb', line 32

def asset_ids
  @asset_ids
end

#asset_type_idObject

Returns the value of attribute asset_type_id.



32
33
34
# File 'lib/services/search.rb', line 32

def asset_type_id
  @asset_type_id
end

#bounding_boxObject

Returns the value of attribute bounding_box.



32
33
34
# File 'lib/services/search.rb', line 32

def bounding_box
  @bounding_box
end

#channelsObject

Returns the value of attribute channels.



32
33
34
# File 'lib/services/search.rb', line 32

def channels
  @channels
end

#cityObject

Returns the value of attribute city.



32
33
34
# File 'lib/services/search.rb', line 32

def city
  @city
end

#countryObject

Returns the value of attribute country.



32
33
34
# File 'lib/services/search.rb', line 32

def country
  @country
end

#dmaObject

Returns the value of attribute dma.



32
33
34
# File 'lib/services/search.rb', line 32

def dma
  @dma
end

#end_dateObject

Returns the value of attribute end_date.



32
33
34
# File 'lib/services/search.rb', line 32

def end_date
  @end_date
end

#end_pointObject (readonly)

Returns the value of attribute end_point.



36
37
38
# File 'lib/services/search.rb', line 36

def end_point
  @end_point
end

#endIndexObject (readonly)

Returns the value of attribute endIndex.



36
37
38
# File 'lib/services/search.rb', line 36

def endIndex
  @endIndex
end

#facetObject

Returns the value of attribute facet.



32
33
34
# File 'lib/services/search.rb', line 32

def facet
  @facet
end

#keywordsObject

Returns the value of attribute keywords.



32
33
34
# File 'lib/services/search.rb', line 32

def keywords
  @keywords
end

#latitudeObject

Returns the value of attribute latitude.



32
33
34
# File 'lib/services/search.rb', line 32

def latitude
  @latitude
end

#limitObject

Returns the value of attribute limit.



32
33
34
# File 'lib/services/search.rb', line 32

def limit
  @limit
end

#locationObject

Returns the value of attribute location.



32
33
34
# File 'lib/services/search.rb', line 32

def location
  @location
end

#longitudeObject

Returns the value of attribute longitude.



32
33
34
# File 'lib/services/search.rb', line 32

def longitude
  @longitude
end

#metaObject (readonly)

Returns the value of attribute meta.



36
37
38
# File 'lib/services/search.rb', line 36

def meta
  @meta
end

#num_resultsObject

Returns the value of attribute num_results.



32
33
34
# File 'lib/services/search.rb', line 32

def num_results
  @num_results
end

#numberOfResultsObject (readonly)

Returns the value of attribute numberOfResults.



36
37
38
# File 'lib/services/search.rb', line 36

def numberOfResults
  @numberOfResults
end

#offsetObject

Returns the value of attribute offset.



32
33
34
# File 'lib/services/search.rb', line 32

def offset
  @offset
end

#pageObject

Returns the value of attribute page.



32
33
34
# File 'lib/services/search.rb', line 32

def page
  @page
end

#pageSizeObject (readonly)

Returns the value of attribute pageSize.



36
37
38
# File 'lib/services/search.rb', line 36

def pageSize
  @pageSize
end

#radiusObject

Returns the value of attribute radius.



32
33
34
# File 'lib/services/search.rb', line 32

def radius
  @radius
end

#resultsObject (readonly)

Returns the value of attribute results.



36
37
38
# File 'lib/services/search.rb', line 36

def results
  @results
end

#searchObject

TODO the seach method should not raise an error.



244
245
246
# File 'lib/services/search.rb', line 244

def search
  @search
end

#searchTimeObject (readonly)

Returns the value of attribute searchTime.



36
37
38
# File 'lib/services/search.rb', line 36

def searchTime
  @searchTime
end

#sortObject

Returns the value of attribute sort.



32
33
34
# File 'lib/services/search.rb', line 32

def sort
  @sort
end

#split_media_typeObject

Returns the value of attribute split_media_type.



32
33
34
# File 'lib/services/search.rb', line 32

def split_media_type
  @split_media_type
end

#start_dateObject

Returns the value of attribute start_date.



32
33
34
# File 'lib/services/search.rb', line 32

def start_date
  @start_date
end

#stateObject

Returns the value of attribute state.



32
33
34
# File 'lib/services/search.rb', line 32

def state
  @state
end

#urlObject

Returns the value of attribute url.



32
33
34
# File 'lib/services/search.rb', line 32

def url
  @url
end

#viewObject

Returns the value of attribute view.



32
33
34
# File 'lib/services/search.rb', line 32

def view
  @view
end

Class Method Details

.return_cached(key) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/services/search.rb', line 332

def self.return_cached key
  if Active.CACHE
    begin
      cached_version = Active.CACHE.get(key)
    rescue Exception => e
      return nil
    end
    
    
    
    if cached_version
      puts "Active Search [CACHE] #{key}"
      return cached_version 
    end
  end
  nil
end

.search(data = nil) ⇒ Object

examples

Keywords =

Keywords can be set like this Search.new(=> “Dog,Cat,Cow”) Search.new(=> %w(Dog Cat Cow)) Search.new(=> [“Dog”,“Cat”,“Cow”])

Channels and MediaType =

Search.new(=> [:running], :split_media_type => [“5k”]) for a list of channels and splitMediaTypes please see developer.active.com/docs/Activecom_Search_API_Reference

Location =

The location will be set in this order and will override other values. For example is you set a zip code and a location only the zip will be used.

1 Look for zip codes

Search.search( {:zips => "92121, 92078, 92114"} )
Search.search( {:zips => %w(92121, 92078, 92114)} )
Search.search( {:zips => [92121, 92078, 92114]} )

2 Look for lat lng

Search.search( {:latitude=>"37.785895", :longitude=>"-122.40638"} )

3 Look for a formatted string “San Diego, CA, US”

Search.search( {:location = "San Diego, CA, US"} )

4 Look for a DMA

Search.new({:dma=>"San Francisco - Oakland - San Jose"})

How to look at the results =

developer.active.com/docs/Activecom_Search_API_Reference returns an array of results and query info



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/services/search.rb', line 319

def self.search(data=nil)
  search      = Search.new(data)
  search_hash = Digest::MD5.hexdigest(search.end_point)
  puts "search_hash #{search_hash}"
  cache = Search.return_cached(search_hash)
  if cache != nil
    return cache
  else
    search.search
    return search  
  end
end

Instance Method Details

#[]Object



351
352
353
# File 'lib/services/search.rb', line 351

def []
  @results
end

#asset_id=(value) ⇒ Object



123
124
125
126
# File 'lib/services/search.rb', line 123

def asset_id=(value)
  return if value.empty?
  @asset_ids<<value
end

#zipsObject



84
85
86
# File 'lib/services/search.rb', line 84

def zips
  @zips
end

#zips=(value) ⇒ Object

Example Search.search( => “92121, 92078, 92114” ) or Search.new( => [92121, 92078, 92114] )



76
77
78
79
80
81
82
# File 'lib/services/search.rb', line 76

def zips=(value)
  if value.class == String
    @zips = value.split(",").each { |k| k.strip! }
  else
    @zips = value
  end        
end