class NetCity

Constants

Assignment
Error
LoginInfo
SchoolAddress
VERSION

Attributes

client[RW]
login_info[RW]

Public Class Methods

new(base_url, state, province, city, func, school, user_name, password) click to toggle source
# File lib/netcity/netcity.rb, line 8
def initialize(base_url, 
               state, province, city, func, school, 
               user_name, password)
  @client = HTTP['user-agent': "netcity/#{VERSION}", referer: base_url]
    .persistent(base_url)

  @login_info = LoginInfo.new.tap do |l|
    l.user_name      = user_name
    l.password       = password
    l.school_address = SchoolAddress.new(state, province, city, func, school)
  end
end

Public Instance Methods

diary(week_start: Date.today - Date.today.wday + 1, week_end: Date.today - Date.today.wday + 6) click to toggle source
# File lib/netcity/netcity.rb, line 21
def diary(week_start: Date.today - Date.today.wday + 1,
          week_end: Date.today - Date.today.wday + 6)
  JSON.parse_diary(@client.get('/webapi/student/diary', params: {
    'studentId' => @user_id,
    'weekStart' => week_start.to_s,
    'weekEnd'   => week_end.to_s,
    'yearId'    => @year_id
  }))
end
login() click to toggle source
# File lib/netcity/netcity.rb, line 38
def login
  form = JSON.parse(@client.get('/webapi/prepareloginform')).keep_if do |k|
    %w[cid sid pid cn sft scid].include?(k) 
  end
  queue = form
    .keys[...-1]
    .each_index
    .zip(@login_info.school_address.members)

  queue.each do |i, member|
    current_form = JSON.parse(@client.get('/webapi/loginform', params: {
      'lastname' => form.keys[i]
    }.merge(form)))

    match = current_form['items'].find do |item|
      item['name'] == @login_info.school_address[member]
    end 

    if match.nil?
      raise LoginFormError.new(member, @login_info.school_address[member])
    end
    form.update(form.keys[i + 1] => match['id'])
  end

  data = {}.tap do |h|
    responce = @client.post('/webapi/auth/getdata')
    @client  = @client.cookies(responce.cookies)
    h.update(JSON.parse(responce))
  end

  md5 = Digest::MD5.new.tap do |md5|
    md5 << data.delete('salt') << Digest::MD5.hexdigest(@login_info.password)
  end.hexdigest

  info = {}.tap do |h|
    responce = @client.post('/webapi/login', form: {
      'logintype' => 1,
      'un'        => @login_info.user_name,
      'pw'        => md5[...@login_info.password.size],
      'pw2'       => md5
    }.merge(form, data))

    @client = @client.cookies(responce.cookies)
    h.update(JSON.parse(responce))
  end

  raise Error, info['message'] unless info.include?('at')
  @client = @client.headers(at: info['at'])

  context  = JSON.parse(@client.get('/webapi/context'))
  @user_id = context['user']['id']
  @year_id = context['schoolYearId']
  nil
end
logout() click to toggle source
# File lib/netcity/netcity.rb, line 93
def logout
  @client.post('/webapi/auth/logout')
  nil
end
session() { |self| ... } click to toggle source
# File lib/netcity/netcity.rb, line 31
def session
  login
  yield self
ensure
  logout
end