netcity 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24de9d2d19d1e4fbb099524730790458a61d98f2dc75fc139c74b6c86b636b05
4
- data.tar.gz: 1fbf557670cebc5faa508d18fde393c26b95c2ae3fcd5dcce1b4c8fb8011b3b2
3
+ metadata.gz: acf2517bc6c976016890ba1caf76949805d6afec2aa413f3172ba9754cc5b081
4
+ data.tar.gz: 93806e656f0404d18f6dcea593c80b8de5b5cfe69960291036207a0f4883a31f
5
5
  SHA512:
6
- metadata.gz: 73002751295a82ab78a9f4af076f2b33fc9307b6af3aff75b4708b604cce17e8d44f54091e6f6f0b35b1bdf3e6c74d4bc01b252402c38feea7953b0a90e94361
7
- data.tar.gz: 1713737e21a113d0f0068f0205d3d71df483e719e982e34cdd73904e7df35a27263a0f5abb39d704480c4812f773cb6d384231ee8eaaa3879f97d4439ac23617
6
+ metadata.gz: 3631c97c268327d2ebef673a49808d7b74c9bed5dae476daaa44c99a527d4f3f1373f6f3f36a4c5b7a6482e438defd8fb7e5ea904a2b11d68d3a214888cba0a3
7
+ data.tar.gz: 137e08d1f8a1385f06ce3b3ae9be7195e65024be11c6ef0fd5205f159ed0c2e49b73024abf44b8b3cfb1a2ffd34aed4e7069040fdcc272eb6287ea64b71d8d5b
data/README.md CHANGED
@@ -1,20 +1,46 @@
1
- # netcity
1
+ <center><h2>netcity</h2></center>
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/netcity.svg)](https://badge.fury.io/rb/netcity)
4
4
  ![Gem](https://img.shields.io/gem/dt/netcity)
5
5
  [![License](https://img.shields.io/github/license/tulen34/netcity)](https://github.com/tulen34/netcity/blob/main/LICENSE)
6
6
 
7
- **API** клиент для "Сетевого города". Он позволяет легко взаимодействовать с **электронным дневником**.
7
+ **API** клиент для "Сетевого города". Он позволяет легко взаимодействовать с **электронным дневником**. Клиент разрабатывался изначально для **Волгоградской области**, поэтому, возможно, у вас он **может не работать**.
8
8
 
9
- ### Установка
9
+ ### :hammer_and_wrench: Установка
10
10
 
11
- ```bash
11
+ ```sh
12
12
  gem install netcity
13
13
  ```
14
14
 
15
- ### :warning: Внимание
15
+ Или добавьте в ваш Gemfile эту строку:
16
16
 
17
- Клиент разрабатывался изначально для **Волгоградской области**, поэтому, возможно, у вас он может **не работать**
17
+ ```ruby
18
+ gem "http"
19
+ ```
20
+
21
+ ### :book: Пример
22
+
23
+ ```ruby
24
+ require 'netcity'
25
+
26
+ client = NetCity.new('https://sgo.volganet.ru',
27
+ 'Волгоградская обл',
28
+ 'Городской округ Волжский',
29
+ 'Волжский, г.',
30
+ 'Общеобразовательная',
31
+ 'МОУ СШ № 123',
32
+ 'Иванов',
33
+ '123456789')
34
+ client.session do |session|
35
+ puts session.diary
36
+ end
37
+ ```
38
+
39
+ ### :footprints: Прогресс разработки
40
+
41
+ - [x] Авторизация
42
+ - [x] Получение дневника
43
+ - [ ] Получение доски объявлений
18
44
 
19
45
  ### :love_letter: Благодарность
20
46
 
data/lib/netcity/data.rb CHANGED
@@ -1,21 +1,100 @@
1
+ require 'json'
2
+ require 'date'
3
+
1
4
  class NetCity
2
- SchoolAddress = Struct.new(:state, :province, :city, :func, :school)
5
+ SchoolAddress = Struct.new(:state,
6
+ :province,
7
+ :city,
8
+ :func,
9
+ :school)
10
+
11
+ LoginInfo = Struct.new(:school_address,
12
+ :user_name,
13
+ :password)
14
+
15
+ class Diary
16
+ include Enumerable
17
+
18
+ attr_reader :start, :end
19
+
20
+ def initialize(hash)
21
+ @start = hash['weekStart']
22
+ @end = hash['weekEnd']
23
+ @days = hash['weekDays'].map { |h| Day.new(h) }
24
+ end
3
25
 
4
- class Context
5
- attr_reader :full_name, :user_id, :year_id
26
+ def [](index)
27
+ @days[index]
28
+ end
6
29
 
7
- def initialize(data)
8
- @full_name = data['user']['name']
9
- @user_id = data['user']['id']
10
- @year_id = data['schoolYearId']
30
+ def each(&block)
31
+ @days.each { |lesson| block.call(day) }
11
32
  end
12
33
  end
13
34
 
14
- class Diary
15
- attr_reader :week_start, :week_end, :schedule
35
+ class Day
36
+ include Enumerable
37
+
38
+ attr_reader :date
39
+
40
+ def initialize(hash)
41
+ @date = hash['date']
42
+ @lessons = hash['lessons'].map { |h| Lesson.new(h) }
43
+ end
44
+
45
+ def [](index)
46
+ @lessons[index]
47
+ end
16
48
 
17
- def initilaize(data)
49
+ def each(&block)
50
+ @lessons.each { |lesson| block.call(lesson) }
51
+ end
52
+ end
53
+
54
+ Assignment = Struct.new(:id,
55
+ :type,
56
+ :name,
57
+ :mark,
58
+ :deadline)
59
+
60
+ class Lesson
61
+ include Enumerable
62
+
63
+ attr_reader :start, :end, :number, :name, :room
64
+
65
+ def initialize(hash)
66
+ @start = hash['startTime']
67
+ @end = hash['endTime']
68
+ @number = hash['number']
69
+ @name = hash['subjectName']
70
+ @room = hash['room']
71
+ return unless hash.include?('assignments')
72
+
73
+ @assignments = hash['assignments'].map do |h|
74
+ Assignment.new.tap do |a|
75
+ a.id = h['id']
76
+ a.type = h['typeId']
77
+ a.name = h['assignmentName']
78
+ a.mark = h['mark']['mark'] if h.include?('mark')
79
+ a.deadline = h['dueDate']
80
+ end
81
+ end
82
+ end
83
+
84
+ def [](index)
85
+ @assignments[index]
86
+ end
18
87
 
88
+ def each(&block)
89
+ @assignments.each { |assignment| block.call(assignment) }
19
90
  end
20
91
  end
21
92
  end
93
+
94
+ module JSON
95
+ def parse_diary(source)
96
+ NetCity::Diary.new(JSON.parse(source))
97
+ end
98
+
99
+ module_function :parse_diary
100
+ end
@@ -1,23 +1,31 @@
1
1
  require 'http'
2
+ require 'date'
2
3
  require 'digest'
3
4
 
4
5
  class NetCity
5
- attr_accessor :client, :user_name, :password, :school_address
6
- attr_reader :context
6
+ attr_accessor :client, :login_info
7
7
 
8
8
  def initialize(base_url,
9
9
  state, province, city, func, school,
10
10
  user_name, password)
11
- @client = HTTP["user-agent": "netcity/#{VERSION}", referer: base_url]
11
+ @client = HTTP['user-agent': "netcity/#{VERSION}", referer: base_url]
12
12
  .persistent(base_url)
13
13
 
14
- @user_name = user_name
15
- @password = password
16
- @school_address = SchoolAddress.new(state, province, city, func, school)
14
+ @login_info = LoginInfo.new.tap do |l|
15
+ l.user_name = user_name
16
+ l.password = password
17
+ l.school_address = SchoolAddress.new(state, province, city, func, school)
18
+ end
17
19
  end
18
20
 
19
- def diary(options = {})
20
-
21
+ def diary(week_start: Date.today - Date.today.wday + 1,
22
+ week_end: Date.today - Date.today.wday + 6)
23
+ JSON.parse_diary(@client.get('/webapi/student/diary', params: {
24
+ 'studentId' => @user_id,
25
+ 'weekStart' => week_start.to_s,
26
+ 'weekEnd' => week_end.to_s,
27
+ 'yearId' => @year_id
28
+ }))
21
29
  end
22
30
 
23
31
  def session
@@ -31,21 +39,23 @@ class NetCity
31
39
  form = JSON.parse(@client.get('/webapi/prepareloginform')).keep_if do |k|
32
40
  %w[cid sid pid cn sft scid].include?(k)
33
41
  end
34
-
35
42
  queue = form
36
43
  .keys[...-1]
37
44
  .each_index
38
- .zip(@school_address.members)
45
+ .zip(@login_info.school_address.members)
39
46
 
40
47
  queue.each do |i, member|
41
48
  current_form = JSON.parse(@client.get('/webapi/loginform', params: {
42
- lastname: form.keys[i]
49
+ 'lastname' => form.keys[i]
43
50
  }.merge(form)))
44
51
 
45
52
  match = current_form['items'].find do |item|
46
- item['name'] == @school_address[member]
47
- end or raise LoginFormError.new(member, @school_address[member])
53
+ item['name'] == @login_info.school_address[member]
54
+ end
48
55
 
56
+ if match.nil?
57
+ raise LoginFormError.new(member, @login_info.school_address[member])
58
+ end
49
59
  form.update(form.keys[i + 1] => match['id'])
50
60
  end
51
61
 
@@ -56,15 +66,15 @@ class NetCity
56
66
  end
57
67
 
58
68
  md5 = Digest::MD5.new.tap do |md5|
59
- md5 << data.delete("salt") << Digest::MD5.hexdigest(@password)
69
+ md5 << data.delete('salt') << Digest::MD5.hexdigest(@login_info.password)
60
70
  end.hexdigest
61
71
 
62
72
  info = {}.tap do |h|
63
73
  responce = @client.post('/webapi/login', form: {
64
- logintype: 1,
65
- un: @user_name,
66
- pw: md5[...@password.size],
67
- pw2: md5
74
+ 'logintype' => 1,
75
+ 'un' => @login_info.user_name,
76
+ 'pw' => md5[...@login_info.password.size],
77
+ 'pw2' => md5
68
78
  }.merge(form, data))
69
79
 
70
80
  @client = @client.cookies(responce.cookies)
@@ -74,7 +84,9 @@ class NetCity
74
84
  raise Error, info['message'] unless info.include?('at')
75
85
  @client = @client.headers(at: info['at'])
76
86
 
77
- @context = Context.new(JSON.parse(@client.get('/webapi/context')))
87
+ context = JSON.parse(@client.get('/webapi/context'))
88
+ @user_id = context['user']['id']
89
+ @year_id = context['schoolYearId']
78
90
  nil
79
91
  end
80
92
 
@@ -1,3 +1,3 @@
1
1
  class NetCity
2
- VERSION = "0.4.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/netcity.gemspec CHANGED
@@ -26,9 +26,9 @@ Gem::Specification.new do |s|
26
26
  "Gemfile",
27
27
  "LICENSE",
28
28
  "netcity.gemspec",
29
- "README.md",
29
+ "README.md"
30
30
  ]
31
- s.executables = s.files.grep(/^bin\/.+/) { |f| File.basename(f) }
31
+ s.executable = "netcity"
32
32
 
33
33
  s.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
34
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netcity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glushonkov Ilya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-08 00:00:00.000000000 Z
11
+ date: 2021-03-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  API клиент для "Сетевого города". Он позволяет легко взаимодействовать с