Class: Botch::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/botch/base.rb

Direct Known Subclasses

Main

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



66
67
68
# File 'lib/botch/base.rb', line 66

def initialize
  @header, @body = nil, nil
end

Class Attribute Details

.clientObject

Returns the value of attribute client.



85
86
87
# File 'lib/botch/base.rb', line 85

def client
  @client
end

Class Method Details

.filter(label, options = {}, &block) ⇒ Object



112
113
114
# File 'lib/botch/base.rb', line 112

def filter(label, options = {}, &block)
  route(:filter, label, options, &block)
end

.generate_main_block(&block) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/botch/base.rb', line 132

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



125
126
127
128
129
130
# File 'lib/botch/base.rb', line 125

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(&block) ⇒ Object



120
121
122
123
# File 'lib/botch/base.rb', line 120

def generate_wrapper(&block)
  block.arity != 0 ? proc {|args| block.call(*args) } :
                     proc {|args| block.call }
end

.get(*urls, &block) ⇒ Object Also known as: run



183
# File 'lib/botch/base.rb', line 183

def get(*urls, &block); request(:get, *urls, &block); end

.helpers(*extensions, &block) ⇒ Object



91
92
93
94
# File 'lib/botch/base.rb', line 91

def helpers(*extensions, &block)
  class_eval(&block) if block_given?
  include(*extensions) if extensions.any?
end

.instanceObject



87
88
89
# File 'lib/botch/base.rb', line 87

def instance
  @instance ||= self.new
end

.optionsObject



196
197
198
# File 'lib/botch/base.rb', line 196

def options
  @options ||= {}
end

.post(*urls, &block) ⇒ Object



184
# File 'lib/botch/base.rb', line 184

def post(*urls, &block); request(:post, *urls, &block); end

.request(method, *urls, &block) ⇒ Object

Raises:

  • (ArgumentError)


150
151
152
153
154
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
# File 'lib/botch/base.rb', line 150

def request(method, *urls, &block)
  set_default_options  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, options)

    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

.resetObject



141
142
143
# File 'lib/botch/base.rb', line 141

def reset
  @@routes = { :filter => Filter.new, :rule => Rule.new }
end

.reset!Object



145
146
147
148
# File 'lib/botch/base.rb', line 145

def reset!
  reset
  settings = {}
end

.route(type, label, options = {}, &block) ⇒ Object



105
106
107
108
109
110
# File 'lib/botch/base.rb', line 105

def route(type, label, options = {}, &block)
  unbound_method = generate_method("#{type} #{label}", &block).bind(instance)
  wrapper = generate_wrapper(&unbound_method)

  @@routes[type.to_sym].add(label, options, &wrapper)
end

.rule(label, options = {}, &block) ⇒ Object



116
117
118
# File 'lib/botch/base.rb', line 116

def rule(label, options = {}, &block)
  route(:rule, label, options, &block)
end

.set(key, value = nil) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/botch/base.rb', line 96

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)

  options[key_symbol] = value
end

.settingsObject



192
193
194
# File 'lib/botch/base.rb', line 192

def settings
  @settings ||= { :disabled_invalid => false }
end

Instance Method Details

#clientObject



70
71
72
# File 'lib/botch/base.rb', line 70

def client
  self.class.client
end

#optionsObject



74
75
76
# File 'lib/botch/base.rb', line 74

def options
  self.class.options
end

#settingsObject



78
79
80
# File 'lib/botch/base.rb', line 78

def settings
  self.class.settings
end