Class: MonsterMash::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hydra, options = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/monster_mash/base.rb', line 16

def initialize(hydra, options = {})
  self.hydra = hydra
  self.options = options
end

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



12
13
14
# File 'lib/monster_mash/base.rb', line 12

def defaults
  @defaults
end

#hydraObject

Returns the value of attribute hydra.



13
14
15
# File 'lib/monster_mash/base.rb', line 13

def hydra
  @hydra
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/monster_mash/base.rb', line 14

def options
  @options
end

Class Method Details

.build_method(http_method, name, &setup_block) ⇒ Object

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/monster_mash/base.rb', line 46

def self.build_method(http_method, name, &setup_block) # :nodoc:
  if respond_to?(name)
    raise ArgumentError, "The method name \"#{name}\" is in use!"
  else
    method_name = "__real__" << name.to_s
    define_method(method_name) do |block, *args|
      self.class.execute(http_method, self.hydra, block, *args, &setup_block)
    end

    # Define the real instance method for this, and proxy
    # to the __real__method.
    class_eval <<-EOF
      def #{name}(*args, &block)
        #{method_name}(block, *args)
      end
    EOF

    (class << self; self; end).instance_eval do
      define_method(name) do |*args|
        execute(http_method, nil, nil, *args, &setup_block)
      end
    end
  end
end

.check_response_and_raise!(response) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/monster_mash/base.rb', line 71

def self.check_response_and_raise!(response)
  code = response.code.to_i
  if code < 200 or code >= 400
    error = MonsterMash::HTTPError.new("Got bad HTTP response! code: #{code}\n\n#{response.body}")
    error.response = response
    error.code = code
    error.body = response.body
    raise error
  end
end

.defaults(&block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/monster_mash/base.rb', line 37

def self.defaults(&block)
  if block_given? and !@has_defaults
    @has_defaults = true
    @defaults << block
  else
    @defaults
  end
end

.delete(name, &setup_block) ⇒ Object



33
34
35
# File 'lib/monster_mash/base.rb', line 33

def self.delete(name, &setup_block)
  build_method(:delete, name, &setup_block)
end

.get(name, &setup_block) ⇒ Object



21
22
23
# File 'lib/monster_mash/base.rb', line 21

def self.get(name, &setup_block)
  build_method(:get, name, &setup_block)
end

.inherited(subclass) ⇒ Object



82
83
84
# File 'lib/monster_mash/base.rb', line 82

def self.inherited(subclass)
  subclass.instance_variable_set("@defaults", defaults.dup)
end

.post(name, &setup_block) ⇒ Object



25
26
27
# File 'lib/monster_mash/base.rb', line 25

def self.post(name, &setup_block)
  build_method(:post, name, &setup_block)
end

.put(name, &setup_block) ⇒ Object



29
30
31
# File 'lib/monster_mash/base.rb', line 29

def self.put(name, &setup_block)
  build_method(:put, name, &setup_block)
end