diff --git a/lib/chh_moretext.rb b/lib/chh_moretext.rb index c98c57c..83fdcdd 100644 --- a/lib/chh_moretext.rb +++ b/lib/chh_moretext.rb @@ -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" diff --git a/lib/chh_moretext/moretext.rb b/lib/chh_moretext/moretext.rb new file mode 100644 index 0000000..8cbe379 --- /dev/null +++ b/lib/chh_moretext/moretext.rb @@ -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