color_parser 0.0.2 → 0.1.0
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/.gitignore +2 -0
- data/Guardfile +9 -0
- data/README.md +22 -22
- data/Rakefile +18 -6
- data/color_parser.gemspec +9 -1
- data/lib/color_parser/color.rb +154 -0
- data/lib/color_parser/errors.rb +3 -0
- data/lib/color_parser/request.rb +21 -0
- data/lib/color_parser/stylesheet.rb +4 -11
- data/lib/color_parser/version.rb +1 -1
- data/lib/color_parser.rb +4 -89
- data/{test/color_parser_test.rb → spec/color_parser_spec.rb} +19 -14
- data/spec/color_spec.rb +10 -0
- data/{test → spec}/fixtures/css/absolute.html +0 -0
- data/{test → spec}/fixtures/css/inline.html +0 -0
- data/{test → spec}/fixtures/css/inline_import.html +0 -0
- data/{test → spec}/fixtures/css/invalid.html +0 -0
- data/{test → spec}/fixtures/css/relative.html +0 -0
- data/{test → spec}/fixtures/css/relative_root.html +0 -0
- data/{test → spec}/fixtures/css/stylesheets/colors.css +0 -0
- data/{test → spec}/fixtures/css/stylesheets/fonts.css +0 -0
- data/{test → spec}/fixtures/css/stylesheets/print.css +0 -0
- data/{test → spec}/fixtures/css/stylesheets/screen.css +0 -0
- data/{test → spec}/fixtures/css_color/frequency.html +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/color_styles.css +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/css_elements.css +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/frequency.css +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/imported_selectors.css +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/properties.css +0 -0
- data/{test → spec}/fixtures/css_images/images/apple.png +0 -0
- data/{test → spec}/fixtures/css_images/images/cantaloupe.png +0 -0
- data/{test → spec}/fixtures/css_images/images/kiwi.jpg +0 -0
- data/{test → spec}/fixtures/css_images/images/mango.png +0 -0
- data/{test → spec}/fixtures/css_images/images/pineapple.png +0 -0
- data/{test → spec}/fixtures/css_images/paths.html +0 -0
- data/{test → spec}/fixtures/css_images/stylesheets/import_paths.css +0 -0
- data/{test → spec}/fixtures/css_images/stylesheets/paths.css +0 -0
- data/{test → spec}/fixtures/css_images/stylesheets/quotes.css +0 -0
- data/{test → spec}/fixtures/css_import/index.html +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/borders.css +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/colors.css +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/fonts.css +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/ie.css +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/images.css +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/master.css +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/print.css +0 -0
- data/{test → spec}/fixtures/css_import/stylesheets/screen.css +0 -0
- data/{test → spec}/fixtures/inline_images/absolute.html +0 -0
- data/{test → spec}/fixtures/inline_images/images/apple.png +0 -0
- data/{test → spec}/fixtures/inline_images/images/kiwi.jpg +0 -0
- data/{test → spec}/fixtures/inline_images/relative.html +0 -0
- data/{test → spec}/fixtures/inline_images/relative_root.html +0 -0
- data/spec/image_spec.rb +33 -0
- data/{test/page_test.rb → spec/page_spec.rb} +57 -57
- data/spec/request_spec.rb +12 -0
- data/spec/spec_helper.rb +10 -0
- data/{test/test_request.rb → spec/stubs/fake_request.rb} +2 -2
- data/spec/stylesheet_spec.rb +235 -0
- data/spec/version_spec.rb +9 -0
- metadata +186 -100
- data/test/image_test.rb +0 -27
- data/test/stylesheet_test.rb +0 -257
- data/test/test_helper.rb +0 -6
- data/test/version_test.rb +0 -7
data/.gitignore
CHANGED
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -1,14 +1,32 @@
|
|
1
1
|
## ColorParser
|
2
2
|
|
3
|
-
The ColorParser gem provides a simple way to parse the colors from an
|
3
|
+
The ColorParser gem provides a simple way to parse the colors from an HTML page.
|
4
|
+
It scans both the HTML and CSS to find all colors and sort them by frequency
|
5
|
+
used.
|
4
6
|
|
5
7
|
## Example
|
6
8
|
|
9
|
+
Get colors on a given webpage
|
10
|
+
|
7
11
|
```ruby
|
8
|
-
page = ColorParser::Page.new("http://
|
12
|
+
page = ColorParser::Page.new("http://google.com/")
|
9
13
|
colors = page.colors
|
10
14
|
```
|
11
15
|
|
16
|
+
Get stylesheets on a given webpage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
page = ColorParser::Page.new("http://google.com/")
|
20
|
+
stylesheets = page.stylesheets
|
21
|
+
```
|
22
|
+
|
23
|
+
Get images on a given webpage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
page = ColorParser::Page.new("http://google.com/")
|
27
|
+
images = page.images
|
28
|
+
```
|
29
|
+
|
12
30
|
## Installation
|
13
31
|
|
14
32
|
```
|
@@ -20,24 +38,6 @@ gem "color_parser"
|
|
20
38
|
|
21
39
|
## LICENSE
|
22
40
|
|
23
|
-
(
|
24
|
-
|
25
|
-
Copyright © 2012 [Derek DeVries](https://github.com/devrieda/)
|
26
|
-
|
27
|
-
Permission is hereby granted, free of charge, to any person obtaining a
|
28
|
-
copy of this software and associated documentation files (the "Software"),
|
29
|
-
to deal in the Software without restriction, including without
|
30
|
-
limitation the rights to use, copy, modify, merge, publish, distribute,
|
31
|
-
sublicense, and/or sell copies of the Software, and to permit persons
|
32
|
-
to whom the Software is furnished to do so, subject to the following conditions:
|
33
|
-
|
34
|
-
The above copyright notice and this permission notice shall be included
|
35
|
-
in all copies or substantial portions of the Software.
|
41
|
+
Copyright (c) 2012 Derek DeVries
|
36
42
|
|
37
|
-
|
38
|
-
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
39
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
40
|
-
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
41
|
-
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
42
|
-
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
43
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
43
|
+
Released under the [MIT License](http://www.opensource.org/licenses/MIT)
|
data/Rakefile
CHANGED
@@ -1,10 +1,22 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require "bundler/gem_tasks"
|
3
|
-
require 'rake/testtask'
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new
|
7
|
+
|
8
|
+
namespace :spec do
|
9
|
+
desc 'Run unit specs only'
|
10
|
+
RSpec::Core::RakeTask.new(:unit) do |task|
|
11
|
+
task.pattern = 'spec'
|
12
|
+
task.rspec_opts = '--tag "~live"'
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Run acceptance specs only'
|
16
|
+
RSpec::Core::RakeTask.new(:acceptance) do |task|
|
17
|
+
task.pattern = 'spec'
|
18
|
+
task.rspec_opts = '--tag "live"'
|
19
|
+
end
|
9
20
|
end
|
10
|
-
|
21
|
+
|
22
|
+
task :default => :spec
|
data/color_parser.gemspec
CHANGED
@@ -6,7 +6,7 @@ require 'color_parser/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "color_parser"
|
8
8
|
gem.version = ColorParser::VERSION
|
9
|
-
gem.summary = %q{
|
9
|
+
gem.summary = %q{Finds colors on a given webpage}
|
10
10
|
gem.description = gem.summary
|
11
11
|
|
12
12
|
gem.required_ruby_version = '>= 1.9.3'
|
@@ -21,6 +21,14 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
22
22
|
gem.require_paths = ["lib"]
|
23
23
|
|
24
|
+
gem.add_runtime_dependency("curb", "~> 0.8")
|
24
25
|
gem.add_runtime_dependency("nokogiri", "~> 1.5")
|
26
|
+
|
25
27
|
gem.add_development_dependency("rake")
|
28
|
+
gem.add_development_dependency("rspec", "~> 2.9")
|
29
|
+
|
30
|
+
# guard
|
31
|
+
gem.add_development_dependency("guard", "~> 1.7")
|
32
|
+
gem.add_development_dependency("guard-rspec", "~> 2.5")
|
33
|
+
gem.add_development_dependency("rb-fsevent", "~> 0.9")
|
26
34
|
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module ColorParser
|
2
|
+
class Color
|
3
|
+
|
4
|
+
def self.text_colors
|
5
|
+
{ black: "000000",
|
6
|
+
navy: "000080",
|
7
|
+
darkblue: "00008b",
|
8
|
+
mediumblue: "0000cd",
|
9
|
+
blue: "0000ff",
|
10
|
+
darkgreen: "006400",
|
11
|
+
green: "008000",
|
12
|
+
teal: "008080",
|
13
|
+
darkcyan: "008b8b",
|
14
|
+
deepskyblue: "00bfff",
|
15
|
+
darkturquoise: "00ced1",
|
16
|
+
mediumspringgreen: "00fa9a",
|
17
|
+
lime: "00ff00",
|
18
|
+
springgreen: "00ff7f",
|
19
|
+
aqua: "00ffff",
|
20
|
+
cyan: "00ffff",
|
21
|
+
midnightblue: "191970",
|
22
|
+
dodgerblue: "1e90ff",
|
23
|
+
lightseagreen: "20b2aa",
|
24
|
+
forestgreen: "228b22",
|
25
|
+
seagreen: "2e8b57",
|
26
|
+
darkslategray: "2f4f4f",
|
27
|
+
darkslategrey: "2f4f4f",
|
28
|
+
limegreen: "32cd32",
|
29
|
+
mediumseagreen: "3cb371",
|
30
|
+
turquoise: "40e0d0",
|
31
|
+
royalblue: "4169e1",
|
32
|
+
steelblue: "4682b4",
|
33
|
+
darkslateblue: "483d8b",
|
34
|
+
mediumturquoise: "48d1cc",
|
35
|
+
indigo: "4b0082",
|
36
|
+
darkolivegreen: "556b2f",
|
37
|
+
cadetblue: "5f9ea0",
|
38
|
+
cornflowerblue: "6495ed",
|
39
|
+
mediumaquamarine: "66cdaa",
|
40
|
+
dimgray: "696969",
|
41
|
+
dimgrey: "696969",
|
42
|
+
slateblue: "6a5acd",
|
43
|
+
olivedrab: "6b8e23",
|
44
|
+
slategray: "708090",
|
45
|
+
slategrey: "708090",
|
46
|
+
lightslategray: "778899",
|
47
|
+
lightslategrey: "778899",
|
48
|
+
mediumslateblue: "7b68ee",
|
49
|
+
lawngreen: "7cfc00",
|
50
|
+
chartreuse: "7fff00",
|
51
|
+
aquamarine: "7fffd4",
|
52
|
+
maroon: "800000",
|
53
|
+
purple: "800080",
|
54
|
+
olive: "808000",
|
55
|
+
gray: "808080",
|
56
|
+
grey: "808080",
|
57
|
+
skyblue: "87ceeb",
|
58
|
+
lightskyblue: "87cefa",
|
59
|
+
blueviolet: "8a2be2",
|
60
|
+
darkred: "8b0000",
|
61
|
+
darkmagenta: "8b008b",
|
62
|
+
saddlebrown: "8b4513",
|
63
|
+
darkseagreen: "8fbc8f",
|
64
|
+
lightgreen: "90ee90",
|
65
|
+
mediumpurple: "9370db",
|
66
|
+
darkviolet: "9400d3",
|
67
|
+
palegreen: "98fb98",
|
68
|
+
darkorchid: "9932cc",
|
69
|
+
yellowgreen: "9acd32",
|
70
|
+
sienna: "a0522d",
|
71
|
+
brown: "a52a2a",
|
72
|
+
darkgray: "a9a9a9",
|
73
|
+
darkgrey: "a9a9a9",
|
74
|
+
lightblue: "add8e6",
|
75
|
+
greenyellow: "adff2f",
|
76
|
+
paleturquoise: "afeeee",
|
77
|
+
lightsteelblue: "b0c4de",
|
78
|
+
powderblue: "b0e0e6",
|
79
|
+
firebrick: "b22222",
|
80
|
+
darkgoldenrod: "b8860b",
|
81
|
+
mediumorchid: "ba55d3",
|
82
|
+
rosybrown: "bc8f8f",
|
83
|
+
darkkhaki: "bdb76b",
|
84
|
+
silver: "c0c0c0",
|
85
|
+
mediumvioletred: "c71585",
|
86
|
+
indianred: "cd5c5c",
|
87
|
+
peru: "cd853f",
|
88
|
+
chocolate: "d2691e",
|
89
|
+
tan: "d2b48c",
|
90
|
+
lightgray: "d3d3d3",
|
91
|
+
lightgrey: "d3d3d3",
|
92
|
+
thistle: "d8bfd8",
|
93
|
+
orchid: "da70d6",
|
94
|
+
goldenrod: "daa520",
|
95
|
+
palevioletred: "db7093",
|
96
|
+
crimson: "dc143c",
|
97
|
+
gainsboro: "dcdcdc",
|
98
|
+
plum: "dda0dd",
|
99
|
+
burlywood: "deb887",
|
100
|
+
lightcyan: "e0ffff",
|
101
|
+
lavender: "e6e6fa",
|
102
|
+
darksalmon: "e9967a",
|
103
|
+
violet: "ee82ee",
|
104
|
+
palegoldenrod: "eee8aa",
|
105
|
+
lightcoral: "f08080",
|
106
|
+
khaki: "f0e68c",
|
107
|
+
aliceblue: "f0f8ff",
|
108
|
+
honeydew: "f0fff0",
|
109
|
+
azure: "f0ffff",
|
110
|
+
sandybrown: "f4a460",
|
111
|
+
wheat: "f5deb3",
|
112
|
+
beige: "f5f5dc",
|
113
|
+
whitesmoke: "f5f5f5",
|
114
|
+
mintcream: "f5fffa",
|
115
|
+
ghostwhite: "f8f8ff",
|
116
|
+
salmon: "fa8072",
|
117
|
+
antiquewhite: "faebd7",
|
118
|
+
linen: "faf0e6",
|
119
|
+
lightgoldenrodyellow: "fafad2",
|
120
|
+
oldlace: "fdf5e6",
|
121
|
+
red: "ff0000",
|
122
|
+
fuchsia: "ff00ff",
|
123
|
+
magenta: "ff00ff",
|
124
|
+
deeppink: "ff1493",
|
125
|
+
orangered: "ff4500",
|
126
|
+
tomato: "ff6347",
|
127
|
+
hotpink: "ff69b4",
|
128
|
+
coral: "ff7f50",
|
129
|
+
darkorange: "ff8c00",
|
130
|
+
lightsalmon: "ffa07a",
|
131
|
+
orange: "ffa500",
|
132
|
+
lightpink: "ffb6c1",
|
133
|
+
pink: "ffc0cb",
|
134
|
+
gold: "ffd700",
|
135
|
+
peachpuff: "ffdab9",
|
136
|
+
navajowhite: "ffdead",
|
137
|
+
moccasin: "ffe4b5",
|
138
|
+
bisque: "ffe4c4",
|
139
|
+
mistyrose: "ffe4e1",
|
140
|
+
blanchedalmond: "ffebcd",
|
141
|
+
papayawhip: "ffefd5",
|
142
|
+
lavenderblush: "fff0f5",
|
143
|
+
seashell: "fff5ee",
|
144
|
+
cornsilk: "fff8dc",
|
145
|
+
lemonchiffon: "fffacd",
|
146
|
+
floralwhite: "fffaf0",
|
147
|
+
snow: "fffafa",
|
148
|
+
yellow: "ffff00",
|
149
|
+
lightyellow: "ffffe0",
|
150
|
+
ivory: "fffff0",
|
151
|
+
white: "ffffff" }
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Request
|
2
|
+
def get(url)
|
3
|
+
curl = Curl::Easy.perform(url) do |curl|
|
4
|
+
curl.headers["User-Agent"] = user_agent
|
5
|
+
curl.follow_location = true
|
6
|
+
end
|
7
|
+
|
8
|
+
curl.body_str
|
9
|
+
|
10
|
+
rescue ColorParser::Error
|
11
|
+
raise
|
12
|
+
|
13
|
+
# re-raise external library errors in our namespace
|
14
|
+
rescue => error
|
15
|
+
raise ColorParser::Error.new("#{error.class}: #{error.message}")
|
16
|
+
end
|
17
|
+
|
18
|
+
def user_agent
|
19
|
+
"Ruby/ColorParser Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7"
|
20
|
+
end
|
21
|
+
end
|
@@ -1,15 +1,6 @@
|
|
1
1
|
module ColorParser
|
2
2
|
# a set of css selectors
|
3
3
|
class Stylesheet
|
4
|
-
TEXT_COLORS = {
|
5
|
-
aqua: "00ffff", black: "000000", blue: "0000ff",
|
6
|
-
fuchsia: "ff00ff", gray: "808080", green: "008000",
|
7
|
-
lime: "00ff00", maroon: "800000", navy: "000080",
|
8
|
-
olive: "808000", purple: "800080", red: "ff0000",
|
9
|
-
silver: "c0c0c0", teal: "008080", white: "ffffff",
|
10
|
-
yellow: "ffff00"
|
11
|
-
}
|
12
|
-
|
13
4
|
attr_reader :url, :type, :host, :path, :query, :text
|
14
5
|
|
15
6
|
def initialize(options)
|
@@ -153,6 +144,8 @@ module ColorParser
|
|
153
144
|
|
154
145
|
def parse_colors(property_list)
|
155
146
|
colors = {}
|
147
|
+
|
148
|
+
text_colors = ColorParser::Color.text_colors.map {|k,v| k }.join("|")
|
156
149
|
|
157
150
|
property_list.each do |key, value|
|
158
151
|
# hex
|
@@ -164,7 +157,7 @@ module ColorParser
|
|
164
157
|
rgb_to_hex(matches[1])
|
165
158
|
|
166
159
|
# textual
|
167
|
-
elsif matches = value.match(/(#{
|
160
|
+
elsif matches = value.match(/(#{text_colors})/)
|
168
161
|
text_to_hex(matches[1])
|
169
162
|
end
|
170
163
|
|
@@ -185,7 +178,7 @@ module ColorParser
|
|
185
178
|
|
186
179
|
# find hex for textual color
|
187
180
|
def text_to_hex(color)
|
188
|
-
|
181
|
+
ColorParser::Color.text_colors[color.intern]
|
189
182
|
end
|
190
183
|
|
191
184
|
# convert 3 digit hex to 6
|
data/lib/color_parser/version.rb
CHANGED
data/lib/color_parser.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
require 'net/http'
|
2
1
|
require 'uri'
|
2
|
+
require 'curb'
|
3
3
|
require 'nokogiri'
|
4
4
|
|
5
|
+
require 'color_parser/errors'
|
5
6
|
require 'color_parser/version'
|
7
|
+
require 'color_parser/request'
|
6
8
|
require 'color_parser/page'
|
7
9
|
require 'color_parser/stylesheet'
|
10
|
+
require 'color_parser/color'
|
8
11
|
require 'color_parser/image'
|
9
12
|
|
10
13
|
module ColorParser
|
11
|
-
|
12
14
|
# Build url of an asset based on the relative/absolute url
|
13
15
|
def self.parse_asset(doc_url, asset_url)
|
14
16
|
doc_host, doc_path, doc_query = self.parse_url(doc_url)
|
@@ -52,91 +54,4 @@ module ColorParser
|
|
52
54
|
def self.request
|
53
55
|
@request ||= Request.new
|
54
56
|
end
|
55
|
-
|
56
|
-
# Request an asset
|
57
|
-
#
|
58
|
-
class Request
|
59
|
-
@@last_request = Time.now
|
60
|
-
|
61
|
-
# default throttle requests 1 per sec
|
62
|
-
def initialize(params={})
|
63
|
-
@throttle = params[:throttle] || 1
|
64
|
-
end
|
65
|
-
|
66
|
-
def get(url)
|
67
|
-
throttle
|
68
|
-
|
69
|
-
begin
|
70
|
-
uri = URI.parse(url.strip)
|
71
|
-
rescue URI::InvalidURIError
|
72
|
-
uri = URI.parse(URI.escape(url.strip))
|
73
|
-
end
|
74
|
-
|
75
|
-
response = get_response(uri)
|
76
|
-
|
77
|
-
# redirect
|
78
|
-
@prev_redirect ||= ""
|
79
|
-
if response.header['location']
|
80
|
-
# make sure we're not in an infinite loop
|
81
|
-
if response.header['location'] == @prev_redirect
|
82
|
-
raise HTTPError, "Recursive redirect: #{@prev_redirect}"
|
83
|
-
end
|
84
|
-
@prev_redirect = response.header['location']
|
85
|
-
|
86
|
-
return get(response.header['location'])
|
87
|
-
end
|
88
|
-
|
89
|
-
# bad req
|
90
|
-
if response.to_s.index 'Bad Request' || response.nil?
|
91
|
-
raise HTTPError, "invalid HTTP request #{url}"
|
92
|
-
end
|
93
|
-
|
94
|
-
response = fix_encoding(response)
|
95
|
-
response.body
|
96
|
-
end
|
97
|
-
|
98
|
-
def user_agent
|
99
|
-
"ColorParser Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7"
|
100
|
-
end
|
101
|
-
|
102
|
-
|
103
|
-
private
|
104
|
-
|
105
|
-
# Use charset in content-type, default to UTF-8 if absent
|
106
|
-
#
|
107
|
-
# text/html; charset=UTF-8
|
108
|
-
# - or -
|
109
|
-
# text/html; charset=iso-8859-1
|
110
|
-
# - or -
|
111
|
-
# text/html
|
112
|
-
def fix_encoding(response)
|
113
|
-
charset = if response.header["Content-Type"].to_s.include?("charset")
|
114
|
-
response.header["Content-Type"].split(";")[1].split("=")[1]
|
115
|
-
else
|
116
|
-
"UTF-8"
|
117
|
-
end
|
118
|
-
|
119
|
-
response.body.force_encoding(charset.upcase).encode("UTF-8")
|
120
|
-
response
|
121
|
-
end
|
122
|
-
|
123
|
-
# build http request object
|
124
|
-
def get_response(uri)
|
125
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
126
|
-
http.open_timeout = 15
|
127
|
-
http.read_timeout = 30
|
128
|
-
|
129
|
-
request = Net::HTTP::Get.new(uri.request_uri)
|
130
|
-
request["User-Agent"] = user_agent
|
131
|
-
|
132
|
-
http.request(request)
|
133
|
-
end
|
134
|
-
|
135
|
-
# throttle requests to 1 per sec
|
136
|
-
def throttle
|
137
|
-
sleep @throttle if @@last_request + @throttle > Time.now
|
138
|
-
@@last_request = Time.now
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
57
|
end
|
@@ -1,14 +1,14 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ColorParser do
|
4
|
-
|
5
|
-
ColorParser.request =
|
4
|
+
before(:each) do
|
5
|
+
ColorParser.request = FakeRequest.new
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should retrieve fixture" do
|
9
9
|
url = "http://example.com/css/absolute.html?foo=bar"
|
10
10
|
result = ColorParser.request.get(url)
|
11
|
-
result.
|
11
|
+
expect(result).to be
|
12
12
|
end
|
13
13
|
|
14
14
|
|
@@ -16,18 +16,23 @@ describe ColorParser do
|
|
16
16
|
|
17
17
|
it "should parse url" do
|
18
18
|
url = "http://example.com/test/something/"
|
19
|
-
|
19
|
+
parsed = ColorParser.parse_url(url)
|
20
|
+
|
21
|
+
expect(parsed).to eq ["example.com", "/test/something/", nil]
|
20
22
|
end
|
21
23
|
|
22
24
|
it "should parse url with no trailing slash" do
|
23
|
-
|
24
25
|
url = "http://example.com"
|
25
|
-
|
26
|
+
parsed = ColorParser.parse_url(url)
|
27
|
+
|
28
|
+
expect(parsed).to eq ["example.com", "/", nil]
|
26
29
|
end
|
27
30
|
|
28
31
|
it "should parse url with query params" do
|
29
32
|
url = "http://example.com?foo=bar&baz=bar"
|
30
|
-
|
33
|
+
parsed = ColorParser.parse_url(url)
|
34
|
+
|
35
|
+
expect(parsed).to eq ["example.com", "/", "foo=bar&baz=bar"]
|
31
36
|
end
|
32
37
|
|
33
38
|
|
@@ -38,7 +43,7 @@ describe ColorParser do
|
|
38
43
|
asset = "http://asset.example.com/stylesheets/style.css"
|
39
44
|
|
40
45
|
parsed = ColorParser.parse_asset(doc, asset)
|
41
|
-
parsed.
|
46
|
+
expect(parsed).to eq "http://asset.example.com/stylesheets/style.css"
|
42
47
|
end
|
43
48
|
|
44
49
|
it "should parse asset absolute path with query string" do
|
@@ -46,7 +51,7 @@ describe ColorParser do
|
|
46
51
|
asset = "http://asset.example.com/stylesheets/style.css?baz=bar"
|
47
52
|
|
48
53
|
parsed = ColorParser.parse_asset(doc, asset)
|
49
|
-
parsed.
|
54
|
+
expect(parsed).to eq "http://asset.example.com/stylesheets/style.css?baz=bar"
|
50
55
|
end
|
51
56
|
|
52
57
|
it "should parse relative root path" do
|
@@ -54,7 +59,7 @@ describe ColorParser do
|
|
54
59
|
asset = "/styles/style.css"
|
55
60
|
|
56
61
|
parsed = ColorParser.parse_asset(doc, asset)
|
57
|
-
parsed.
|
62
|
+
expect(parsed).to eq "http://example.com/styles/style.css"
|
58
63
|
end
|
59
64
|
|
60
65
|
it "should parse relative root path with query string" do
|
@@ -62,7 +67,7 @@ describe ColorParser do
|
|
62
67
|
asset = "/styles/style.css?baz=bar"
|
63
68
|
|
64
69
|
parsed = ColorParser.parse_asset(doc, asset)
|
65
|
-
parsed.
|
70
|
+
expect(parsed).to eq "http://example.com/styles/style.css?baz=bar"
|
66
71
|
end
|
67
72
|
|
68
73
|
it "should parse relative path" do
|
@@ -70,7 +75,7 @@ describe ColorParser do
|
|
70
75
|
asset = "ie.css"
|
71
76
|
|
72
77
|
parsed = ColorParser.parse_asset(doc, asset)
|
73
|
-
parsed.
|
78
|
+
expect(parsed).to eq "http://example.com/stylesheets/ie.css"
|
74
79
|
end
|
75
80
|
|
76
81
|
it "should parse relative path with query string" do
|
@@ -78,6 +83,6 @@ describe ColorParser do
|
|
78
83
|
asset = "ie.css?baz=bar"
|
79
84
|
|
80
85
|
parsed = ColorParser.parse_asset(doc, asset)
|
81
|
-
parsed.
|
86
|
+
expect(parsed).to eq "http://example.com/stylesheets/ie.css?baz=bar"
|
82
87
|
end
|
83
88
|
end
|
data/spec/color_spec.rb
ADDED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|