Class: Quotifier
- Inherits:
-
Object
- Object
- Quotifier
- Defined in:
- lib/quotifier.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Quotifier
constructor
A new instance of Quotifier.
- #quotify(html, headers) ⇒ Object
- #random_quote ⇒ Object
Constructor Details
#initialize(app) ⇒ Quotifier
6 7 8 9 10 |
# File 'lib/quotifier.rb', line 6 def initialize(app) @app = app url = "https://raw.githubusercontent.com/jusleg/quotifier/master/db.json" @dictionnary = JSON.parse(Net::HTTP.get(URI.parse(url))) end |
Instance Method Details
#call(env) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/quotifier.rb', line 12 def call(env) status, headers, body = @app.call(env) if headers['Content-Type'].include?'text/html' [status, headers, quotify(body,headers)] else [status, headers, body] end end |
#quotify(html, headers) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/quotifier.rb', line 23 def quotify(html,headers) string_html = html[0] counter = 0 fresh_output = String.new string_html.each_line do |line| random_mod = rand(1..10) if counter % random_mod == 0 line += random_quote end fresh_output << line counter = counter + 1 end html[0] = fresh_output headers['Content-Length'] = fresh_output.length.to_s return html end |
#random_quote ⇒ Object
42 43 44 45 46 |
# File 'lib/quotifier.rb', line 42 def random_quote() = rand(0..(@dictionnary['authors'].count-1)) random_quote = rand(0..(@dictionnary['quotes'].count - 1)) return "\t\"" + @dictionnary['quotes'][random_quote] + " - "+ @dictionnary['authors'][] + "\"" + "\n" end |