Class: Sfn::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_url, loader) ⇒ Search

Returns a new instance of Search.



4
5
6
7
8
# File 'lib/satisfaction/search.rb', line 4

def initialize(root_url, loader)
  @root_url = root_url ? root_url : "http://api.getsatisfaction.com"
  @loader = loader
  @results = {}
end

Instance Attribute Details

#loaderObject (readonly)

Returns the value of attribute loader.



2
3
4
# File 'lib/satisfaction/search.rb', line 2

def loader
  @loader
end

#resultsObject (readonly)

Returns the value of attribute results.



2
3
4
# File 'lib/satisfaction/search.rb', line 2

def results
  @results
end

#root_urlObject (readonly)

Returns the value of attribute root_url.



2
3
4
# File 'lib/satisfaction/search.rb', line 2

def root_url
  @root_url
end

Instance Method Details

#for_likely_matches_to(name, entities = %w(companies products))) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/satisfaction/search.rb', line 10

def for_likely_matches_to(name, entities = %w(companies products))
  @results = entities.inject({}) {|hash, entity|
    query_string = "#{@root_url}/#{entity}.json?q=#{name}"
    #result = Net::HTTP.get_response(URI.parse(query_string)).body
    answer = @loader.get(query_string)
    if answer[0] == :ok
      result = answer[1]
      hash.merge({entity => JSON.parse(result)['data']})
    else
      raise "Search service not available at the moment, please try again later."
    end
  }
end