netcity 0.1.0 → 0.3.5

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: 276594a434d3b63e3be1cd653a408a4ff31c6470369782e823ced1ebd6843937
4
- data.tar.gz: d9522c13f09c84881135c0483619c613c9c5b3a1bbcfaf13cb1c4fcc7b04015a
3
+ metadata.gz: e74345b40482be87c3f32bfe664bf3fd78011b07f9a13929284a280acb14b4d2
4
+ data.tar.gz: abfcbee97e017fff139458d3ee8cb6fc906f121b93dd67bfce9cb20018ca5226
5
5
  SHA512:
6
- metadata.gz: 96086c00b3c51bc4ce8115bb73f097c7a821556bfc9fd32760c0d5e5a59f3fa5fabc76ca5d87a97fa7077e94ea354da9095ce041f8dc54b89d9df2a4348fd83e
7
- data.tar.gz: 354cf177c62adb030bb82907ce01abe6e579e4d5dafaaa9e24654c6b86bc1b3145e0b0cced42078d2129d3d51bfc6a52d699e69d109aad6a8b067bc68dbbb120
6
+ metadata.gz: 7493df8082793fc00598852edf70dc4fff77372179db218d36a481aeeb0247db621e825626fe05e97301ee1e6d7b17a4ac3ade38471a9114b3cb42496495ca85
7
+ data.tar.gz: c5e0a9b422e8784328adc8340240ace60147f07fa25899b728589227572e7dcdb38b3c44ae96476805f05078ef753af745fcfb9c3d7f8e6fd83f88265fe8f297
data/README.md CHANGED
@@ -1 +1,7 @@
1
- # netcity
1
+ # netcity
2
+
3
+ [![Build Status](https://travis-ci.com/tulen34/netcity.svg?branch=main)](https://travis-ci.com/tulen34/netcity)
4
+ [![Gem Version](https://badge.fury.io/rb/netcity.svg)](https://badge.fury.io/rb/netcity)
5
+ [![License](https://img.shields.io/github/license/tulen34/netcity)](https://github.com/tulen34/netcity/blob/main/LICENSE)
6
+
7
+ **API** клиент для "Сетевого города". Он позволяет легко взаимодействовать с **электронным дневником**.
data/bin/test ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "netcity"
4
+
5
+ client = NetCity.new("https://sgo.volganet.ru",
6
+ "Волгоградская обл",
7
+ "Городской округ Волжский",
8
+ "Волжский, г.",
9
+ "Общеобразовательная",
10
+ "МОУ СШ № 28",
11
+ "Глушонков",
12
+ "22081979As")
13
+ client.login
14
+ pp client
15
+ client.logout
data/lib/netcity.rb CHANGED
@@ -7,70 +7,69 @@ require "netcity/errors.rb"
7
7
  class NetCity
8
8
  attr_accessor :client, :user_name, :password, :school_address
9
9
 
10
- def initialize(base_url, user_name, password, school_address)
10
+ def initialize(base_url, state, province, city, func, school,
11
+ user_name, password)
11
12
  @client = HTTP["user-agent": "netcity/#{VERSION}", referer: base_url]
12
13
  .persistent(base_url)
13
14
 
14
15
  @user_name = user_name
15
16
  @password = password
16
- @school_address = school_address
17
+ @school_address = SchoolAddress.new(state, province, city, func, school)
17
18
  end
18
19
 
19
20
  def login
20
- data = Hash.new.tap do |hash|
21
+ form = JSON.parse(@client.get("/webapi/prepareloginform"))
22
+ .keep_if { |k| %w(cid sid pid cn sft scid).include?(k) }
23
+
24
+ queue = form.keys[...-1].each_index
25
+ .zip(@school_address.members)
26
+
27
+ queue.each do |index, member|
28
+ concrete_form = JSON.parse(@client.get("/webapi/loginform", params: {
29
+ lastname: form.keys[index]
30
+ }.merge(form)))
31
+
32
+ match = concrete_form["items"].find do |item|
33
+ item["name"] == @school_address[member]
34
+ end or raise LoginFormError.new(member, @school_address[member])
35
+
36
+ form.update(form.keys[index + 1] => match["id"])
37
+ end
38
+
39
+ data = {}.tap do |h|
21
40
  responce = @client.post("/webapi/auth/getdata")
22
41
  @client = @client.cookies(responce.cookies)
23
- hash.update(JSON.parse(responce))
42
+ h.update(JSON.parse(responce))
24
43
  end
25
44
 
26
45
  encoded_password = Digest::MD5.new.tap do |md5|
27
46
  md5 << data.delete("salt") << Digest::MD5.hexdigest(@password)
28
47
  end.hexdigest
29
48
 
30
- data.update(login_form,
31
- logintype: 1,
32
- un: @user_name,
33
- pw: encoded_password[...@password.size],
34
- pw2: encoded_password)
35
-
36
- responce = JSON.parse(@client.post("/webapi/login", form: data))
37
- unless responce.include?("at")
38
- if responce["message"].size == 29
39
- raise LoginDataError
40
- else
41
- raise Error, responce["message"]
42
- end
43
- end
44
- @client = @client.headers(at: responce["at"])
49
+ info = {}.tap do |h|
50
+ responce = @client.post("/webapi/login", form: {
51
+ logintype: 1,
52
+ un: @user_name,
53
+ pw: encoded_password[...@password.size],
54
+ pw2: encoded_password
55
+ }.merge(form, data))
45
56
 
46
- if block_given?
47
- yield self
48
- logout
57
+ @client = @client.cookies(responce.cookies)
58
+ h.update(JSON.parse(responce))
49
59
  end
60
+
61
+ raise Error, info["message"] unless info.include?("at")
62
+ @client = @client.headers(at: info["at"])
63
+
64
+ students, id = JSON.parse(@client.get("/webapi/student/diary/init"))
65
+ .values_at("students", "currentStudentId")
66
+
67
+ @student_id = students[id]["studentId"]
68
+ @year_id = JSON.parse(@client.get("/webapi/context"))["schoolYearId"]
50
69
  end
51
70
 
52
71
  def logout
53
72
  @client.post("/webapi/auth/logout")
54
73
  nil
55
74
  end
56
-
57
- private
58
-
59
- def login_form
60
- login_form = JSON.parse(@client.get("/webapi/prepareloginform"))
61
- .keep_if { |key| key =~ /cid|sid|pid|cn|sft|scid/ }
62
-
63
- queue = login_form.keys[...-1].each_index.zip(@school_address.members)
64
- queue.each do |index, member|
65
- params = login_form.merge(lastname: login_form.keys[index])
66
- form = JSON.parse(@client.get("/webapi/loginform", params: params))
67
-
68
- match = form["items"].find do |item|
69
- item["name"] == @school_address[member]
70
- end or raise LoginFormError.new(member, @school_address[member])
71
-
72
- login_form.update(login_form.keys[index + 1] => match["id"])
73
- end
74
- login_form
75
- end
76
75
  end
data/lib/netcity/data.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class NetCity
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.5"
3
3
 
4
4
  SchoolAddress = Struct.new(:state, :province, :city, :func, :school)
5
5
  end
@@ -3,18 +3,30 @@ class NetCity
3
3
 
4
4
  class LoginFormError < Error
5
5
  def initialize(member, value)
6
- @member = member
7
- @value = value
6
+ @member = case member
7
+ when :state
8
+ "Регион"
9
+ when :province
10
+ "Городской округ / Муниципальный район"
11
+ when :city
12
+ "Населённый пункт"
13
+ when :func
14
+ "Тип ОО"
15
+ when :school
16
+ "Образовательная организация"
17
+ end
18
+
19
+ @value = value
8
20
  end
9
21
 
10
22
  def to_s
11
- "Missing #{@member} with name #{@value}"
23
+ "Отсутствует #{@member} со значением #{@value}"
12
24
  end
13
25
  end
14
26
 
15
27
  class LoginDataError < Error
16
28
  def to_s
17
- "Incorrect password or login"
29
+ "Неправильный логин или пароль"
18
30
  end
19
31
  end
20
32
  end
data/netcity.gemspec CHANGED
@@ -12,8 +12,11 @@ Gem::Specification.new do |s|
12
12
  API клиент для "Сетевого города". Он позволяет легко взаимодействовать с
13
13
  электронным дневником.
14
14
  EOF
15
- s.homepage = "https://github.com/tulen34/netcity"
15
+ s.homepage = "https://rubygems.org/gems/netcity"
16
16
  s.license = "MIT"
17
+ s.metadata = {
18
+ "source_code_uri" => "https://github.com/tulen34/netcity"
19
+ }
17
20
 
18
21
  s.files = Rake::FileList["**/*"].exclude(*File.read(".gitignore").split)
19
22
  s.executables = s.files.grep(/^bin\/.+/) { |f| File.basename(f) }
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.1.0
4
+ version: 0.3.5
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-02-21 00:00:00.000000000 Z
11
+ date: 2021-03-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  API клиент для "Сетевого города". Он позволяет легко взаимодействовать с
@@ -16,6 +16,7 @@ description: |
16
16
  email: pulsar04040@gmail.com
17
17
  executables:
18
18
  - netcity
19
+ - test
19
20
  extensions: []
20
21
  extra_rdoc_files: []
21
22
  files:
@@ -23,14 +24,16 @@ files:
23
24
  - LICENSE
24
25
  - README.md
25
26
  - bin/netcity
27
+ - bin/test
26
28
  - lib/netcity.rb
27
29
  - lib/netcity/data.rb
28
30
  - lib/netcity/errors.rb
29
31
  - netcity.gemspec
30
- homepage: https://github.com/tulen34/netcity
32
+ homepage: https://rubygems.org/gems/netcity
31
33
  licenses:
32
34
  - MIT
33
- metadata: {}
35
+ metadata:
36
+ source_code_uri: https://github.com/tulen34/netcity
34
37
  post_install_message:
35
38
  rdoc_options: []
36
39
  require_paths: