kurki 0.3.1 → 0.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db479d77a3a2d711264f9aefa65446bdd4ad9136
4
- data.tar.gz: a04e7310befd0e261520818a21633d9187a766d1
3
+ metadata.gz: dfd88f581b6697927541b0bd0271fd001cb6e3bf
4
+ data.tar.gz: 50daa7d8c536c2d16379ac220e23693cd7ecdd4f
5
5
  SHA512:
6
- metadata.gz: 4bc5a08affcd5280aa4b22ed5d5cd54b3ac27f345ba947d46a8026c826cd2732ad2188204b10cc8221a1dbf301f14ad2205bbce5047c74fd9ebc8f876fe03b8d
7
- data.tar.gz: 7fd2b404a68472d513c4b7f6603e1302dbecd18e55aa1e7f3c95191803cb27946a638a013bc3506d1dd68a3635cbc67c9ff372aad73c3b6ae74bcc04e54532c9
6
+ metadata.gz: 801deab3c17fefd92de5b056e776f30136f0839203b263d2ab5c94741255483dc349ae11a53e1e138abb4a7370f877abebb14f55b7a4d220234cbc1438c77b65
7
+ data.tar.gz: 6262dbd7c3a7065d47409344b80b22747266fe5671d6e2b6e0d287c58cca1f4305b532b27865c4bc3980041fa01bdace1f6af45048f68f465d6a51af0837be7c
data/lib/kurki.rb CHANGED
@@ -42,34 +42,74 @@ module Kurki
42
42
  set_grades course_id, grades
43
43
  end
44
44
 
45
+
46
+ # Exam points in format:
47
+ # {
48
+ # "123123123" => {
49
+ # "1" => 1,
50
+ # "2" => 56
51
+ # }
52
+ # }
53
+ def self.set_exam_points(course_id, points)
54
+ post(url + "courses/#{course_id}/students", parse_exam_points(points))
55
+ end
56
+
57
+
58
+ # Exercise points in format:
59
+ # {
60
+ # "123123123" => {
61
+ # "2" => 1,
62
+ # "3" => 4
63
+ # }
64
+ # }
65
+ def self.set_exercise_points(course_id, points)
66
+ post(url + "courses/#{course_id}/students", parse_exercise_points(points))
67
+ end
68
+
45
69
  private
46
70
 
47
- def self.read_csv(file_path, delimiter)
48
- CSV.read(file_path, col_sep: delimiter).drop(1)
49
- end
71
+ def self.parse_exercise_points(points)
72
+ parse(points, lambda {|x,y| {id: x, exercises: y}})
73
+ end
50
74
 
51
- def self.parse_students(student_ids)
52
- parse(student_ids, lambda { |x| {"id": x} })
53
- end
75
+ def self.parse_exam_points(points)
76
+ parse(points, lambda {|x,y| { id: x, exams: y } })
77
+ end
54
78
 
55
- def self.parse_grades(grades)
56
- parse(grades, lambda {|x,y| { "id": x, "arvosana": y } })
57
- end
79
+ def self.read_csv(file_path, delimiter)
80
+ CSV.read(file_path, col_sep: delimiter).drop(1)
81
+ end
58
82
 
59
- def self.parse(items, exp)
60
- json = {}
61
- json["students"] = items.map(&exp)
62
- json
63
- end
83
+ def self.parse_students(student_ids)
84
+ parse(student_ids, lambda { |x| {id: x} })
85
+ end
64
86
 
65
- def self.post(address, data, params = {})
66
- return unless data
67
- params[:authorization] = ENV["KURKI_TOKEN"]
68
- JSON.parse(RestClient.post address, data.to_json, params: params, content_type: :json, accept: :json)
69
- end
87
+ def self.parse_grades(grades)
88
+ parse(grades, lambda {|x,y| { id: x, arvosana: y } })
89
+ end
70
90
 
71
- def self.get(address, params = {})
72
- params[:authorization] = ENV["KURKI_TOKEN"]
73
- JSON.parse(RestClient.get address, { params: params, accept: :json, content_type: :json })
74
- end
91
+ def self.parse(items, exp)
92
+ json = {}
93
+ json["students"] = items.map(&exp)
94
+ json
95
+ end
96
+
97
+ def self.post(address, data, params = {})
98
+ return unless data
99
+ params[:authorization] = ENV["KURKI_TOKEN"]
100
+ JSON.parse(RestClient.post address, data.to_json, params: params, content_type: :json, accept: :json)
101
+ end
102
+
103
+ def self.get(address, params = {})
104
+ params[:authorization] = ENV["KURKI_TOKEN"]
105
+
106
+ result = RestClient::Request.execute(
107
+ :url => address,
108
+ :method => :get,
109
+ :headers => { params: params, accept: :json, content_type: :json },
110
+ :verify_ssl => false
111
+ )
112
+
113
+ JSON.parse result
114
+ end
75
115
  end
data/lib/kurki/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kurki
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/spec/kurki_spec.rb CHANGED
@@ -34,6 +34,23 @@ describe Kurki do
34
34
  {\"id\":\"014291915\",\"arvosana\":\"1\"},
35
35
  {\"id\":\"123456789\",\"arvosana\":\"0\"}],\"error\":[]}", :headers => {})
36
36
 
37
+ stub_request(:post, /.*581259.2012.K.K.1\/students.*/).
38
+ with(:body => "{\"students\":[{\"id\":\"01293492\",\"exams\":{\"2\":23,\"1\":11}},{\"id\":\"012312333\",\"exams\":{\"1\":22,\"2\":40}}]}",
39
+ :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'99', 'Content-Type'=>'application/json'}).
40
+ to_return(:status => 200, :body => "{\"success\":[
41
+ {\"id\":\"01293492\",\"exams\":{\"2\":23, \"1\":11}},
42
+ {\"id\":\"012312333\",\"exams\":{\"1\":22, \"2\":40}}
43
+ ],
44
+ \"error\":[]}", :headers => {})
45
+
46
+ stub_request(:post, /.*581259.2012.K.K.1\/students.*/).
47
+ with(:body => "{\"students\":[{\"id\":\"014475359\",\"exercises\":{\"2\":23,\"1\":11}},{\"id\":\"012312333\",\"exercises\":{\"1\":22,\"2\":40}}]}",
48
+ :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'108', 'Content-Type'=>'application/json', 'Host'=>'test'}).
49
+ to_return(:status => 200, :body => "{\"success\":[
50
+ {\"id\":\"014475359\", \"exercises\":{\"2\":23, \"1\":11}}],
51
+ \"error\":[{\"id\":\"012312333\", \"exercises\":{\"1\":22, \"2\":40}}]}", :headers => {})
52
+
53
+
37
54
  end
38
55
 
39
56
  describe "GET" do
@@ -71,7 +88,7 @@ describe Kurki do
71
88
  hash = Kurki.parse_students(["01293492","18239123","812383172"])
72
89
  expect(hash).to have_key("students")
73
90
  expect(hash["students"].size).to eq(3)
74
- expect(hash["students"].first).to eq({"id": "01293492"})
91
+ expect(hash["students"].first).to eq({id: "01293492"})
75
92
  end
76
93
 
77
94
  it "can parse grades array correctly" do
@@ -79,7 +96,7 @@ describe Kurki do
79
96
  expect(hash).to have_key("students")
80
97
  expect(hash["students"].size).to eq 3
81
98
  expect(hash["students"].is_a? Array)
82
- expect(hash["students"].first).to eq({id:"01293492", "arvosana": "3"})
99
+ expect(hash["students"].first).to eq({id:"01293492", arvosana: "3"})
83
100
  end
84
101
 
85
102
  it "can POST students to a course" do
@@ -106,5 +123,37 @@ describe Kurki do
106
123
  )
107
124
  )
108
125
  end
126
+
127
+ it "can parse exam points correctly" do
128
+ hash = Kurki.parse_exam_points({"01293492" => {"2" =>23, "1"=>11}, "012312333" => {"1"=>22, "2"=>40} })
129
+ expect(hash).to have_key("students")
130
+ expect(hash["students"].size).to eq 2
131
+ expect(hash["students"].is_a? Array)
132
+ expect(hash["students"].first).to have_key(:id)
133
+ expect(hash["students"].first[:id]).to eq("01293492")
134
+ expect(hash["students"][1][:exams]).to eq({"1"=>22, "2"=>40})
135
+ end
136
+
137
+ it "can POST exam points from a hash" do
138
+ exam_points = {"01293492" => {"2" =>23, "1"=>11}, "012312333" => {"1"=>22, "2"=>40} }
139
+ ans = Kurki.set_exam_points("581259.2012.K.K.1", exam_points)
140
+ expect(ans). to eq(
141
+ JSON.parse("{\"success\":[
142
+ {\"id\":\"01293492\",\"exams\":{\"2\":23, \"1\":11}},
143
+ {\"id\":\"012312333\",\"exams\":{\"1\":22, \"2\":40}}
144
+ ],
145
+ \"error\":[]}" )
146
+ )
147
+ end
148
+
149
+ it "can POST exercise points from a hash" do
150
+ exercise_points = {"014475359" => {"2" =>23, "1"=>11}, "012312333" => {"1"=>22, "2"=>40} }
151
+ ans = Kurki.set_exercise_points("581259.2012.K.K.1", exercise_points)
152
+ resp = "{\"success\":[
153
+ {\"id\":\"014475359\", \"exercises\":{\"2\":23, \"1\":11}}],
154
+ \"error\":[{\"id\":\"012312333\", \"exercises\":{\"1\":22, \"2\":40}}]}"
155
+ expect(ans). to eq(JSON.parse(resp))
156
+ end
157
+
109
158
  end
110
159
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kurki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chang Rajani
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-09 00:00:00.000000000 Z
12
+ date: 2017-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler