Module: LipsumAPI
- Included in:
- Fixnum
- Defined in:
- lib/lipsum.rb
Constant Summary
collapse
- LIPSUM_URL =
'http://lipsum.com/feed/html'
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
25
|
# File 'lib/lipsum.rb', line 8
def method_missing(method, *args)
results = []
if method.to_s =~ /^lipsum_(.*)/ && ["paragraphs", "lists", "words", "bytes"].include?($1)
opts = (args.first.respond_to? :merge)?args.first: {}
opts.merge!(:what => $1)
opts.merge!(:amount => self) if self.is_a?(Fixnum)
plain_doc = perform_request opts
parse_response(plain_doc) do |element|
results << element.inner_text
end
results = if opts[:what] == 'words'
results.first.rstrip.lstrip
else
results.collect { |item| item.rstrip.lstrip }
end
results
end
end
|
Instance Method Details
#parse_response(plain_doc) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/lipsum.rb', line 27
def parse_response(plain_doc)
doc = Nokogiri::HTML(plain_doc)
doc.search('#lipsum p').each { |element|
yield element
}
end
|
34
35
36
37
|
# File 'lib/lipsum.rb', line 34
def perform_request(opts)
opts.merge!( :start => 'yes' ) if opts.delete( :start_with_lorem )
send_request(opts)
end
|
#send_request(opts) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/lipsum.rb', line 39
def send_request(opts)
begin
RestClient.post LIPSUM_URL, opts
rescue
puts "some error with inet connection =:)"
end
end
|