Class: Schapi::SchoolAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/schapi/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(kind, region, code) ⇒ SchoolAPI

Returns a new instance of SchoolAPI.



38
39
40
41
42
# File 'lib/schapi/api.rb', line 38

def initialize(kind, region, code)
    @kind = kind
    @region = region
    @code = code
end

Instance Method Details

#get_menu_hash_from_data(data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/schapi/api.rb', line 48

def get_menu_hash_from_data(data)
    menu = Hash.new
    chunks = data.scan(/[가-힣]+\([가-힣]+\)|[가-힣]+/)
    
    timings = ['조식', '중식', '석식']
    timing = 0
    
    chunks.each do |t|
        if t.match /[조중석]식/
            timing = timings.index(t)
            menu[timing] = Array.new
        else
            menu[timing].push(t)
        end
    end

    return menu
end

#get_monthly_menus(year, month) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/schapi/api.rb', line 77

def get_monthly_menus(year, month)
    menus = Hash.new
    doc = Nokogiri::HTML open(self.get_monthly_menus_url(year, month))
    
    self.get_monthly_menus_from_doc(doc).each_pair do |d, m|
        breakfast, lunch, dinner = m[0], m[1], m[2]
        menus[d] = Menu.new breakfast, lunch, dinner
    end

    return menus
end

#get_monthly_menus_from_doc(doc) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/schapi/api.rb', line 67

def get_monthly_menus_from_doc(doc) 
    menus = Hash.new

    doc.css('.tbl_calendar.tbl_type3 td').select{ |e| e.text != ' ' }.each.with_index do |e, i|
        menus[i + 1] = self.get_menu_hash_from_data(e.text)
    end

    return menus
end

#get_monthly_menus_url(year, month) ⇒ Object



44
45
46
# File 'lib/schapi/api.rb', line 44

def get_monthly_menus_url(year, month)
    return URL::MONTHLY_MENUS % [@region, @code, @kind, @kind, year, month]
end