binger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bing.rb +58 -0
- metadata +45 -0
data/lib/bing.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
#API DOC http://www.bing.com/developers/s/APIBasics.html#_Using_the_API
|
6
|
+
|
7
|
+
class Bing
|
8
|
+
|
9
|
+
attr_accessor :api_key
|
10
|
+
|
11
|
+
#format = json || xml => http://api.bing.net/xml.aspx
|
12
|
+
ENDPOINT = "http://api.bing.net/json.aspx"
|
13
|
+
|
14
|
+
# Create a new instance of Bing with your Bing api key
|
15
|
+
#
|
16
|
+
# Example:
|
17
|
+
#
|
18
|
+
# >> require 'bing'
|
19
|
+
# => true
|
20
|
+
# >> bing = Bing.new('your-api-key')
|
21
|
+
# => #<Bing:0x1af10 @api_key="your-api-key">
|
22
|
+
#
|
23
|
+
# Arguments:
|
24
|
+
#
|
25
|
+
# @param (String) api_key - you specify a key (Application ID) and such key will be used to authenticate the request.
|
26
|
+
def initialize(api_key)
|
27
|
+
@api_key = api_key
|
28
|
+
end
|
29
|
+
|
30
|
+
# Sending a request HTTP GET
|
31
|
+
#
|
32
|
+
# A request to the HTTP endpoint consists of a HTTP GET request to the approriate URI. There are two URLs, one for XML results and one for JSON results.
|
33
|
+
# These are http://api.bing.com/xml.aspx and and http://api.bing.com/json.aspx, respectively.
|
34
|
+
#
|
35
|
+
# Example:
|
36
|
+
#
|
37
|
+
# >> bing.search(query="MooTools",sources="web")
|
38
|
+
# => Anatomy of a result set
|
39
|
+
# => The results returned for a request differ from SourceType to SourceType, but in every case they include a header and a results body
|
40
|
+
# => The document element is always SearchResponse, with a Query child element that contains the query used to produce the results.
|
41
|
+
# => {"SearchResponse"=>{"Version"=>"2.2", "Query"=>{"SearchTerms"=>"MooTools"}, "Web"=>{"Total"=>85100, "Offset"=>0, "Results"=>[{}.{}...]}
|
42
|
+
|
43
|
+
# Arguments:
|
44
|
+
# @param (String) q - The query parameter is the textof the query you want the API to execute. example: query=mootools
|
45
|
+
# @param (String) sources - The sources parameter is one or more values indicating the SourceType or SourceTypes from which you want to request results.
|
46
|
+
# The "web" Source type returns a set of results from the WEB SourceType, "Image" searches for images on the WEB, "News" searches news stories
|
47
|
+
# @param (Hash) optional - page/offset
|
48
|
+
def search(q="",sources="",optional=nil)
|
49
|
+
|
50
|
+
params = {"query"=>q, "sources"=>sources}
|
51
|
+
params.merge!(optional) if optional
|
52
|
+
params = URI.encode_www_form( params )
|
53
|
+
url = URI("#{ENDPOINT}?Appid=#{api_key}&#{params}")
|
54
|
+
|
55
|
+
return JSON.parse(Net::HTTP.get(url))
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: binger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adrian Statescu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple hello world for Bing
|
15
|
+
email: mergesortv@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/bing.rb
|
21
|
+
homepage: http://rubygems.org/gems/rbing
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Bing!
|
45
|
+
test_files: []
|