Skip to content

Commit

Permalink
Mct/bing v7 (#144)
Browse files Browse the repository at this point in the history
* reorganize FormattedQuery classes
* reorganize bing response parsers
* reorganize search classes and add bing v7 support
  • Loading branch information
MothOnMars committed Dec 10, 2018
1 parent 7bb1b77 commit 9ab587e
Show file tree
Hide file tree
Showing 221 changed files with 51,631 additions and 4,712 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (>= 2.0, < 4.0)
capybara-screenshot (1.0.21)
capybara-screenshot (1.0.22)
capybara (>= 1.0, < 4)
launchy
chunky_png (1.3.10)
Expand Down Expand Up @@ -419,7 +419,7 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mimemagic (0.3.2)
mini_mime (1.0.0)
mini_mime (1.0.1)
mini_portile2 (2.3.0)
minitest (5.11.3)
mobile-fu (1.4.0)
Expand Down Expand Up @@ -471,7 +471,7 @@ GEM
pry (~> 0.10)
pry-rails (0.3.6)
pry (>= 0.10.4)
public_suffix (3.0.2)
public_suffix (3.0.3)
rack (1.6.11)
rack-accept (0.4.5)
rack (>= 0.4)
Expand Down Expand Up @@ -734,7 +734,7 @@ GEM
will_paginate (3.1.6)
will_paginate-bootstrap (1.0.1)
will_paginate (>= 3.0.3)
xpath (3.1.0)
xpath (3.2.0)
nokogiri (~> 1.8)
yajl-ruby (1.3.1)
yui-compressor (0.12.0)
Expand Down Expand Up @@ -877,4 +877,4 @@ DEPENDENCIES
yui-compressor (~> 0.12.0)

BUNDLED WITH
1.16.2
1.17.1
2 changes: 1 addition & 1 deletion app/controllers/api/v2/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def docs

def affiliate_docs_search_class
case @search_options.site.search_engine
when 'BingV6'
when %r{BingV\d+}
ApiBingDocsSearch
when 'Google'
ApiGoogleDocsSearch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AzureFormattedQuery < FormattedQuery
class BingFormattedQuery < FormattedQuery
DEFAULT_DOMAIN_SCOPE = 'site:gov OR site:mil'.freeze

attr_reader :scope_ids
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class BingV6ImageResponseParser < BingV6ResponseParser
class BingImageResponseParser < BingResponseParser
def results
@results ||= bing_response_body.value.map { |v| individual_result(v) }
end
Expand All @@ -9,36 +9,35 @@ def total

private

def default_bing_response_parts
super.merge({
next_offset: nil,
total_estimated_matches: 0,
value: [],
})
end

def individual_result(bing_result)
Hashie::Mash::Rash.new({
title: bing_result.name,
url: bing_result.host_page_url,
media_url: bing_result.content_url,
display_url: bing_result.host_page_display_url,
media_url: bing_result.content_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: {
url: bing_result.thumbnail_url,
content_type: nil,
file_size: nil,
content_type: bing_result.content_type,
width: bing_result.thumbnail.width,
height: bing_result.thumbnail.height,
file_size: bing_result.file_size,
},
})
end

def default_bing_response_parts
{
_type: nil,
next_offset: nil,
total_estimated_matches: 0,
value: [],
}
end

def spelling_suggestion
nil
def next_offset
bing_response_body.next_offset || super
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class BingV5ResponseParser
class BingResponseParser
attr_reader :engine
attr_reader :bing_response

Expand Down Expand Up @@ -29,8 +29,21 @@ def bing_response_body
@bing_response_body ||= Hashie::Mash.new(bing_response.body.reverse_merge(default_bing_response_parts))
end

def default_bing_response_parts
{
_type: nil,
errors: [{ message: nil }],
query_context: { },
status_code: 200,
}
end

def response_error_message
"received status code #{bing_response_body.status_code} - #{bing_response_body.message}" if bing_response_body.status_code != 200
if bing_response_body.status_code != 200
"received status code #{bing_response_body.status_code} - #{bing_response_body.message}"
elsif bing_response_body._type == 'ErrorResponse'
bing_response_body.errors.first.message
end
end

def start_record
Expand All @@ -42,7 +55,7 @@ def end_record
end

def spelling_suggestion
bing_response_body.query_context.altered_query
nil
end

def next_offset
Expand Down
82 changes: 82 additions & 0 deletions app/engines/bing_search.rb
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
82 changes: 0 additions & 82 deletions app/engines/bing_v5_engine.rb

This file was deleted.

7 changes: 7 additions & 0 deletions app/engines/bing_v5_hosted_subscription_key.rb
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
4 changes: 3 additions & 1 deletion app/engines/bing_v5_image_engine.rb
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
45 changes: 6 additions & 39 deletions app/engines/bing_v5_image_response_parser.rb
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
Loading

0 comments on commit 9ab587e

Please sign in to comment.