Class: Botch::Base

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

Constant Summary collapse

DEFAULT_INSTANCE_VARIABLES =
{ :header => nil, :body => nil, :status => 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.



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

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

Class Attribute Details

.clientObject

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, options = {}, &block)
  route(:filter, label, options, &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



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

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

.instanceObject



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

def instance
  @instance ||= self.new
end

.optionsObject



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

def options
  @options ||= {}
end

.post(*urls, &block) ⇒ Object



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

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
# 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])

    response = response[:response]

    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!
  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, 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



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

def rule(label, options = {}, &block)
  route(:rule, label, options, &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)

  options[key_symbol] = value
end

.settingsObject



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

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

Instance Method Details

#clientObject



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

def client
  self.class.client
end

#optionsObject



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

def options
  self.class.options
end

#settingsObject



83
84
85
# File 'lib/botch/base.rb', line 83

def settings
  self.class.settings
end