cut 0.0.3 → 0.0.4
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.md +5 -1
- data/lib/cut/class_methods.rb +8 -10
- data/lib/cut/client.rb +6 -4
- data/lib/cut/mapping.rb +11 -2
- data/lib/cut/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -30,6 +30,7 @@ class SearchResult
|
|
30
30
|
selector "li.g"
|
31
31
|
|
32
32
|
map :title, String, to: "h3.r"
|
33
|
+
map :url, String, to: "div.s cite", operation: lambda {|str| str.upcase }
|
33
34
|
|
34
35
|
end
|
35
36
|
```
|
@@ -38,7 +39,10 @@ Return Results:
|
|
38
39
|
|
39
40
|
```ruby
|
40
41
|
SearchResult.all(keywords: "war and peace")
|
41
|
-
#=> [#<SearchResult:
|
42
|
+
#=> [#<SearchResult:0x007f94bbfaae90 @title="War and Peace - Wikipedia, the free encyclopedia", @url="HTTPS://EN.WIKIPEDIA.ORG/WIKI/WAR_AND_PEACE">, #<SearchResult:0x007f94beed97c0 @title="War and Peace (Vintage Classics): Leo Tolstoy, Richard Pevear ...", @url="WWW.AMAZON.COM/WAR-PEACE-VINTAGE-CLASSICS.../DP/1400079985">, #<SearchResult:0x007f94be95ee80 @title="War and Peace (1956) - IMDb", @url="WWW.IMDB.COM/TITLE/TT0049934/">, #<SearchResult:0x007f94be9cb198 @title="SparkNotes: War and Peace", @url="WWW.SPARKNOTES.COM/LIT/WARANDPEACE/">, #<SearchResult:0x007f94be9c7ea8 @title="War and Peace by graf Leo Tolstoy - Free Ebook - Project Gutenberg", @url="WWW.GUTENBERG.ORG/EBOOKS/2600">, #<SearchResult:0x007f94bc83f218 @title="War and Peace by Leo Tolstoy - Reviews, Discussion, Bookclubs, Lists", @url="WWW.GOODREADS.COM/BOOK/SHOW/656.WAR_AND_PEACE">, #<SearchResult:0x007f94bba7ee80 @title="War and Peace - The Literature Network", @url="WWW.ONLINE-LITERATURE.COM/TOLSTOY/WAR_AND_PEACE/">, #<SearchResult:0x007f94bba7b820 @title="War and Peace - graf Leo Tolstoy - Google Books", @url="BOOKS.GOOGLE.COM/BOOKS/ABOUT/WAR_AND_PEACE.HTML?ID=2GOK4HJO2VKC">, #<SearchResult:0x007f94bbed4ac0 @title="Images for war and peace", @url="">, #<SearchResult:0x007f94bdda0eb8 @title="War and Peace - Shmoop", @url="WWW.SHMOOP.COM/WAR-AND-PEACE/">, #<SearchResult:0x007f94bdd695d0 @title="War and Peace - Planet PDF", @url="WWW.PLANETPDF.COM/PLANETPDF/PDFS/FREE_EBOOKS/WAR_AND_PEACE_NT.PDF">, #<SearchResult:0x007f94bdde53d8 @title="News for war and peace", @url="">]
|
43
|
+
|
44
|
+
SearchResult.first(keywords: "war and peace")
|
45
|
+
#=> #<SearchResult:0x007f94bdfbeb78 @title="War and Peace - Wikipedia, the free encyclopedia", @url="HTTPS://EN.WIKIPEDIA.ORG/WIKI/WAR_AND_PEACE">
|
42
46
|
```
|
43
47
|
|
44
48
|
|
data/lib/cut/class_methods.rb
CHANGED
@@ -14,11 +14,11 @@ module Cut
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def all(options = {})
|
17
|
-
|
18
|
-
|
19
|
-
response = Client.get(endpoint)
|
17
|
+
Client.get(@url, options).css(@selector).map {|node| from_node(node) }
|
18
|
+
end
|
20
19
|
|
21
|
-
|
20
|
+
def first(options = {})
|
21
|
+
from_node(Client.get(@url, options).at_css(@selector))
|
22
22
|
end
|
23
23
|
|
24
24
|
private
|
@@ -32,12 +32,10 @@ module Cut
|
|
32
32
|
send(:attribute, name, type)
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
instance.send("#{mapping.name}=", node.at_css(mapping.selector).value)
|
40
|
-
end
|
35
|
+
def from_node(node)
|
36
|
+
new.tap do |instance|
|
37
|
+
mappings.each do |mapping|
|
38
|
+
instance.send("#{mapping.name}=", mapping.value_from_node(node))
|
41
39
|
end
|
42
40
|
end
|
43
41
|
end
|
data/lib/cut/client.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module Cut
|
2
2
|
class Client
|
3
3
|
|
4
|
-
def self.get(
|
5
|
-
new(
|
4
|
+
def self.get(*args)
|
5
|
+
new(*args).get
|
6
6
|
end
|
7
7
|
|
8
|
-
def initialize(endpoint)
|
9
|
-
@endpoint = endpoint
|
8
|
+
def initialize(endpoint, parameters = {})
|
9
|
+
@endpoint = parameters.inject(endpoint) do |str, param|
|
10
|
+
str.gsub("{{#{param.first}}}", CGI.escape(param.last))
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
def get
|
data/lib/cut/mapping.rb
CHANGED
@@ -4,13 +4,22 @@ module Cut
|
|
4
4
|
attr_reader :name
|
5
5
|
|
6
6
|
def initialize(name, options = {})
|
7
|
-
@name
|
8
|
-
@selector
|
7
|
+
@name = name
|
8
|
+
@selector = options[:to]
|
9
|
+
@operation = options[:operation]
|
9
10
|
end
|
10
11
|
|
11
12
|
def selector
|
12
13
|
@selector ||= ".#{name}"
|
13
14
|
end
|
14
15
|
|
16
|
+
def operation
|
17
|
+
@operation ||= Proc.new {|value| value }
|
18
|
+
end
|
19
|
+
|
20
|
+
def value_from_node(node)
|
21
|
+
operation.call( node.at_css(selector).value.to_s )
|
22
|
+
end
|
23
|
+
|
15
24
|
end
|
16
25
|
end
|
data/lib/cut/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cut
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|