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
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/lipsum.rb', line 9
def method_missing(method, *args)
results = []
if method.to_s =~ /^lipsum_(.*)/
opts = (args.first.respond_to? :merge)?args.first: {}
opts.merge!(:what => $1) if ["paragraphs", "lists", "words", "bytes"].include?($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 = results.first.rstrip.lstrip if opts[:what] == 'words'
results
end
end
|
Instance Method Details
#parse_response(plain_doc) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/lipsum.rb', line 24
def parse_response(plain_doc)
doc = Nokogiri::HTML(plain_doc)
doc.search('#lipsum p').each { |element|
yield element
}
end
|
31
32
33
34
|
# File 'lib/lipsum.rb', line 31
def perform_request(opts)
opts.merge!( :start => 'yes' ) if opts.delete( :start_with_lorem )
send_request(opts)
end
|
#send_request(opts) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/lipsum.rb', line 36
def send_request(opts)
begin
RestClient.post LIPSUM_URL, opts
rescue
puts "some error with inet connection =:)"
end
end
|