Class: HTTPalooza::Lineup

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/httpalooza/lineup.rb

Overview

A Lineup assembles a set of Players to perform HTTP requests.

Examples:

Perform a request with curb

lineup = HTTPalooza::Lineup.new.with_curb
response = lineup.get('http://httpbin.org/')
response[:curb] #=> Get the response from Curb

Performing multiple requests

lineup = HTTPalooza::Lineup.new.with_curb.and_unirest
lineup.get('http://httpbin.org/')
response[:curb] #=> Get the response from Curb
response[:unirest] #=> Get the response from Unirest

Constant Summary collapse

UNINVITE_REGEX =
/^(uninvite|but|not|except|without|save|besides?|minus|barring|excluding|omitting|exempting|other_than|apart_from|aside_from)_(.+)/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

#delete, #get, #head, #post, #put

Constructor Details

#initializeLineup

Returns a new instance of Lineup.



21
22
23
# File 'lib/httpalooza/lineup.rb', line 21

def initialize
  @players = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/httpalooza/lineup.rb', line 38

def method_missing(name, *args, &blk)
  if uninvite = UNINVITE_REGEX.match(name)
    name = uninvite.captures.second
  end

  matched_players = Players.available.select do |player|
    name.to_s.include?(player.name.to_s)
  end

  return super(name, *args, &blk) if matched_players.empty?

  if uninvite
    players.subtract(matched_players)
  else
    players.merge(matched_players)
  end

  self
end

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



19
20
21
# File 'lib/httpalooza/lineup.rb', line 19

def players
  @players
end

Instance Method Details

#everyoneObject

Return a Lineup with all available Players



33
34
35
36
# File 'lib/httpalooza/lineup.rb', line 33

def everyone
  players.merge(Players.available)
  self
end

#run!(request) ⇒ Object



25
26
27
28
29
30
# File 'lib/httpalooza/lineup.rb', line 25

def run!(request)
  players.inject({}) do |responses, player|
    responses[player.name] = player.execute!(request)
    responses
  end
end