module Numbers

Public Class Methods

date(day, month) click to toggle source
# File lib/numbers.rb, line 11
def date(day, month)
  if day.is_a?(Integer) && month.is_a?(Integer)
    response = get_response(day, month)
    JSON.parse(response.body)
  else
    puts "day and month must be integers"
  end
end
math(int) click to toggle source
# File lib/numbers.rb, line 20
def math(int)
  if int.is_a?(Integer)  
    response = get_response(int)
    JSON.parse(response.body)
  else
    puts "must submit an integer"
  end
end
random() click to toggle source
# File lib/numbers.rb, line 29
def random 
  types = %w{trivia math date year}
  response = get_response(types.sample)
  JSON.parse(response.body)
end
trivia(int) click to toggle source
# File lib/numbers.rb, line 35
def trivia(int)
  if int.is_a?(Integer)
    response = get_response(int)
    JSON.parse(response.body)
  else
    puts "must submit an integer"
  end
end
year(int) click to toggle source
# File lib/numbers.rb, line 44
def year(int)
  if int.is_a?(Integer)
    response = get_response(int)
    JSON.parse(response.body)
  else
    puts "must submit an integer"
  end
end

Private Class Methods

get_response(*args) click to toggle source
# File lib/numbers.rb, line 54
def get_response(*args)
  type = caller[0][/`.*'/][1..-2]

  if type == "date"
    Connection.connection.get do |req|
      req.url "/#{args[0]}/#{args[1]}/#{type}?json=true" 
    end

  elsif type == "math"
    Connection.connection.get do |req|
      req.url "/#{args[0]}/#{type}?json=true" 
    end

  elsif type == "random"
    Connection.connection.get do |req|
      req.url "/#{type}/#{args[0]}/?json=true" 
    end

  elsif type == "trivia"
    Connection.connection.get do |req|
      req.url "/#{args[0]}/#{type}?json=true" 
    end
  
  elsif type == "year"
    Connection.connection.get do |req|
      req.url "/#{args[0]}/#{type}?json=true" 
    end
  
  else
    puts "what the hell did you do"
  end
end