-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* reorganize FormattedQuery classes * reorganize bing response parsers * reorganize search classes and add bing v7 support
- Loading branch information
1 parent
7bb1b77
commit 9ab587e
Showing
221 changed files
with
51,631 additions
and
4,712 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
2 changes: 1 addition & 1 deletion
2
app/engines/azure_formatted_query.rb → app/engines/bing_formatted_query.rb
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
class BingSearch < SearchEngine | ||
API_HOST = 'https://api.cognitive.microsoft.com'.freeze | ||
VALID_ADULT_FILTERS = %w{off moderate strict} | ||
CACHE_LIFETIME = BING_CACHE_DURATION | ||
|
||
attr_reader :options | ||
|
||
class_attribute :api_endpoint | ||
class_attribute :api_cache_namespace | ||
class_attribute :response_parser_class | ||
|
||
def initialize(options = { }) | ||
super | ||
@options = options | ||
@api_connection = self.class.api_connection | ||
Rails.logger.error("CACHE_LIFETIME: #{CACHE_LIFETIME}") | ||
end | ||
|
||
def execute_query | ||
api_connection.connection.headers['Ocp-Apim-Subscription-Key'] = subscription_key if subscription_key | ||
super | ||
end | ||
|
||
def params | ||
{ | ||
offset: offset, | ||
count: count, | ||
mkt: market, | ||
q: options[:query], | ||
safeSearch: safe_search, | ||
responseFilter: 'WebPages', | ||
textDecorations: !!options[:enable_highlighting], | ||
}.merge(ADDITIONAL_BING_PARAMS) | ||
end | ||
|
||
protected | ||
|
||
def self.api_host | ||
API_HOST | ||
end | ||
|
||
def parse_search_engine_response(bing_response) | ||
parser = response_parser_class.new(self, bing_response) | ||
parser.parsed_response | ||
end | ||
|
||
def market | ||
Language.bing_market_for_code(language) | ||
end | ||
|
||
def language | ||
options[:language] | ||
end | ||
|
||
def offset | ||
options[:offset] || 0 | ||
end | ||
|
||
def count | ||
options[:limit] || 20 | ||
end | ||
|
||
def safe_search | ||
filter_index = get_filter_index(options[:filter]) | ||
VALID_ADULT_FILTERS[filter_index] | ||
end | ||
|
||
def subscription_key | ||
options[:password] || hosted_subscription_key | ||
end | ||
|
||
def hosted_subscription_key | ||
nil | ||
end | ||
|
||
class << self | ||
def api_connection | ||
@api_connection ||= { } | ||
@api_connection[self] ||= CachedSearchApiConnection.new(api_cache_namespace, api_host, CACHE_LIFETIME) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module BingV5HostedSubscriptionKey | ||
BING_V5_SUBSCRIPTION_KEY = Rails.application.secrets.hosted_azure['v5_account_key'].freeze | ||
|
||
def hosted_subscription_key | ||
BING_V5_SUBSCRIPTION_KEY | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
class BingV5ImageEngine < BingV5Engine | ||
class BingV5ImageEngine < BingSearch | ||
API_ENDPOINT = '/bing/v5.0/images/search'.freeze | ||
include BingV5HostedSubscriptionKey | ||
|
||
self.api_endpoint = API_ENDPOINT | ||
self.api_cache_namespace = 'bing_v5_api' | ||
self.response_parser_class = BingV5ImageResponseParser | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,13 @@ | ||
class BingV5ImageResponseParser < BingV5ResponseParser | ||
def results | ||
@results ||= bing_response_body.value.map { |v| individual_result(v) } | ||
end | ||
|
||
def total | ||
bing_response_body.total_estimated_matches | ||
end | ||
|
||
def next_offset | ||
offset = super | ||
offset + bing_response_body.next_offset_add_count if offset | ||
end | ||
|
||
class BingV5ImageResponseParser < BingImageResponseParser | ||
private | ||
|
||
def individual_result(bing_result) | ||
Hashie::Mash.new({ | ||
title: bing_result.name, | ||
super.merge({ | ||
source_url: bing_result.host_page_url, | ||
media_url: bing_result.content_url, | ||
display_url: bing_result.host_page_display_url, | ||
content_type: "image/#{bing_result.encoding_format}", | ||
file_size: bing_result.content_size.to_i, | ||
width: bing_result.width, | ||
height: bing_result.height, | ||
thumbnail: { | ||
}).tap do |h| | ||
h[:thumbnail].merge!({ | ||
media_url: bing_result.thumbnail_url, | ||
width: bing_result.thumbnail.width, | ||
height: bing_result.thumbnail.height, | ||
}, | ||
}) | ||
end | ||
|
||
def default_bing_response_parts | ||
{ | ||
next_offset_add_count: 0, | ||
query_context: { | ||
altered_query: nil, | ||
}, | ||
status_code: 200, | ||
total_estimated_matches: 0, | ||
value: [], | ||
} | ||
}) | ||
end | ||
end | ||
end |
Oops, something went wrong.