Module: QuijotipsumAPI

Included in:
Fixnum
Defined in:
lib/quijotipsum.rb

Constant Summary collapse

QuijotipsumURL =
"http://quijotipsum.com/utils/lescript.php?%s=%s"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/quijotipsum.rb', line 8

def method_missing(method, *args)
  results = []
  if method.to_s =~ /^quijotipsum_(.*)/ && ["paragraphs", "words"].include?($1)
    opts = { :what => ($1 =~ /^par/? "par" : "pal") }
    opts [:amount] = self if self.is_a? Fixnum
    plain_doc = HTTParty.get(QuijotipsumURL % [opts[:what], opts[:amount]])

    paragraphs = plain_doc.split("\n\n").collect { |t|
      t.gsub(/<br \/>/, " ")
    }.map(&:rstrip).reject(&:empty?)

    return paragraphs.first if opts[:what] == 'words'
    paragraphs
  else
    raise NoMethodError
  end
end