klepto 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ module Klepto
2
+ module Parser
3
+ def self.included(klass)
4
+ klass.extend ClassMethods
5
+ end
6
+
7
+ def call(node)
8
+ node.try(:text)
9
+ end
10
+
11
+ module ClassMethods
12
+ end
13
+ end
14
+ end
15
+
16
+
@@ -29,8 +29,13 @@ module Klepto
29
29
  options[:attr] ||= nil
30
30
  options[:default] ||= nil
31
31
  options[:limit] ||= nil
32
+ options[:parser] ||= nil
32
33
  selector = args.shift
33
34
 
35
+ if !block_given? && options[:parser]
36
+ block = options[:parser].new
37
+ end
38
+
34
39
  Klepto.logger.debug("\t\tDefining attribute: #{meth} -> #{selector}")
35
40
 
36
41
  if options[:as] == :collection
@@ -48,7 +53,6 @@ module Klepto
48
53
  result = _context.first( options[:syntax], selector )
49
54
  Klepto.logger.debug("\t\t\tAs: resource, Result? #{!result.nil?}")
50
55
  @_hash[meth] = Structure.build(result, self, &block)
51
-
52
56
  elsif block
53
57
  result = selector ?
54
58
  _context.send( options[:match], options[:syntax], selector ) : _context
@@ -1,3 +1,3 @@
1
1
  module Klepto
2
- VERSION = "0.4.8"
2
+ VERSION = "0.4.9"
3
3
  end
@@ -247,6 +247,21 @@ describe Klepto::Bot do
247
247
  end
248
248
  end
249
249
 
250
+ describe 'structuring with a Parser' do
251
+ before(:each) do
252
+ @bot = Klepto::Bot.new("https://twitter.com/justinbieber"){
253
+ name 'h1.fullname', parser: TextParser
254
+ links 'span.url a', :match => :all, :parser => HrefParser
255
+ }
256
+ @structure = @bot.resources
257
+ end
258
+
259
+ it 'should structure the data' do
260
+ @structure.first[:name].should match(/Justin/i)
261
+ @structure.first[:links].first.should match(/^http:/i)
262
+ end
263
+ end
264
+
250
265
  describe 'creating a bot with a node limit' do
251
266
  before(:each) do
252
267
  @bot = Klepto::Bot.new("https://twitter.com/justinbieber"){
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,7 @@ SimpleCov.start do
7
7
  end
8
8
 
9
9
  require 'klepto'
10
+ require 'klepto/parser'
10
11
  require 'vcr'
11
12
  require 'orm/active_record'
12
13
 
@@ -33,4 +34,14 @@ RSpec.configure do |config|
33
34
  config.after(:all) { TestMigration.down }
34
35
  config.treat_symbols_as_metadata_keys_with_true_values = true
35
36
  #config.filter_run_including :only => true
37
+ end
38
+
39
+ class TextParser
40
+ include Klepto::Parser
41
+ end
42
+ class HrefParser
43
+ include Klepto::Parser
44
+ def call(node)
45
+ node[:href]
46
+ end
36
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klepto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: poltergeist
16
- requirement: &70255695656940 !ruby/object:Gem::Requirement
16
+ requirement: &70325624163420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70255695656940
24
+ version_requirements: *70325624163420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: capybara
27
- requirement: &70255695655600 !ruby/object:Gem::Requirement
27
+ requirement: &70325624162920 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.0.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70255695655600
35
+ version_requirements: *70325624162920
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &70255695653740 !ruby/object:Gem::Requirement
38
+ requirement: &70325624162460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.5.6
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70255695653740
46
+ version_requirements: *70325624162460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activesupport
49
- requirement: &70255695652860 !ruby/object:Gem::Requirement
49
+ requirement: &70325624162080 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70255695652860
57
+ version_requirements: *70325624162080
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: multi_json
60
- requirement: &70255695375220 !ruby/object:Gem::Requirement
60
+ requirement: &70325624161540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '1.0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70255695375220
68
+ version_requirements: *70325624161540
69
69
  description: Tearing up web pages into ActiveRecord resources
70
70
  email:
71
71
  - github@coryodaniel.com
@@ -86,6 +86,7 @@ files:
86
86
  - lib/klepto/bot.rb
87
87
  - lib/klepto/browser.rb
88
88
  - lib/klepto/config.rb
89
+ - lib/klepto/parser.rb
89
90
  - lib/klepto/structure.rb
90
91
  - lib/klepto/tasks.rb
91
92
  - lib/klepto/version.rb