live-search 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +22 -0
- data/Rakefile +75 -0
- data/lib/live-search.rb +27 -0
- data/lib/live-search/request.rb +57 -0
- data/lib/live-search/response.rb +29 -0
- data/lib/live-search/result.rb +24 -0
- data/lib/live-search/version.rb +5 -0
- data/spec/live-search_spec.rb +20 -0
- data/spec/spec_helper.rb +11 -0
- metadata +81 -0
data/README
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# LiveAPI.application_id = "YOU_APPLICATION_ID"
|
2
|
+
# search = LiveAPI::Search.web("seo")
|
3
|
+
# results = search.results #=> 50 happy results :)
|
4
|
+
#
|
5
|
+
# results.each do |result|
|
6
|
+
# puts result.title
|
7
|
+
# puts result.description
|
8
|
+
# puts result.url
|
9
|
+
# puts result.display_url
|
10
|
+
# end
|
11
|
+
|
12
|
+
You can also provide options in the second parameter. Example:
|
13
|
+
|
14
|
+
# LiveAPI::Search.web("seo", :Count => 25, :Offset => 26) #=> Returns 25 results instead of our default 50 and starts on the 2nd page
|
15
|
+
|
16
|
+
See LiveAPI::Search::Request for query options.
|
17
|
+
|
18
|
+
For more information on the API, visit: http://dev.live.com/livesearch/
|
19
|
+
|
20
|
+
=== Contact
|
21
|
+
|
22
|
+
info@rubyskills.com if you're looking for some extra help
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/testtask'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
|
8
|
+
require 'lib/live-search/version'
|
9
|
+
|
10
|
+
NAME = "live-search"
|
11
|
+
AUTHOR = "Lance Carlson"
|
12
|
+
EMAIL = "info@rubyskills.com"
|
13
|
+
HOMEPAGE = "http://www.rubyskills.com"
|
14
|
+
SUMMARY = "Live Search API"
|
15
|
+
DESCRIPTION = "Ruby gem for the Live Search API"
|
16
|
+
|
17
|
+
dist_dirs = [ "lib", "spec" ]
|
18
|
+
|
19
|
+
spec = Gem::Specification.new do |s|
|
20
|
+
s.name = NAME
|
21
|
+
s.version = LiveAPI::Search::VERSION
|
22
|
+
s.platform = Gem::Platform::RUBY
|
23
|
+
s.summary = SUMMARY
|
24
|
+
s.description = DESCRIPTION
|
25
|
+
s.author = AUTHOR
|
26
|
+
s.email = EMAIL
|
27
|
+
s.homepage = HOMEPAGE
|
28
|
+
s.has_rdoc = true
|
29
|
+
|
30
|
+
s.add_dependency('rspec')
|
31
|
+
s.add_dependency('rake')
|
32
|
+
|
33
|
+
s.files = [ "Rakefile", "README" ]
|
34
|
+
dist_dirs.each do |dir|
|
35
|
+
s.files = s.files + Dir.glob("#{dir}/**/*")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Rake::GemPackageTask.new(spec) do |p|
|
40
|
+
p.gem_spec = spec
|
41
|
+
end
|
42
|
+
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
rdoc.rdoc_dir = 'doc'
|
45
|
+
rdoc.title = 'Live Search API'
|
46
|
+
rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
|
47
|
+
rdoc.options << '--charset' << 'utf-8'
|
48
|
+
rdoc.rdoc_files.include('README')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'Run :package and install the resulting .gem'
|
53
|
+
task :install => :package do
|
54
|
+
sh %{sudo gem install pkg/#{NAME}-#{LiveAPI::Search::VERSION} --no-rdoc --no-ri}
|
55
|
+
end
|
56
|
+
|
57
|
+
desc 'Run :clean and uninstall the .gem'
|
58
|
+
task :uninstall => [:clean] do
|
59
|
+
sh %{sudo gem uninstall #{NAME}}
|
60
|
+
end
|
61
|
+
|
62
|
+
desc "Run all specs"
|
63
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
64
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
65
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
66
|
+
end
|
67
|
+
|
68
|
+
desc "Run all specs and generate an rcov report"
|
69
|
+
Spec::Rake::SpecTask.new('spec:rcov') do |t|
|
70
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
71
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
72
|
+
t.rcov = true
|
73
|
+
t.rcov_dir = 'coverage'
|
74
|
+
t.rcov_opts = ['--exclude', 'gems', '--exclude', 'spec']
|
75
|
+
end
|
data/lib/live-search.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module LiveAPI
|
5
|
+
class << self
|
6
|
+
attr_accessor :application_id
|
7
|
+
end
|
8
|
+
|
9
|
+
module Search
|
10
|
+
class << self
|
11
|
+
def web(query, options = {})
|
12
|
+
options[:Query] = clean_query(query) unless query.nil?
|
13
|
+
Request.new(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def clean_query(query)
|
19
|
+
query.gsub(" ", "+")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
%w(request response result version).each do |file|
|
26
|
+
require File.join(File.dirname(__FILE__), "live-search", file)
|
27
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module LiveAPI
|
2
|
+
module Search
|
3
|
+
class Request
|
4
|
+
API_DOMAIN = "http://api.search.live.net"
|
5
|
+
FORMAT = "json"
|
6
|
+
SOURCE = "web"
|
7
|
+
DEFAULT_OFFSET = 0
|
8
|
+
DEFAULT_COUNT = 50
|
9
|
+
DEFAULT_MARKET = "en-US"
|
10
|
+
|
11
|
+
# Converts a hash to a query string
|
12
|
+
def self.hash_to_query(hash)
|
13
|
+
hash.map {|key, value| "#{key}=#{value}"}.join("&")
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(options)
|
17
|
+
@options = options
|
18
|
+
@options[:AppId] = LiveAPI.application_id
|
19
|
+
@options[:Sources] = SOURCE
|
20
|
+
@options[:Count] = @options[:Count] ||= DEFAULT_COUNT
|
21
|
+
@options[:Offset] = @options[:Offset] ||= DEFAULT_OFFSET
|
22
|
+
@options[:Market] = @options[:Market] ||= DEFAULT_MARKET
|
23
|
+
|
24
|
+
raise "Application ID is needed" if @options[:AppId].empty?
|
25
|
+
raise "A query is needed" unless @options.has_key?(:Query)
|
26
|
+
end
|
27
|
+
|
28
|
+
# The API URL call
|
29
|
+
def path
|
30
|
+
@options["Web.Count"] = @options[:Count]
|
31
|
+
@options["Web.Offset"] = @options[:Offset]
|
32
|
+
@options.delete(:Count)
|
33
|
+
@options.delete(:Offset)
|
34
|
+
"#{API_DOMAIN}/#{FORMAT}.aspx?#{query_string}"
|
35
|
+
end
|
36
|
+
|
37
|
+
# The response object of the request
|
38
|
+
def response
|
39
|
+
@response = @response ||= LiveAPI::Search::Response.new(response_body)
|
40
|
+
end
|
41
|
+
|
42
|
+
# The response body of the request
|
43
|
+
def response_body
|
44
|
+
open(path).readlines.join
|
45
|
+
end
|
46
|
+
|
47
|
+
# The results from the response object
|
48
|
+
def results
|
49
|
+
response.results
|
50
|
+
end
|
51
|
+
|
52
|
+
def query_string
|
53
|
+
self.class.hash_to_query(@options)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module LiveAPI
|
2
|
+
module Search
|
3
|
+
class Response
|
4
|
+
def initialize(response_body)
|
5
|
+
@body = response_body
|
6
|
+
end
|
7
|
+
|
8
|
+
# The number of URLs returned. This may be lower than the number of results requested if there were fewer total results available.
|
9
|
+
def total
|
10
|
+
self.to_json["SearchResponse"]["Web"]["Total"].to_i
|
11
|
+
end
|
12
|
+
|
13
|
+
# The result objects returned from the request
|
14
|
+
def results
|
15
|
+
begin
|
16
|
+
self.to_json["SearchResponse"]["Web"]["Results"].map do |result_hash|
|
17
|
+
LiveAPI::Search::Result.new(result_hash)
|
18
|
+
end
|
19
|
+
rescue
|
20
|
+
self.to_json["SearchResponse"]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_json
|
25
|
+
@json = @json ||= JSON.parse(@body)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module LiveAPI
|
2
|
+
module Search
|
3
|
+
class Result
|
4
|
+
def initialize(hash)
|
5
|
+
@hash = hash
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(name)
|
9
|
+
@hash[camelize(name)]
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
# Ganked from rails
|
15
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
16
|
+
if first_letter_in_uppercase
|
17
|
+
lower_case_and_underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
|
18
|
+
else
|
19
|
+
lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
describe LiveAPI::Search do
|
4
|
+
before do
|
5
|
+
@search_term = "search engine optimization"
|
6
|
+
LiveAPI.application_id = "797DAB5F6860C407969014FD103EA24404AD7F35" # Your app id here
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return the top 50 results for the term seo" do
|
10
|
+
search = LiveAPI::Search.web(@search_term)
|
11
|
+
results = search.results
|
12
|
+
results.length.should == 50
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should return the next 50 results for the search term seo" do
|
16
|
+
search = LiveAPI::Search.web(@search_term, :Offset => 51)
|
17
|
+
results = search.results
|
18
|
+
results.length.should == 50
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# Ensure that only the source is getting tested and not the installed gem
|
2
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
require "test/unit"
|
6
|
+
require "spec"
|
7
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "live-search")
|
8
|
+
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: live-search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lance Carlson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-15 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Ruby gem for the Live Search API
|
36
|
+
email: info@rubyskills.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- Rakefile
|
45
|
+
- README
|
46
|
+
- lib/live-search
|
47
|
+
- lib/live-search/request.rb
|
48
|
+
- lib/live-search/response.rb
|
49
|
+
- lib/live-search/result.rb
|
50
|
+
- lib/live-search/version.rb
|
51
|
+
- lib/live-search.rb
|
52
|
+
- spec/live-search_spec.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://www.rubyskills.com
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.3.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 2
|
79
|
+
summary: Live Search API
|
80
|
+
test_files: []
|
81
|
+
|