Class: Botch::Base
- Inherits:
-
Object
- Object
- Botch::Base
- Defined in:
- lib/botch/base.rb
Constant Summary collapse
- DEFAULT_INSTANCE_VARIABLES =
{ :header => nil, :body => nil, :status => nil, :url => nil }
- @@routes =
{ :filter => Filter.new, :rule => Rule.new }
Class Attribute Summary collapse
-
.client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
- .filter(label, options = {}, &block) ⇒ Object
- .generate_main_block(&block) ⇒ Object
- .generate_method(method_name, &block) ⇒ Object
- .generate_wrapper(&method) ⇒ Object
- .get(*urls, &block) ⇒ Object (also: run)
- .helpers(*extensions, &block) ⇒ Object
- .instance ⇒ Object
- .options ⇒ Object
- .post(*urls, &block) ⇒ Object
- .request(method, *urls, &block) ⇒ Object
- .reset ⇒ Object
- .reset! ⇒ Object
- .route(type, label, options = {}, &block) ⇒ Object
- .rule(label, options = {}, &block) ⇒ Object
- .set(key, value = nil) ⇒ Object
- .settings ⇒ Object
Instance Method Summary collapse
- #client ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #options ⇒ Object
- #settings ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
71 72 73 |
# File 'lib/botch/base.rb', line 71 def initialize @header, @body = nil, nil end |
Class Attribute Details
.client ⇒ Object
Returns the value of attribute client.
90 91 92 |
# File 'lib/botch/base.rb', line 90 def client @client end |
Class Method Details
.filter(label, options = {}, &block) ⇒ Object
117 118 119 |
# File 'lib/botch/base.rb', line 117 def filter(label, = {}, &block) route(:filter, label, , &block) end |
.generate_main_block(&block) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/botch/base.rb', line 137 def generate_main_block(&block) unbound_method = generate_method(:main_unbound_method, &block).bind(instance) case unbound_method.arity when 2 then proc{|r,v| unbound_method.call(r,v) } when 1 then proc{|r,v| unbound_method.call(r) } else proc{|r,v| unbound_method.call } end end |
.generate_method(method_name, &block) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/botch/base.rb', line 130 def generate_method(method_name, &block) define_method(method_name, &block) unbound_method = instance_method(method_name) remove_method(method_name) unbound_method end |
.generate_wrapper(&method) ⇒ Object
125 126 127 128 |
# File 'lib/botch/base.rb', line 125 def generate_wrapper(&method) method.arity != 0 ? proc {|args| method.call(*args) } : proc {|args| method.call } end |
.get(*urls, &block) ⇒ Object Also known as: run
189 |
# File 'lib/botch/base.rb', line 189 def get(*urls, &block); request(:get, *urls, &block); end |
.helpers(*extensions, &block) ⇒ Object
96 97 98 99 |
# File 'lib/botch/base.rb', line 96 def helpers(*extensions, &block) class_eval(&block) if block_given? include(*extensions) if extensions.any? end |
.instance ⇒ Object
92 93 94 |
# File 'lib/botch/base.rb', line 92 def instance @instance ||= self.new end |
.options ⇒ Object
202 203 204 |
# File 'lib/botch/base.rb', line 202 def ||= {} end |
.post(*urls, &block) ⇒ Object
190 |
# File 'lib/botch/base.rb', line 190 def post(*urls, &block); request(:post, *urls, &block); end |
.request(method, *urls, &block) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/botch/base.rb', line 155 def request(method, *urls, &block) unless self.client raise ArgumentError unless self.client.respond_to?(method) block = generate_main_block(&block) if block_given? urls.map do |url| filters, rules = @@routes.map{ |k, v| v.inject(url) } response = self.client.send(method, url, ) set_instance_variables :header => response[:header], :body => response[:body], :status => response[:status], :url => url response = response[:response] valid = true unless filters.empty? valid = filters.map{ |_filter| _filter[:block].call(response) }.all? next if settings[:disabled_invalid] && !valid end response = rules.inject(nil) { |result, _rule| _rule[:block].call((result || response)) } unless rules.empty? response = block.call(response, valid) if block_given? set_instance_variables(DEFAULT_INSTANCE_VARIABLES) response end end |
.reset ⇒ Object
146 147 148 |
# File 'lib/botch/base.rb', line 146 def reset @@routes = { :filter => Filter.new, :rule => Rule.new } end |
.reset! ⇒ Object
150 151 152 153 |
# File 'lib/botch/base.rb', line 150 def reset! reset settings = {} end |
.route(type, label, options = {}, &block) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/botch/base.rb', line 110 def route(type, label, = {}, &block) unbound_method = generate_method("#{type} #{label}", &block).bind(instance) wrapper = generate_wrapper(&unbound_method) @@routes[type.to_sym].add(label, , &wrapper) end |
.rule(label, options = {}, &block) ⇒ Object
121 122 123 |
# File 'lib/botch/base.rb', line 121 def rule(label, = {}, &block) route(:rule, label, , &block) end |
.set(key, value = nil) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/botch/base.rb', line 101 def set(key, value = nil) return __send__("#{key}=", value) if respond_to?("#{key}=") key_symbol = key.to_sym return settings[key_symbol] = value if settings.has_key?(key_symbol) [key_symbol] = value end |
.settings ⇒ Object
198 199 200 |
# File 'lib/botch/base.rb', line 198 def settings @settings ||= { :disabled_invalid => false } end |
Instance Method Details
#client ⇒ Object
75 76 77 |
# File 'lib/botch/base.rb', line 75 def client self.class.client end |
#options ⇒ Object
79 80 81 |
# File 'lib/botch/base.rb', line 79 def self.class. end |
#settings ⇒ Object
83 84 85 |
# File 'lib/botch/base.rb', line 83 def settings self.class.settings end |