forked from alexreisner/google_custom_search
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,22 @@ | |||
## | |||
# Single search result. | |||
# | |||
class GoogleCustomSearch::Result | |||
attr_reader :url, :title, :description, :labels | |||
|
|||
def self.parse(res_r) | |||
res_r = [res_r] if res_r.is_a?(Hash) | |||
|
|||
res_r.collect do |r| | |||
self.new(r['U'], # url | |||
r['T'].try(:sub, / \[[^\]]*\]$/, ''), # title | |||
r['S'].try(:gsub, '<br>', ''), # desciption | |||
r['Label'].is_a?(String) ? [r['Label']] : r['Label']) # array of labels | |||
end | |||
end | |||
|
|||
def initialize(url, title, description, labels) | |||
@url, @title, @description, @labels = url, title, description, labels | |||
end | |||
end | |||
|
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,36 @@ | |||
## | |||
# Simple class to hold a collection of search result data. | |||
# | |||
class GoogleCustomSearch::ResultSet | |||
attr_reader :total, :pages, :suggestion, :labels | |||
|
|||
def self.create(xml_hash) | |||
self.new(xml_hash['RES']['M'].to_i, | |||
parse_results(xml_hash['RES']['R']), | |||
spelling = xml_hash['SPELLING'] ? spelling['SUGGESTION'] : nil, | |||
parse_labels(xml_hash)) | |||
end | |||
|
|||
def self.create_empty | |||
self.new(0, [], nil, {}) | |||
end | |||
|
|||
def self.parse_results(res_r) | |||
GoogleCustomSearch::Result.parse(res_r) | |||
end | |||
|
|||
def self.parse_labels(xml_hash) | |||
return {} unless context = xml_hash['Context'] and facets = context['Facet'] | |||
facets.inject({}) do |h, facet_item| | |||
facet_item['FacetItem'].each do |element| | |||
h[element['label']] = element['anchor_text'] | |||
end | |||
h | |||
end | |||
end | |||
|
|||
def initialize(total, pages, suggestion, labels) | |||
@total, @pages, @suggestion, @labels = total, pages, suggestion, labels | |||
end | |||
end | |||
|
This file contains 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
This file contains 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