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 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:0x007fd18be96588 @title="War and Peace - Wikipedia, the free encyclopedia">, #<SearchResult:0x007fd18b97c098 @title="War and Peace (1956) - IMDb">, #<SearchResult:0x007fd18b986188 @title="War and Peace (Vintage Classics): Leo Tolstoy, Richard Pevear ...">, #<SearchResult:0x007fd18b874038 @title="War and Peace by graf Leo Tolstoy - Free Ebook - Project Gutenberg">, #<SearchResult:0x007fd18b8b46b0 @title="SparkNotes: War and Peace">, #<SearchResult:0x007fd18bc070d8 @title="War and Peace by Leo Tolstoy - Reviews, Discussion, Bookclubs, Lists">, #<SearchResult:0x007fd18bf7c8d0 @title="War and Peace - The Literature Network">, #<SearchResult:0x007fd18bf7a0f8 @title="War and Peace - graf Leo Tolstoy - Google Books">, #<SearchResult:0x007fd18bfc2d58 @title="Images for war and peace">, #<SearchResult:0x007fd189397be8 @title="War and Peace - Planet PDF">, #<SearchResult:0x007fd1893c6268 @title="War and Peace - Shmoop">, #<SearchResult:0x007fd1895fe0f8 @title="News for war and peace">]
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
 
@@ -14,11 +14,11 @@ module Cut
14
14
  end
15
15
 
16
16
  def all(options = {})
17
- endpoint = @url.dup
18
- options.each {|key,value| endpoint.gsub!("{{#{key}}}", CGI.escape(value)) }
19
- response = Client.get(endpoint)
17
+ Client.get(@url, options).css(@selector).map {|node| from_node(node) }
18
+ end
20
19
 
21
- parse(response)
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 parse(response)
36
- response.css(@selector).map do |node|
37
- new.tap do |instance|
38
- mappings.each do |mapping|
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
@@ -1,12 +1,14 @@
1
1
  module Cut
2
2
  class Client
3
3
 
4
- def self.get(endpoint)
5
- new(endpoint).get
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
@@ -4,13 +4,22 @@ module Cut
4
4
  attr_reader :name
5
5
 
6
6
  def initialize(name, options = {})
7
- @name = name
8
- @selector = options[:to]
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
@@ -1,3 +1,3 @@
1
1
  module Cut
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
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.3
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-13 00:00:00.000000000 Z
12
+ date: 2013-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty