Class: BingSearch::Client
- Inherits:
-
Object
- Object
- BingSearch::Client
- Defined in:
- lib/bing-search/client.rb
Instance Attribute Summary collapse
-
#account_key ⇒ String
readonly
The Account Key obtained from the Azure Marketplace.
-
#web_only ⇒ Boolean
readonly
Whether to use the less expensive web-only API.
Constructors collapse
-
#initialize(account_key: nil, web_only: nil) ⇒ Client
constructor
A new instance of Client.
Sessions collapse
-
.open(*args) {|client| ... } ⇒ Client
Opens a client and yields it to the given block.
-
#close ⇒ self
Closes the client.
-
#open { ... } ⇒ Object, self
Opens the client, creating a new TCP connection.
-
#open? ⇒ Boolean
Whether the client is open.
Searching collapse
-
#composite(query, sources, opts = {}) ⇒ CompositeSearchResult
Searches multiple sources.
-
#image(query, opts = {}) ⇒ Array<ImageResult>
Searches for images.
-
#news(query, opts = {}) ⇒ Array<NewsResult>
Searches for news.
-
#related_search(query, opts = {}) ⇒ Array<RelatedSearchResult>
(also: #related)
Searches for related queries.
-
#spelling_suggestions(query, opts = {}) ⇒ Array<SpellingSuggestionsResult>
(also: #spelling)
Corrects spelling in the query text.
-
#video(query, opts = {}) ⇒ Array<VideoResult>
Searches for videos.
-
#web(query, opts = {}) ⇒ Array<WebResult>
Searches for web pages.
Constructor Details
#initialize(account_key: nil, web_only: nil) ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 32 33 34 |
# File 'lib/bing-search/client.rb', line 26 def initialize(account_key: nil, web_only: nil) @session = nil @account_key = account_key || BingSearch.account_key @web_only = web_only.nil? ? BingSearch.web_only : web_only unless @account_key raise ArgumentError, "Pass an Account Key or set BingSearch.account_key" end end |
Instance Attribute Details
#account_key ⇒ String (readonly)
The Account Key obtained from the Azure Marketplace
10 11 12 |
# File 'lib/bing-search/client.rb', line 10 def account_key @account_key end |
#web_only ⇒ Boolean (readonly)
Whether to use the less expensive web-only API
14 15 16 |
# File 'lib/bing-search/client.rb', line 14 def web_only @web_only end |
Class Method Details
.open(*args) {|client| ... } ⇒ Client
Opens a client and yields it to the given block. Takes the same arguments as #initialize.
46 47 48 49 50 51 |
# File 'lib/bing-search/client.rb', line 46 def self.open(*args) raise "Block required" unless block_given? client = new(*args) client.open { yield client } client end |
Instance Method Details
#close ⇒ self
Closes the client. Must be called after #open is called without a block.
91 92 93 94 95 |
# File 'lib/bing-search/client.rb', line 91 def close @session.finish if open? @session = nil self end |
#composite(query, sources, opts = {}) ⇒ CompositeSearchResult
Searches multiple sources. At most 15 news results are returned by a composite query regardless of the :limit
option.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/bing-search/client.rb', line 268 def composite(query, sources, opts = {}) results = invoke('Composite', query, opts, passthrough_opts: %i( web_file_type video_filters video_sort news_category news_location_override news_sort ), enum_opt_to_module: { web_file_type: FileType, video_filters: VideoFilter, video_sort: VideoSort, news_category: NewsCategory, news_sort: NewsSort }, param_name_replacements: { video_sort: 'VideoSortBy', news_sort: 'NewsSortBy' }, params: { sources: sources.collect { |source| enum_value(source, Source) }, web_search_options: (opts, :web_), image_filters: image_filters_from_opts(opts, :image_) } ) results.first end |
#image(query, opts = {}) ⇒ Array<ImageResult>
Searches for images
169 170 171 172 173 174 175 |
# File 'lib/bing-search/client.rb', line 169 def image(query, opts = {}) invoke 'Image', query, opts, param_name_replacements: {filters: 'ImageFilters'}, params: {filters: image_filters_from_opts(opts)} end |
#news(query, opts = {}) ⇒ Array<NewsResult>
Searches for news
206 207 208 209 210 211 212 213 |
# File 'lib/bing-search/client.rb', line 206 def news(query, opts = {}) invoke 'News', query, opts, passthrough_opts: %i(category location_override sort), enum_opt_to_module: {category: NewsCategory, sort: NewsSort}, param_name_replacements: {category: 'NewsCategory', location_override: 'NewsLocationOverride', sort: 'NewsSortBy'} end |
#open { ... } ⇒ Object, self
Opens the client, creating a new TCP connection.
If a block is given, yields to the block, closes the client when the block returns, and returns the return value of the block. If a block is not given, returns self and leaves the client open, relying on the caller to close the client with #close.
Note that opening and closing the client is only required if you want to make several calls under one TCP connection. Otherwise, you can simply call the search methods (#web, #image, etc.), which call #open for you if necessary.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bing-search/client.rb', line 72 def open raise "Already open" if open? @session = Net::HTTP.new(HOST, Net::HTTP.https_default_port) @session.use_ssl = true begin @session.start block_given? ? yield : self ensure close if block_given? end end |
#open? ⇒ Boolean
Whether the client is open
101 102 103 |
# File 'lib/bing-search/client.rb', line 101 def open? @session && @session.started? end |
#related_search(query, opts = {}) ⇒ Array<RelatedSearchResult> Also known as:
Searches for related queries
219 220 221 |
# File 'lib/bing-search/client.rb', line 219 def (query, opts = {}) invoke 'RelatedSearch', query, opts end |
#spelling_suggestions(query, opts = {}) ⇒ Array<SpellingSuggestionsResult> Also known as: spelling
Corrects spelling in the query text
228 229 230 |
# File 'lib/bing-search/client.rb', line 228 def spelling_suggestions(query, opts = {}) invoke 'SpellingSuggestions', query, opts end |
#video(query, opts = {}) ⇒ Array<VideoResult>
Searches for videos
184 185 186 187 188 189 190 191 |
# File 'lib/bing-search/client.rb', line 184 def video(query, opts = {}) invoke 'Video', query, opts, passthrough_opts: %i(filters sort), enum_opt_to_module: {filters: VideoFilter, sort: VideoSort}, param_name_replacements: {filters: 'VideoFilters', sort: 'VideoSortBy'} end |
#web(query, opts = {}) ⇒ Array<WebResult>
Searches for web pages
149 150 151 152 153 154 155 156 157 |
# File 'lib/bing-search/client.rb', line 149 def web(query, opts = {}) invoke 'Web', query, opts, passthrough_opts: %i(file_type), enum_opt_to_module: {file_type: FileType}, param_name_replacements: {file_type: 'WebFileType'}, params: {web_search_options: (opts)} end |