keyword_ranking 1.1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/keyword_ranking.gemspec +1 -1
- data/lib/keyword_ranking.rb +55 -61
- metadata +12 -37
data/keyword_ranking.gemspec
CHANGED
data/lib/keyword_ranking.rb
CHANGED
@@ -7,82 +7,76 @@ class Array
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
|
10
|
+
class Ranking
|
11
|
+
@@default_options = { :limit => 200, :res_per_page => 10, :supported_engines => [:bing, :google, :yahoo] } #careful, res_per_page doesn't work with more than 10 for all search engines!
|
11
12
|
|
13
|
+
attr_reader :options
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
attr_reader :options
|
17
|
-
|
18
|
-
def initialize(*options) # keyword, url, limit
|
19
|
-
self.options = options.extract_options!
|
20
|
-
end
|
21
|
-
|
22
|
-
def options=(options)
|
23
|
-
@options = @@default_options.merge(options)
|
24
|
-
@options[:url].gsub!(/(http:\/\/|https:\/\/)/, '')
|
25
|
-
@options[:url].gsub!(/^www/, '')
|
26
|
-
end
|
15
|
+
def initialize(*options) # keyword, url, limit
|
16
|
+
self.options = options.extract_options!
|
17
|
+
end
|
27
18
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
19
|
+
def options=(options)
|
20
|
+
@options = @@default_options.merge(options)
|
21
|
+
@options[:url].gsub!(/(http:\/\/|https:\/\/)/, '')
|
22
|
+
@options[:url].gsub!(/^www/, '')
|
23
|
+
end
|
33
24
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
send("from")
|
39
|
-
else
|
40
|
-
super(method, *args, &block)
|
41
|
-
end
|
42
|
-
end
|
25
|
+
def from
|
26
|
+
validate_options
|
27
|
+
Finder.find(@options)
|
28
|
+
end
|
43
29
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
30
|
+
def method_missing(method, *args, &block)
|
31
|
+
engine = @@default_options[:supported_engines].find{ |name| ("from_" << name.to_s).match /#{method}/ }
|
32
|
+
if engine
|
33
|
+
@options.merge!({:engine => engine})
|
34
|
+
send("from")
|
35
|
+
else
|
36
|
+
super(method, *args, &block)
|
48
37
|
end
|
38
|
+
end
|
49
39
|
|
40
|
+
protected
|
41
|
+
def validate_options
|
42
|
+
raise "Keyword and site parameters must be Strings" unless @options[:keyword].is_a?(String) and @options[:url].is_a?(String)
|
43
|
+
raise "Engine should be 'bing', 'google' or 'yahoo'" unless @@default_options[:supported_engines].include?(@options[:engine].to_sym)
|
50
44
|
end
|
51
45
|
|
52
|
-
|
53
|
-
|
54
|
-
def find(options) #keyword, url, limit, engine, res_per_page
|
55
|
-
options[:keyword].gsub!(/\s/, '+')
|
56
|
-
request_url, results_selector, cite_selector = case options[:engine].to_sym
|
57
|
-
when :bing
|
58
|
-
["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&first=", '#wg0 > li', 'cite']
|
59
|
-
when :google
|
60
|
-
["http://www.google.com/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
|
61
|
-
when :yahoo
|
62
|
-
["http://search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
|
63
|
-
end
|
46
|
+
end
|
64
47
|
|
65
|
-
|
48
|
+
class Finder
|
49
|
+
|
50
|
+
def self.find(options) #keyword, url, limit, engine, res_per_page
|
51
|
+
options[:keyword].gsub!(/\s/, '+')
|
52
|
+
request_url, results_selector, cite_selector = case options[:engine].to_sym
|
53
|
+
when :bing
|
54
|
+
["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&first=", '#wg0 > li', 'cite']
|
55
|
+
when :google
|
56
|
+
["http://www.google.com/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
|
57
|
+
when :yahoo
|
58
|
+
["http://search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
|
59
|
+
end
|
66
60
|
|
67
|
-
|
68
|
-
html_response = Net::HTTP.get_response(URI.parse("#{request_url}#{count}")).body
|
69
|
-
html_results = Nokogiri.parse(html_response).css(results_selector)
|
70
|
-
rank = html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(options[:url]) })
|
61
|
+
count, rank = 0, nil
|
71
62
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
break
|
77
|
-
end
|
78
|
-
count += options[:res_per_page]
|
79
|
-
}
|
63
|
+
loop {
|
64
|
+
html_response = Net::HTTP.get_response(URI.parse("#{request_url}#{count}")).body
|
65
|
+
html_results = Nokogiri.parse(html_response).css(results_selector)
|
66
|
+
rank = html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(options[:url]) })
|
80
67
|
|
81
|
-
|
68
|
+
if count > options[:limit]
|
69
|
+
break
|
70
|
+
elsif rank
|
71
|
+
rank += count
|
72
|
+
break
|
73
|
+
end
|
74
|
+
count += options[:res_per_page]
|
75
|
+
}
|
82
76
|
|
83
|
-
|
77
|
+
rank ? rank.next : nil
|
84
78
|
|
85
|
-
|
79
|
+
end
|
86
80
|
|
87
81
|
end
|
88
82
|
|
metadata
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: keyword_ranking
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease: false
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: "1.1"
|
4
|
+
version: 1.1.1
|
10
5
|
platform: ruby
|
11
6
|
authors:
|
12
7
|
- Luis Alberto Velasco
|
@@ -19,37 +14,24 @@ default_executable:
|
|
19
14
|
dependencies:
|
20
15
|
- !ruby/object:Gem::Dependency
|
21
16
|
name: nokogiri
|
22
|
-
|
23
|
-
|
24
|
-
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
20
|
requirements:
|
26
21
|
- - ">="
|
27
22
|
- !ruby/object:Gem::Version
|
28
|
-
hash: 113
|
29
|
-
segments:
|
30
|
-
- 1
|
31
|
-
- 4
|
32
|
-
- 3
|
33
|
-
- 1
|
34
23
|
version: 1.4.3.1
|
35
|
-
|
36
|
-
version_requirements: *id001
|
24
|
+
version:
|
37
25
|
- !ruby/object:Gem::Dependency
|
38
26
|
name: bundler
|
39
|
-
|
40
|
-
|
41
|
-
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
30
|
requirements:
|
43
31
|
- - ">="
|
44
32
|
- !ruby/object:Gem::Version
|
45
|
-
hash: 15
|
46
|
-
segments:
|
47
|
-
- 0
|
48
|
-
- 9
|
49
|
-
- 26
|
50
33
|
version: 0.9.26
|
51
|
-
|
52
|
-
version_requirements: *id002
|
34
|
+
version:
|
53
35
|
description: Create keyword rankings from the 3 major search engines
|
54
36
|
email: whizkas@hotmail.com
|
55
37
|
executables: []
|
@@ -86,28 +68,21 @@ rdoc_options:
|
|
86
68
|
require_paths:
|
87
69
|
- lib
|
88
70
|
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
71
|
requirements:
|
91
72
|
- - ">="
|
92
73
|
- !ruby/object:Gem::Version
|
93
|
-
hash: 3
|
94
|
-
segments:
|
95
|
-
- 0
|
96
74
|
version: "0"
|
75
|
+
version:
|
97
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
77
|
requirements:
|
100
78
|
- - ">="
|
101
79
|
- !ruby/object:Gem::Version
|
102
|
-
hash: 11
|
103
|
-
segments:
|
104
|
-
- 1
|
105
|
-
- 2
|
106
80
|
version: "1.2"
|
81
|
+
version:
|
107
82
|
requirements: []
|
108
83
|
|
109
84
|
rubyforge_project: keyword_ranking
|
110
|
-
rubygems_version: 1.3.
|
85
|
+
rubygems_version: 1.3.5
|
111
86
|
signing_key:
|
112
87
|
specification_version: 3
|
113
88
|
summary: Get keyword rankings from Google, Yahoo and Bing
|