class Tamai

Constants

VERSION

Public Class Methods

station(postal) click to toggle source
# File lib/tamai.rb, line 7
def self.station(postal)
  puts "数秒お待ちください"

  postal_str = postal.to_s
  return unless validate(postal_str)
  search(postal_str)

  if @station.nil?
    puts "見つかりませんでした"
    return
  end

  name = @station["name"]
  kana = @station["kana"]
  line = @station["line"]
  distance = @station["distance"].to_i.to_s
  prev_station = @station["prev"]
  next_station = @station["next"]
  # prefecture = @station["prefecture"]
  # latitude = @station["y"]
  # longtitude = @station["x"]

  puts "最寄駅: " + line + " " + name + " (" + kana + ")駅"
  if prev_station.nil?
    puts "隣駅: " + next_station
  elsif next_station.nil?
    puts "隣駅: " + prev_station
  else
    puts "隣駅: " + prev_station + "、" + next_station
  end
  puts "ここから約" + distance + "m"
end

Private Class Methods

correct?(postal) click to toggle source
# File lib/tamai.rb, line 62
def self.correct?(postal)
  /^\d{7}$/.match(postal)
end
correct_containing_hyphen?(postal) click to toggle source
# File lib/tamai.rb, line 66
def self.correct_containing_hyphen?(postal)
  /^\d{3}-\d{4}$/.match(postal)
end
validate(postal) click to toggle source
# File lib/tamai.rb, line 49
def self.validate(postal)
  if correct?(postal)
    return true
  elsif correct_containing_hyphen?(postal)
    postal.gsub!("-", "")
    return true
  else
    puts "7桁の郵便番号を半角数字で入力してください"
    puts "(ハイフンの有無は問いません)"
    return false
  end
end