bing-search 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +4 -0
- data/README.md +11 -4
- data/lib/bing-search.rb +21 -20
- data/lib/bing-search/client.rb +21 -25
- data/lib/bing-search/util.rb +22 -0
- data/lib/bing-search/version.rb +1 -1
- metadata +5 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83ee14e6e62c2b189475c2ec3170c42a52cb2396
|
4
|
+
data.tar.gz: 5c130b278bb5ba002ab1a41a1c7dc229d7610afb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cad7e20d0aae7166625ae49039a15ac68fc8c8f606285a74a70145d3013fa3b046b8d8b57b459fa397cbdd43d2c7a41e45d69f9bf3f01d09f3686ea3f0a512ed
|
7
|
+
data.tar.gz: b837f50a97b484ec36a90c89f74588f54de220ee1b7c4bb4e7b8c72337088706af8b7f030fd0b02a42cba74dc6273b6da2ac5f3702597d769737cb422e424336
|
data/.yardopts
ADDED
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
A Ruby client for the [Bing Search API](http://datamarket.azure.com/dataset/bing/search).
|
4
4
|
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/bing-search.svg)](http://badge.fury.io/rb/bing-search)
|
6
|
+
[![Build Status](https://travis-ci.org/jonahb/bing-search.svg?branch=master)](https://travis-ci.org/jonahb/bing-search)
|
7
|
+
|
5
8
|
## Getting Started
|
6
9
|
|
7
10
|
### Installation
|
@@ -14,6 +17,10 @@ gem install bing-search
|
|
14
17
|
|
15
18
|
Sign up for the [Bing Search API](https://datamarket.azure.com/dataset/bing/search) or the [Web-Only Bing Search API](https://datamarket.azure.com/dataset/bing/searchweb) at the Microsoft Azure Marketplace. Then retrieve your Account Key from the [My Account](https://datamarket.azure.com/account) section of the marketplace and provide it as shown below.
|
16
19
|
|
20
|
+
### Documentation
|
21
|
+
|
22
|
+
This README provides an overview of bing-search. Full documentation is available at [rubydoc.info](http://www.rubydoc.info/gems/bing-search).
|
23
|
+
|
17
24
|
## Basics
|
18
25
|
|
19
26
|
To use bing-search, first supply your Account Key:
|
@@ -95,7 +102,7 @@ To retrieve multiple result types at once, use {BingSearch.composite}:
|
|
95
102
|
result = BingSearch.composite('Majorana', [:web, :image, :news])
|
96
103
|
```
|
97
104
|
|
98
|
-
The result is a {BingSearch::
|
105
|
+
The result is a {BingSearch::CompositeSearchResult} ...
|
99
106
|
|
100
107
|
```ruby
|
101
108
|
result.class # => BingSearch::CompositeResult
|
@@ -115,7 +122,7 @@ All of the single-type search options are supported in composite searches, thoug
|
|
115
122
|
BingSearch.composite 'Fermi', [:image, :video], image_filters: [:small], video_filters: [:short]
|
116
123
|
```
|
117
124
|
|
118
|
-
Composite searches also give you access more data about the search including the total number of results in the Bing index and whether Bing corrected apparent errors in the query text:
|
125
|
+
Composite searches also give you access to more data about the search including the total number of results in the Bing index and whether Bing corrected apparent errors in the query text:
|
119
126
|
|
120
127
|
```ruby
|
121
128
|
result = BingSearch.composite('Feyman', [:web, :image, :news])
|
@@ -148,7 +155,7 @@ end
|
|
148
155
|
Or to override global settings:
|
149
156
|
|
150
157
|
```ruby
|
151
|
-
client = BingSearch::Client.new(
|
158
|
+
client = BingSearch::Client.new(account_key: 'hzy9+Y6...', web_only: true)
|
152
159
|
```
|
153
160
|
|
154
161
|
## Tests
|
@@ -156,7 +163,7 @@ client = BingSearch::Client.new(access_key: 'hzy9+Y6...', web_only: true)
|
|
156
163
|
To run the tests:
|
157
164
|
|
158
165
|
1. Sign up for both the standard and web-only APIs
|
159
|
-
2. Set the environment variable BING\_SEARCH\
|
166
|
+
2. Set the environment variable BING\_SEARCH\_ACCOUNT\_KEY to your Account Key
|
160
167
|
3. `rake`
|
161
168
|
|
162
169
|
## Contributing
|
data/lib/bing-search.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
%w{
|
2
|
-
client
|
3
2
|
enums
|
3
|
+
client
|
4
4
|
errors
|
5
5
|
models
|
6
|
+
util
|
6
7
|
version
|
7
8
|
}.each do |file|
|
8
|
-
require
|
9
|
+
require "bing-search/#{file}"
|
9
10
|
end
|
10
11
|
|
11
12
|
module BingSearch
|
@@ -13,81 +14,81 @@ module BingSearch
|
|
13
14
|
HIGHLIGHT_DELIMITER = "\u{e001}"
|
14
15
|
|
15
16
|
class << self
|
16
|
-
# An
|
17
|
-
# attribute once instead of instantiating each {Client} with an
|
17
|
+
# An Account Key obtained from the Azure Marketplace. You can set this
|
18
|
+
# attribute once instead of instantiating each {Client} with an Account Key.
|
18
19
|
# @return [String]
|
19
|
-
attr_accessor :
|
20
|
+
attr_accessor :account_key
|
20
21
|
|
21
22
|
# Whether to use the less expensive web-only API
|
22
23
|
# @return [Boolean]
|
23
24
|
attr_accessor :web_only
|
24
25
|
|
25
26
|
# Convenience method that creates a {Client} and searches for web pages.
|
26
|
-
# Takes the same arguments as {Client#web}. Set {
|
27
|
+
# Takes the same arguments as {Client#web}. Set {account_key} before calling.
|
27
28
|
# @return (see Client#web)
|
28
29
|
# @see Client#web
|
29
30
|
#
|
30
31
|
def web(*args)
|
31
|
-
Client.new.web
|
32
|
+
Client.new.web(*args)
|
32
33
|
end
|
33
34
|
|
34
35
|
# Convenience method that creates a {Client} and searches for images. Takes
|
35
|
-
# the same arguments as {Client#image}. Set {
|
36
|
+
# the same arguments as {Client#image}. Set {account_key} before calling.
|
36
37
|
# @return (see Client#image)
|
37
38
|
# @see Client#image
|
38
39
|
#
|
39
40
|
def image(*args)
|
40
|
-
Client.new.image
|
41
|
+
Client.new.image(*args)
|
41
42
|
end
|
42
43
|
|
43
44
|
# Convenience method that creates a {Client} and searches for videos. Takes
|
44
|
-
# the same arguments as {Client#video}. Set {
|
45
|
+
# the same arguments as {Client#video}. Set {account_key} before calling.
|
45
46
|
# @return (see Client#video)
|
46
47
|
# @see Client#video
|
47
48
|
#
|
48
49
|
def video(*args)
|
49
|
-
Client.new.video
|
50
|
+
Client.new.video(*args)
|
50
51
|
end
|
51
52
|
|
52
53
|
# Convenience method that creates a {Client} and searches for news. Takes
|
53
|
-
# the same arguments as {Client#news}. Set {
|
54
|
+
# the same arguments as {Client#news}. Set {account_key} before calling.
|
54
55
|
# @return (see Client#news)
|
55
56
|
# @see Client#news
|
56
57
|
#
|
57
58
|
def news(*args)
|
58
|
-
Client.new.news
|
59
|
+
Client.new.news(*args)
|
59
60
|
end
|
60
61
|
|
61
62
|
# Convenience method that creates a {Client} and searches for related
|
62
|
-
# queries. Takes the same arguments as {Client#related_search}. Set {
|
63
|
+
# queries. Takes the same arguments as {Client#related_search}. Set {account_key}
|
63
64
|
# before calling.
|
64
65
|
# @return (see Client#related_search)
|
65
66
|
# @see Client#related_search
|
66
67
|
#
|
67
68
|
def related_search(*args)
|
68
|
-
Client.new.related_search
|
69
|
+
Client.new.related_search(*args)
|
69
70
|
end
|
70
71
|
alias_method :related, :related_search
|
71
72
|
|
72
73
|
# Convenience method that creates a {Client} and corrects spelling in the
|
73
74
|
# query text. Takes the same arguments as {Client#related_search}. Set
|
74
|
-
# {
|
75
|
+
# {account_key} before calling.
|
75
76
|
# @return (see Client#spelling_suggestions)
|
76
77
|
# @see Client#spelling_suggestions
|
77
78
|
#
|
78
79
|
def spelling_suggestions(*args)
|
79
|
-
Client.new.spelling_suggestions
|
80
|
+
Client.new.spelling_suggestions(*args)
|
80
81
|
end
|
81
82
|
alias_method :spelling, :spelling_suggestions
|
82
83
|
|
83
|
-
# Convenience method that creates a {Client} and searches multiple sources
|
84
|
-
# Takes the same arguments as {Client#related_search}. Set {
|
84
|
+
# Convenience method that creates a {Client} and searches multiple sources.
|
85
|
+
# Takes the same arguments as {Client#related_search}. Set {account_key} before
|
85
86
|
# calling.
|
86
87
|
# @return (see Client#composite)
|
87
88
|
# @see Client#composite
|
88
89
|
#
|
89
90
|
def composite(*args)
|
90
|
-
Client.new.composite
|
91
|
+
Client.new.composite(*args)
|
91
92
|
end
|
92
93
|
end
|
93
94
|
end
|
data/lib/bing-search/client.rb
CHANGED
@@ -2,17 +2,12 @@ require 'date'
|
|
2
2
|
require 'json'
|
3
3
|
require 'net/http'
|
4
4
|
require 'uri'
|
5
|
-
require 'active_support/core_ext/hash/slice'
|
6
|
-
require 'active_support/core_ext/string/inflections'
|
7
|
-
require_relative 'errors'
|
8
|
-
require_relative 'models'
|
9
|
-
require_relative 'enums'
|
10
5
|
|
11
6
|
module BingSearch
|
12
7
|
class Client
|
13
|
-
# The
|
8
|
+
# The Account Key obtained from the Azure Marketplace
|
14
9
|
# @return [String]
|
15
|
-
attr_reader :
|
10
|
+
attr_reader :account_key
|
16
11
|
|
17
12
|
# Whether to use the less expensive web-only API
|
18
13
|
# @return [Boolean]
|
@@ -21,19 +16,20 @@ module BingSearch
|
|
21
16
|
|
22
17
|
# @!group Constructors
|
23
18
|
|
24
|
-
# @param [String, nil]
|
25
|
-
# An
|
26
|
-
# {BingSearch.
|
19
|
+
# @param [String, nil] account_key
|
20
|
+
# An Account Key obtained from the Azure Marketplace. If nil,
|
21
|
+
# {BingSearch.account_key} is assumed.
|
27
22
|
# @param [Boolean, nil] web_only
|
28
23
|
# Whether to use the less expensive web-only API. If nil,
|
29
24
|
# {BingSearch.web_only} is assumed.
|
30
25
|
#
|
31
|
-
def initialize(
|
32
|
-
@
|
26
|
+
def initialize(account_key: nil, web_only: nil)
|
27
|
+
@session = nil
|
28
|
+
@account_key = account_key || BingSearch.account_key
|
33
29
|
@web_only = web_only.nil? ? BingSearch.web_only : web_only
|
34
30
|
|
35
|
-
unless @
|
36
|
-
raise ArgumentError, "Pass an
|
31
|
+
unless @account_key
|
32
|
+
raise ArgumentError, "Pass an Account Key or set BingSearch.account_key"
|
37
33
|
end
|
38
34
|
end
|
39
35
|
|
@@ -113,7 +109,7 @@ module BingSearch
|
|
113
109
|
# @param [String] query
|
114
110
|
# The query text; supports the
|
115
111
|
# {http://msdn.microsoft.com/en-us/library/ff795667.aspx Bing Query Language}
|
116
|
-
# @param [Hash
|
112
|
+
# @param [Hash{Symbol => Object}] opts
|
117
113
|
# @option opts [Integer] :limit
|
118
114
|
# The maximum number of results to return
|
119
115
|
# @option opts [Integer] :offset
|
@@ -335,15 +331,15 @@ module BingSearch
|
|
335
331
|
# e.g. 'Web'
|
336
332
|
# @param [String] query
|
337
333
|
# The query text
|
338
|
-
# @param [Hash
|
334
|
+
# @param [Hash{Symbol => Object}] opts
|
339
335
|
# The options hash provided by the caller
|
340
336
|
# @param [Array<Symbol>] passthrough_opts
|
341
337
|
# Keys of the options to copy to the params hash
|
342
|
-
# @param [Hash
|
338
|
+
# @param [Hash{Symbol => Module}] enum_opt_to_module
|
343
339
|
# Maps an enum option key to the module containing the enum's values.
|
344
340
|
# Used to translate symbols to enum values. E.g. maps +:web_file_type+
|
345
341
|
# to {FileType}.
|
346
|
-
# @param [Hash
|
342
|
+
# @param [Hash{Symbol => Object}] params
|
347
343
|
# Parameters for the invocation
|
348
344
|
# @return [Object]
|
349
345
|
# @raise [ServiceError]
|
@@ -363,7 +359,7 @@ module BingSearch
|
|
363
359
|
end
|
364
360
|
|
365
361
|
params = params.
|
366
|
-
merge(
|
362
|
+
merge(Util.slice_hash(opts, *GENERAL_PASSTHROUGH_OPTS, *passthrough_opts)).
|
367
363
|
merge(query: query, offset: offset, options: options, format: :JSON).
|
368
364
|
delete_if { |_, v| v.nil? || (v.is_a?(Array) && v.empty?) }
|
369
365
|
|
@@ -373,7 +369,7 @@ module BingSearch
|
|
373
369
|
|
374
370
|
response = in_session do |session|
|
375
371
|
request = Net::HTTP::Get.new("#{base_path}/#{operation}?#{query}")
|
376
|
-
request.basic_auth(
|
372
|
+
request.basic_auth(account_key, account_key)
|
377
373
|
session.request request
|
378
374
|
end
|
379
375
|
|
@@ -423,18 +419,18 @@ module BingSearch
|
|
423
419
|
# The module does not contain a constant corresponing to the symbol
|
424
420
|
#
|
425
421
|
def enum_from_symbol(symbol, module_)
|
426
|
-
[symbol.to_s
|
422
|
+
[Util.camelcase(symbol.to_s), symbol.to_s.upcase].each do |const|
|
427
423
|
return module_.const_get(const) if module_.const_defined?(const)
|
428
424
|
end
|
429
425
|
raise ArgumentError, "#{module_} does not contain a constant corresponding to #{symbol}"
|
430
426
|
end
|
431
427
|
|
432
|
-
# @param [Hash
|
433
|
-
# @return [Hash
|
428
|
+
# @param [Hash{Symbol => Object}] params
|
429
|
+
# @return [Hash{String => Object}]
|
434
430
|
#
|
435
431
|
def replace_param_names(params, replacements)
|
436
432
|
params.each_with_object(Hash.new) do |(key, value), hash|
|
437
|
-
key = replacements[key] || GENERAL_PARAM_NAME_REPLACEMENTS[key] || key.to_s
|
433
|
+
key = replacements[key] || GENERAL_PARAM_NAME_REPLACEMENTS[key] || Util.camelcase(key.to_s)
|
438
434
|
hash[key] = value
|
439
435
|
end
|
440
436
|
end
|
@@ -515,7 +511,7 @@ module BingSearch
|
|
515
511
|
|
516
512
|
for key, value in raw
|
517
513
|
next if key == '__metadata'
|
518
|
-
attr =
|
514
|
+
attr = Util.underscore(key).to_sym
|
519
515
|
model.set attr, parse(value, attr_to_type[attr])
|
520
516
|
end
|
521
517
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module BingSearch
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Util
|
5
|
+
class << self
|
6
|
+
def underscore(string)
|
7
|
+
string.gsub(/([^A-Z])([A-Z])/, '\1_\2').downcase
|
8
|
+
end
|
9
|
+
|
10
|
+
def camelcase(string)
|
11
|
+
string.gsub(/(?:^|_)([a-z])/) { $1.upcase }
|
12
|
+
end
|
13
|
+
|
14
|
+
def slice_hash(hash, *keys)
|
15
|
+
keys.each_with_object(Hash.new) do |key, result|
|
16
|
+
result[key] = hash[key] if hash.has_key?(key)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/bing-search/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bing-search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonah Burke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activesupport
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 4.2.0
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 4.2.0
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,6 +59,7 @@ executables: []
|
|
73
59
|
extensions: []
|
74
60
|
extra_rdoc_files: []
|
75
61
|
files:
|
62
|
+
- ".yardopts"
|
76
63
|
- LICENSE.txt
|
77
64
|
- README.md
|
78
65
|
- lib/bing-search.rb
|
@@ -80,6 +67,7 @@ files:
|
|
80
67
|
- lib/bing-search/enums.rb
|
81
68
|
- lib/bing-search/errors.rb
|
82
69
|
- lib/bing-search/models.rb
|
70
|
+
- lib/bing-search/util.rb
|
83
71
|
- lib/bing-search/version.rb
|
84
72
|
homepage: http://github.com/jonahb/bing-search
|
85
73
|
licenses:
|
@@ -101,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
89
|
version: '0'
|
102
90
|
requirements: []
|
103
91
|
rubyforge_project:
|
104
|
-
rubygems_version: 2.
|
92
|
+
rubygems_version: 2.4.5
|
105
93
|
signing_key:
|
106
94
|
specification_version: 4
|
107
95
|
summary: A Ruby client for the Bing Search API
|