Class: TrollemIpsum

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_trollem_ipsum/trollem_ipsum.rb

Overview

A simple Ruby client for trollemipsum.appspot.com

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(available_lengths = nil, available_types = nil) ⇒ TrollemIpsum

scope available lengths and/or types to those of your choosing



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_trollem_ipsum/trollem_ipsum.rb', line 15

def initialize(available_lengths=nil, available_types=nil)

  @options = JSON.parse(Net::HTTP.get('trollemipsum.appspot.com', '/api.json'))
  @types =
    (available_types ||
      JSON.parse(@options["available_types"].gsub(/'/, "\"")).keys)
  @lengths =
    (available_lengths ||
      JSON.parse(@options["available_lengths"].gsub(/'/, "\"")).keys)

end

Class Method Details

.lorem(type = nil, length = nil) ⇒ Object



10
11
12
# File 'lib/ruby_trollem_ipsum/trollem_ipsum.rb', line 10

def self.lorem(type=nil, length=nil)
  self.new.lorem(type, length)
end

Instance Method Details

#lorem(type = nil, length = nil) ⇒ Object

randomly chooses type and length set in initializer unless you pass them



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_trollem_ipsum/trollem_ipsum.rb', line 29

def lorem(type=nil, length=nil)

  type   ||= @types.sample
  length ||= @lengths.sample

  begin
    JSON.parse(
      Net::HTTP.get(
        "trollemipsum.appspot.com",
        "/api.json?type=#{type}&length=#{length}"))["lorem"]
  rescue => e
    trololol
  end

end

#trolololObject



45
46
47
# File 'lib/ruby_trollem_ipsum/trollem_ipsum.rb', line 45

def trololol
  "tro#{"lo"*(2...10).to_a.sample}l"
end