botch 0.1.4 → 0.1.5
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.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/lib/botch/base.rb +8 -14
- data/lib/botch/main.rb +30 -0
- data/lib/botch/version.rb +1 -1
- data/spec/delegator_spec.rb +50 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eff678a7305dfcfc0c931cce263e2b35448b8468
|
4
|
+
data.tar.gz: 2a6c7ac9038ec3eed13797de421741514b918ea0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e4776acf3a3fe5023b784e2b4eaa902b469827e677c9c4aadd6752f374074d51fd115029c8bcbcd0b070681f591745e9d5b8ab690e1c7ca5505be1990b88c29
|
7
|
+
data.tar.gz: a0465a209cd8fb5199906871144d49ed593fc9de2849f9012737e0b2598ba56999988dba45beacbf270b84278c22c0a98810b74c8ce5c2028a26ab5b0a1e811e
|
data/README.md
CHANGED
data/lib/botch/base.rb
CHANGED
@@ -18,8 +18,7 @@ module Botch
|
|
18
18
|
|
19
19
|
def add(label, options = {}, &block)
|
20
20
|
raise ArgumentError unless block_given?
|
21
|
-
if
|
22
|
-
route = @routes[position]
|
21
|
+
if route = exists?(label)
|
23
22
|
route[:block] = block
|
24
23
|
route[:label] = label
|
25
24
|
else
|
@@ -34,14 +33,10 @@ module Botch
|
|
34
33
|
end
|
35
34
|
|
36
35
|
def exist?(label)
|
37
|
-
|
36
|
+
@routes.find{ |route| route[:label] == label }
|
38
37
|
end
|
39
38
|
|
40
|
-
alias
|
41
|
-
|
42
|
-
def index(label)
|
43
|
-
@routes.index{ |route| route[:label] === label }
|
44
|
-
end
|
39
|
+
alias exists? exist?
|
45
40
|
|
46
41
|
def inject(url)
|
47
42
|
@routes.inject([]) do |result, route|
|
@@ -122,9 +117,9 @@ module Botch
|
|
122
117
|
route(:rule, label, options, &block)
|
123
118
|
end
|
124
119
|
|
125
|
-
def generate_wrapper(&
|
126
|
-
|
127
|
-
|
120
|
+
def generate_wrapper(&block)
|
121
|
+
block.arity != 0 ? proc {|args| block.call(*args) } :
|
122
|
+
proc {|args| block.call }
|
128
123
|
end
|
129
124
|
|
130
125
|
def generate_method(method_name, &block)
|
@@ -153,8 +148,7 @@ module Botch
|
|
153
148
|
end
|
154
149
|
|
155
150
|
def request(method, *urls, &block)
|
156
|
-
|
157
|
-
set_default_options unless self.client
|
151
|
+
set_default_options unless self.client
|
158
152
|
raise ArgumentError unless self.client.respond_to?(method)
|
159
153
|
|
160
154
|
block = generate_main_block(&block) if block_given?
|
@@ -189,7 +183,7 @@ module Botch
|
|
189
183
|
def get(*urls, &block); request(:get, *urls, &block); end
|
190
184
|
def post(*urls, &block); request(:post, *urls, &block); end
|
191
185
|
|
192
|
-
alias
|
186
|
+
alias run get
|
193
187
|
|
194
188
|
def client=(name)
|
195
189
|
@client = Client.const_get("#{name.to_s.capitalize}Client").new(settings) if clients.include?(name)
|
data/lib/botch/main.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'botch'
|
2
|
+
|
3
|
+
module Botch
|
4
|
+
module Delegator
|
5
|
+
class << self
|
6
|
+
def delegate(*methods)
|
7
|
+
methods.each do |method_name|
|
8
|
+
define_method(method_name) do |*args, &block|
|
9
|
+
Delegator.target.send(method_name, *args, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def target
|
15
|
+
@target ||= Main
|
16
|
+
end
|
17
|
+
|
18
|
+
def target=(klass)
|
19
|
+
@target = klass
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
delegate :filter, :get, :helpers, :options, :post, :reset,
|
24
|
+
:request, :rule, :run, :set, :settings
|
25
|
+
end
|
26
|
+
|
27
|
+
class Main < Base; end
|
28
|
+
end
|
29
|
+
|
30
|
+
extend Botch::Delegator
|
data/lib/botch/version.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
require File.expand_path("../../lib/botch/main", __FILE__)
|
4
|
+
|
5
|
+
|
6
|
+
class DelegatorTest
|
7
|
+
class Dummy
|
8
|
+
attr_reader :result
|
9
|
+
|
10
|
+
def method_missing(*args, &block)
|
11
|
+
@result = args
|
12
|
+
@result << block if block_given?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.delegate_test(*args)
|
17
|
+
args.each do |name|
|
18
|
+
describe "delegate #{name}" do
|
19
|
+
before(:all) { @fake = Botch::Fake.new }
|
20
|
+
|
21
|
+
it "#{name}" do
|
22
|
+
result = DelegatorTest.dummy { send(name) }.result
|
23
|
+
expect(result).to eq([name])
|
24
|
+
end
|
25
|
+
|
26
|
+
it "#{name} with arguments" do
|
27
|
+
fake = @fake
|
28
|
+
result = DelegatorTest.dummy { send(name, fake.url) }.result
|
29
|
+
expect(result).to eq([name, "http://example.com/"])
|
30
|
+
end
|
31
|
+
|
32
|
+
it "#{name} with block" do
|
33
|
+
fake, block = @fake, proc {}
|
34
|
+
result = DelegatorTest.dummy { send(name, fake.url, &block) }.result
|
35
|
+
expect(result).to eq([name, "http://example.com/", block])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.dummy(&block)
|
42
|
+
dummy = Dummy.new
|
43
|
+
Botch::Delegator.target = dummy
|
44
|
+
Object.new.extend(Botch::Delegator).instance_eval(&block)
|
45
|
+
Botch::Delegator.target
|
46
|
+
end
|
47
|
+
|
48
|
+
delegate_test :filter, :get, :helpers, :options, :post, :reset,
|
49
|
+
:request, :rule, :run, :set, :settings
|
50
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: botch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- namusyaka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -96,8 +96,10 @@ files:
|
|
96
96
|
- lib/botch/clients/abstract_client.rb
|
97
97
|
- lib/botch/clients/faraday_client.rb
|
98
98
|
- lib/botch/clients/mechanize_client.rb
|
99
|
+
- lib/botch/main.rb
|
99
100
|
- lib/botch/version.rb
|
100
101
|
- spec/botch_spec.rb
|
102
|
+
- spec/delegator_spec.rb
|
101
103
|
- spec/faraday_spec.rb
|
102
104
|
- spec/filter_spec.rb
|
103
105
|
- spec/mechanize_spec.rb
|
@@ -128,6 +130,7 @@ specification_version: 4
|
|
128
130
|
summary: A DSL for web clawler.
|
129
131
|
test_files:
|
130
132
|
- spec/botch_spec.rb
|
133
|
+
- spec/delegator_spec.rb
|
131
134
|
- spec/faraday_spec.rb
|
132
135
|
- spec/filter_spec.rb
|
133
136
|
- spec/mechanize_spec.rb
|