netcity 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 276594a434d3b63e3be1cd653a408a4ff31c6470369782e823ced1ebd6843937
4
+ data.tar.gz: d9522c13f09c84881135c0483619c613c9c5b3a1bbcfaf13cb1c4fcc7b04015a
5
+ SHA512:
6
+ metadata.gz: 96086c00b3c51bc4ce8115bb73f097c7a821556bfc9fd32760c0d5e5a59f3fa5fabc76ca5d87a97fa7077e94ea354da9095ce041f8dc54b89d9df2a4348fd83e
7
+ data.tar.gz: 354cf177c62adb030bb82907ce01abe6e579e4d5dafaaa9e24654c6b86bc1b3145e0b0cced42078d2129d3d51bfc6a52d699e69d109aad6a8b067bc68dbbb120
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in test.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+ gem "http", "~> 4.4.1"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Ilya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # netcity
data/bin/netcity ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env ruby -w
data/lib/netcity.rb ADDED
@@ -0,0 +1,76 @@
1
+ require "http"
2
+ require "digest"
3
+
4
+ require "netcity/data.rb"
5
+ require "netcity/errors.rb"
6
+
7
+ class NetCity
8
+ attr_accessor :client, :user_name, :password, :school_address
9
+
10
+ def initialize(base_url, user_name, password, school_address)
11
+ @client = HTTP["user-agent": "netcity/#{VERSION}", referer: base_url]
12
+ .persistent(base_url)
13
+
14
+ @user_name = user_name
15
+ @password = password
16
+ @school_address = school_address
17
+ end
18
+
19
+ def login
20
+ data = Hash.new.tap do |hash|
21
+ responce = @client.post("/webapi/auth/getdata")
22
+ @client = @client.cookies(responce.cookies)
23
+ hash.update(JSON.parse(responce))
24
+ end
25
+
26
+ encoded_password = Digest::MD5.new.tap do |md5|
27
+ md5 << data.delete("salt") << Digest::MD5.hexdigest(@password)
28
+ end.hexdigest
29
+
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"])
45
+
46
+ if block_given?
47
+ yield self
48
+ logout
49
+ end
50
+ end
51
+
52
+ def logout
53
+ @client.post("/webapi/auth/logout")
54
+ nil
55
+ 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
+ end
@@ -0,0 +1,5 @@
1
+ class NetCity
2
+ VERSION = "0.1.0"
3
+
4
+ SchoolAddress = Struct.new(:state, :province, :city, :func, :school)
5
+ end
@@ -0,0 +1,20 @@
1
+ class NetCity
2
+ Error = Class.new(StandardError)
3
+
4
+ class LoginFormError < Error
5
+ def initialize(member, value)
6
+ @member = member
7
+ @value = value
8
+ end
9
+
10
+ def to_s
11
+ "Missing #{@member} with name #{@value}"
12
+ end
13
+ end
14
+
15
+ class LoginDataError < Error
16
+ def to_s
17
+ "Incorrect password or login"
18
+ end
19
+ end
20
+ end
data/netcity.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ require "rake/file_list"
2
+ require_relative "lib/netcity/data.rb"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "netcity"
6
+ s.version = NetCity::VERSION
7
+ s.author = "Glushonkov Ilya"
8
+ s.email = "pulsar04040@gmail.com"
9
+
10
+ s.summary = 'API клиент для "Сетевого города"'
11
+ s.description = <<~EOF
12
+ API клиент для "Сетевого города". Он позволяет легко взаимодействовать с
13
+ электронным дневником.
14
+ EOF
15
+ s.homepage = "https://github.com/tulen34/netcity"
16
+ s.license = "MIT"
17
+
18
+ s.files = Rake::FileList["**/*"].exclude(*File.read(".gitignore").split)
19
+ s.executables = s.files.grep(/^bin\/.+/) { |f| File.basename(f) }
20
+
21
+ s.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
22
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: netcity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Glushonkov Ilya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ API клиент для "Сетевого города". Он позволяет легко взаимодействовать с
15
+ электронным дневником.
16
+ email: pulsar04040@gmail.com
17
+ executables:
18
+ - netcity
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - bin/netcity
26
+ - lib/netcity.rb
27
+ - lib/netcity/data.rb
28
+ - lib/netcity/errors.rb
29
+ - netcity.gemspec
30
+ homepage: https://github.com/tulen34/netcity
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.7.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.2.7
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: API клиент для "Сетевого города"
53
+ test_files: []