Class: MonsterMash::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, *args, &block) ⇒ Request

Creates a new Request wrapper object.

Parameters:

  • type (Symbol)

    of HTTP request - :get, :post, :delete, :put



12
13
14
15
16
17
18
# File 'lib/monster_mash/request.rb', line 12

def initialize(http_method, *args, &block)
  @handler = nil
  @value = nil

  self.options = { :method => http_method }
  execute_dsl(*args, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/monster_mash/request.rb', line 20

def method_missing(method, *args, &block)
  if caller_klass && caller_klass.respond_to?(method)
    caller_klass.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#caller_klassObject

Returns the value of attribute caller_klass.



7
8
9
# File 'lib/monster_mash/request.rb', line 7

def caller_klass
  @caller_klass
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/monster_mash/request.rb', line 6

def errors
  @errors
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/monster_mash/request.rb', line 5

def options
  @options
end

Instance Method Details

#apply_defaults(default_blocks) ⇒ Object



32
33
34
# File 'lib/monster_mash/request.rb', line 32

def apply_defaults(default_blocks)
  default_blocks.each { |block| execute_dsl(&block) }
end

#base_uri(value = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/monster_mash/request.rb', line 70

def base_uri(value = nil)
  if value
    @base_uri = value
  end
  @base_uri
end

#build_requestObject



40
41
42
# File 'lib/monster_mash/request.rb', line 40

def build_request
  Typhoeus::Request.new(self.uri, self.options)
end

#execute_dsl(*args, &block) ⇒ Object



36
37
38
# File 'lib/monster_mash/request.rb', line 36

def execute_dsl(*args, &block)
  instance_exec(*args, &block) if block_given?
end

#handler(&block) ⇒ Object



77
78
79
80
81
82
# File 'lib/monster_mash/request.rb', line 77

def handler(&block)
  if block_given?
    @handler = block
  end
  @handler
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/monster_mash/request.rb', line 28

def respond_to?(method)
  (caller_klass && caller_klass.respond_to?(method)) || super
end

#run_serial_requestObject



44
45
46
# File 'lib/monster_mash/request.rb', line 44

def run_serial_request
  Typhoeus::Request.run(self.uri, self.options)
end

#uri(value = nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/monster_mash/request.rb', line 62

def uri(value = nil)
  if value
    @uri = base_uri ? URI.join(base_uri, value).to_s : value
  end

  @uri || base_uri
end

#valid?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/monster_mash/request.rb', line 48

def valid?
  self.errors = []

  if !handler
    self.errors << 'You need to set a handler block.'
  end

  if !uri
    self.errors << 'You need to set a uri.'
  end

  self.errors.empty?
end