class GlitchIME

Public Class Methods

new(text) click to toggle source
# File lib/glitch-ime.rb, line 7
def initialize(text)
  @text = text
end

Public Instance Methods

generate() click to toggle source
# File lib/glitch-ime.rb, line 11
def generate
  result = ''
  patterns.each{|pair|
    key, values = *pair
    result += choice(values)
  }
  result
end

Private Instance Methods

choice(array) click to toggle source
# File lib/glitch-ime.rb, line 29
def choice(array)
  if array.respond_to? :choice
    array.choice
  elsif array.respond_to? :sample
    array.sample
  else
    array[(rand * array.length).to_i ]
  end
end
patterns() click to toggle source
# File lib/glitch-ime.rb, line 21
def patterns
  @json ||= begin
    url = "http://www.google.com/transliterate?langpair=ja-Hira%7Cja&text=" + CGI.escape(@text)
    src =  open(url).read.gsub(/\n/, '').gsub(/,\]/, ']')
    JSON.parse(src)
  end
end