Permalink
Browse files

finished 0.1.0

  • Loading branch information...
1 parent 978b40e commit 2ac33082b0baef9c82a9a458b3063a8b583a24e5 @chinghanho committed Jan 4, 2013
Showing with 49 additions and 1 deletion.
  1. +26 −1 lib/chh_moretext.rb
  2. +23 −0 lib/chh_moretext/moretext.rb
View
@@ -1,5 +1,30 @@
require "chh_moretext/version"
+require "json"
+require "open-uri"
module ChhMoretext
- # Your code goes here...
+ class Base
+ class << self
+ def fetch_moretext(number, limit)
+ number = "n=#{number}"
+
+ if limit.is_a?(Range)
+ limit = "limit=#{limit.min},#{limit.max}"
+ elsif limit.is_a?(Integer)
+ limit = "limit=#{limit}"
+ else
+ limit = nil
+ end
+
+ if limit.nil?
+ condition = "?#{number}"
+ else
+ condition = "?#{number}&#{limit}"
+ end
+ return JSON(open("http://more.handlino.com/sentences.json#{condition}").read)["sentences"]
+ end
+ end
+ end
end
+
+require "chh_moretext/moretext"
@@ -0,0 +1,23 @@
+module ChhMoretext
+ class Moretext < Base
+ class << self
+ def sentence(sentence_length_range = nil)
+ fetch_moretext(1, sentence_length_range)
+ end
+
+ def sentences(sentence_count = 1, sentence_length_range = nil)
+ fetch_moretext(sentence_count, sentence_length_range).join("")
+ end
+
+ def paragraph(sentence_count = 3)
+ sentences(sentence_count)
+ end
+
+ def paragraphs(paragraph_count = 3)
+ [].tap { |paragraphs|
+ paragraph_count.times { paragraphs << paragraph }
+ }.join("\r\n\r\n")
+ end
+ end
+ end
+end

0 comments on commit 2ac3308

Please sign in to comment.