kurki 0.0.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a30102744bcc5b5daec6421831454dcaf80aa731
4
- data.tar.gz: ce94b77b00a206fcd274da51ae924f473644194c
3
+ metadata.gz: ead63983b995b93ce4704249d06579fe9fe03df3
4
+ data.tar.gz: 63c6cae4d423d7fe15a1887b1f1ee1c28f031739
5
5
  SHA512:
6
- metadata.gz: b6049b9b6ec060cc7ea59cddb97c1b7a0821ce2fb84a514a817561daf718ba40d5c7d32999328b63f46e2ca9e7e02eb51393cc03b0ad679b96aadd2226b714ed
7
- data.tar.gz: 9d8750a54d873198e62699391f0ccbeb70b3e1c2befa1e325553b006d47709eb8576c64c7df1ac5104eec482c329f082c3504a2928f11c53829992a810bef6f2
6
+ metadata.gz: 0670108ce07b761db746ecd550bc383b58acc90e4c6985925fc68e00823250bcf61c674be9e754cf624bc4cfefcc66983104721eca89824ef9b61a769f3d49b0
7
+ data.tar.gz: f3598f0e3b4c3a8d944c865e28163cc7fdaa0df1a80e37c6c8d09dfdbf444a3a73e66c58820ec720e482ee60d2410ec732119c105194cc8f3742568dbfb1993a
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.2.0"
4
+ # uncomment this line if your project needs to run something other than `rake`:
5
+ script: bundle exec rspec spec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Kurki
1
+ # Kurki [![Build Status](https://travis-ci.org/Coolnesss/kurki.svg?branch=master)](https://travis-ci.org/Coolnesss/kurki) [![Code Climate](https://codeclimate.com/github/Coolnesss/kurki/badges/gpa.svg)](https://codeclimate.com/github/Coolnesss/kurki)
2
2
 
3
3
  Helsinki University Kurki system API Rubygem.
4
4
 
@@ -8,6 +8,11 @@ Currently supports GET requests to:
8
8
  * Fetch a single course based on ID
9
9
  * Fetch all students of a single course
10
10
 
11
+ And POST requests to:
12
+
13
+ * Add students to a course
14
+ * Add grades to students of a course
15
+
11
16
  Requires API token set in environment variable named KURKI_TOKEN
12
17
 
13
18
  ## Installation
@@ -28,21 +33,45 @@ Or install it yourself as:
28
33
 
29
34
  ## Usage
30
35
 
36
+ ### GET methods
37
+
31
38
  ```ruby
32
- Kurki.get_courses
39
+ irb(main):002:0> Kurki.url = "http://localhost:1234"
40
+ => "http://localhost:1234"
41
+ irb(main):003:0> Kurki.url
42
+ => "http://localhost:1234/"
33
43
  ```
34
44
 
35
45
  ```ruby
36
- Kurki.get_course(id)
46
+ irb(main):002:0> Kurki.get_courses
47
+ => [{"kurssikoodi"=>"581259", "lukuvuosi"=>2012, "lukukausi"=>"K", "tyyppi"=>"K", "tila"=>"J", "kurssi_nro"=>1, "nimi"=>"Ohjelmistotuotanto", "alkamis_pvm"=>"2012-03-12 00:00:00 +0300", "paattymis_pvm"=>"2016-04-30 00:00:00 +0300", "url"=>"localhost:1234/courses/581259.2012.K.K.1"}, {"kurssikoodi"=>"58131", "lukuvuosi"=>2010, "lukukausi"=>"K", "tyyppi"=>"K", "tila"=>"J", "kurssi_nro"=>1, "nimi"=>"Tietorakenteet", "alkamis_pvm"=>"2010-01-19 00:00:00 +0300", "paattymis_pvm"=>"2016-04-30 00:00:00 +0300", "url"=>"localhost:1234/courses/58131.2010.K.K.1"}, {"kurssikoodi"=>"58131", "lukuvuosi"=>2011, "lukukausi"=>"K", "tyyppi"=>"K", "tila"=>"J", "kurssi_nro"=>1, "nimi"=>"Tietorakenteet", "alkamis_pvm"=>"2011-01-17 00:00:00 +0300", "paattymis_pvm"=>"2016-04-30 00:00:00 +0300", "url"=>"localhost:1234/courses/58131.2011.K.K.1"}]
48
+ ```
49
+
50
+ ```ruby
51
+ irb(main):003:0> Kurki.get_course("581259.2012.K.K.1")
52
+ => {"kurssikoodi"=>"581259", "lukuvuosi"=>2012, "lukukausi"=>"K", "tyyppi"=>"K", "kurssi_nro"=>1, "alkamis_pvm"=>"2012-03-12 00:00:00 +0300", "paattymis_pvm"=>"2016-04-30 00:00:00 +0300", "omistaja"=>"LUUKKAINEN_M"}
53
+ ```
54
+
55
+ ```ruby
56
+ irb(main):004:0> Kurki.get_students("581259.2012.K.K.1")
57
+ => [{"arvosana"=>"3", "etunimi"=>"LASSE", "sukunimi"=>"TOIVONEN", "id"=>"012345678"}, {"arvosana"=>"5", "etunimi"=>"TERO V E", "sukunimi"=>"SANKARI", "id"=>"012435443"}]
58
+ ```
59
+
60
+ ### POST methods
61
+
62
+ ```ruby
63
+ irb(main):008:0> Kurki.set_students("58131.2010.K.K.1", ["012345678"])
64
+ => {"success"=>[{"id"=>"012345678"}], "error"=>[]}
37
65
  ```
38
66
 
39
67
  ```ruby
40
- Kurki.get_students(course_id)
68
+ irb(main):006:0> Kurki.set_grades("58131.2011.K.K.1", {"013595141" => "5"})
69
+ => {"success"=>[{"id"=>"013595141", "arvosana"=>"5"}], "error"=>[]}
41
70
  ```
42
71
 
43
72
  ## Contributing
44
73
 
45
- 1. Fork it ( https://github.com/[my-github-username]/kurki/fork )
74
+ 1. Fork it ( https://github.com/coolnesss/kurki/fork )
46
75
  2. Create your feature branch (`git checkout -b my-new-feature`)
47
76
  3. Commit your changes (`git commit -am 'Add some feature'`)
48
77
  4. Push to the branch (`git push origin my-new-feature`)
data/kurki.gemspec CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "webmock"
25
+ spec.add_development_dependency "codeclimate-test-reporter"
26
+
25
27
  spec.add_runtime_dependency 'rest-client'
26
28
 
27
29
  end
data/lib/kurki/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kurki
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/kurki.rb CHANGED
@@ -26,10 +26,40 @@ module Kurki
26
26
  get(url + "courses/#{id}").first
27
27
  end
28
28
 
29
+ # Student_ids in form ["12345678", "12345678"]
30
+ def self.set_students(course_id, student_ids)
31
+ post(@@url + "courses/#{course_id}/students", parse_students(student_ids))
32
+ end
33
+
34
+ # Grades in form {"01234567": "3", "01243483": "4"}
35
+ def self.set_grades(course_id, grades)
36
+ post(@@url + "courses/#{course_id}/students", parse_grades(grades))
37
+ end
38
+
29
39
  private
30
40
 
41
+ def self.parse_students(student_ids)
42
+ parse(student_ids, lambda { |x| {"id": x} })
43
+ end
44
+
45
+ def self.parse_grades(grades)
46
+ parse(grades, lambda {|x,y| { "id": x, "arvosana": y } })
47
+ end
48
+
49
+ def self.parse(items, exp)
50
+ json = {}
51
+ json["students"] = items.map(&exp)
52
+ json
53
+ end
54
+
55
+ def self.post(address, data, params = {})
56
+ return unless data
57
+ params[:authorization] = ENV["KURKI_TOKEN"]
58
+ JSON.parse(RestClient.post address, data.to_json, params: params, content_type: :json, accept: :json)
59
+ end
60
+
31
61
  def self.get(address, params = {})
32
62
  params[:authorization] = ENV["KURKI_TOKEN"]
33
- JSON.parse(RestClient.get(address, { params: params, accept: :json, content_type: :json }))
63
+ JSON.parse(RestClient.get address, { params: params, accept: :json, content_type: :json })
34
64
  end
35
65
  end
data/spec/kurki_spec.rb CHANGED
@@ -19,35 +19,81 @@ describe Kurki do
19
19
  with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
20
20
  to_return(:status => 200, :body => IO.read("spec/fixtures/students.json"), :headers => {})
21
21
 
22
- end
22
+ stub_request(:post, /.*581259.2012.K.K.1\/students.*/).
23
+ with(:body => "{\"students\":[{\"id\":\"012345678\"},{\"id\":\"01293492\"}]}",
24
+ :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'51', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
25
+ to_return(:status => 200, :body => "{\"success\":[{\"id\":\"012345678\"},{\"id\":\"01293492\"}],\"error\":[]}", :headers => {})
23
26
 
24
- it "allows setting of url" do
25
- Kurki.url = "test/"
26
- expect(Kurki.url).to eq("test/")
27
- end
27
+ stub_request(:post, /.*581259.2012.K.K.1\/students.*/).
28
+ with(:body => "{\"students\":[{\"id\":\"012345678\",\"arvosana\":\"4\"}]}",
29
+ :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'48', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
30
+ to_return(:status => 200, :body => "{\"success\":[{\"id\":\"012345678\",\"arvosana\":\"4\"}],\"error\":[]}", :headers => {})
28
31
 
29
- it "setting an url without a backslash adds it" do
30
- Kurki.url = "test"
31
- expect(Kurki.url).to eq("test/")
32
32
  end
33
33
 
34
- it "default url works" do
35
- expect(Kurki.url).not_to be_nil
36
- end
34
+ describe "GET" do
37
35
 
38
- it 'can GET the curses index' do
39
- courses = Kurki.get_courses
40
- expect(courses.first["kurssikoodi"]).to eq("581259")
41
- expect(courses.last["kurssikoodi"]).to eq("58131")
42
- end
36
+ it "allows setting of url" do
37
+ Kurki.url = "test/"
38
+ expect(Kurki.url).to eq("test/")
39
+ end
40
+
41
+ it "setting an url without a backslash adds it" do
42
+ Kurki.url = "test"
43
+ expect(Kurki.url).to eq("test/")
44
+ end
45
+
46
+ it "default url works" do
47
+ expect(Kurki.url).not_to be_nil
48
+ end
49
+
50
+ it 'can GET the curses index' do
51
+ courses = Kurki.get_courses
52
+ expect(courses.first["kurssikoodi"]).to eq("581259")
53
+ expect(courses.last["kurssikoodi"]).to eq("58131")
54
+ end
55
+
56
+ it 'can GET the a single course by id' do
57
+ course = Kurki.get_course("581259.2012.K.K.1")
58
+ expect(course["kurssikoodi"]).to eq("581259")
59
+ end
60
+
61
+ it 'can get GET students of a course' do
62
+ students = Kurki.get_students("581259.2012.K.K.1")
63
+ expect(students.first["arvosana"]).to eq("3")
64
+ end
43
65
 
44
- it 'can GET the a single course by id' do
45
- course = Kurki.get_course("581259.2012.K.K.1")
46
- expect(course["kurssikoodi"]).to eq("581259")
47
66
  end
48
67
 
49
- it 'can get GET students of a course' do
50
- students = Kurki.get_students("581259.2012.K.K.1")
51
- expect(students.first["arvosana"]).to eq("3")
68
+ describe "POST" do
69
+
70
+ it "can parse student array correctly" do
71
+ hash = Kurki.parse_students(["01293492","18239123","812383172"])
72
+ expect(hash).to have_key("students")
73
+ expect(hash["students"].size).to eq(3)
74
+ expect(hash["students"].first).to eq({"id": "01293492"})
75
+ end
76
+
77
+ it "can parse grades array correctly" do
78
+ hash = Kurki.parse_grades({"01293492" => "3","18239123" => "4","812383172" => "1"})
79
+ expect(hash).to have_key("students")
80
+ expect(hash["students"].size).to eq 3
81
+ expect(hash["students"].is_a? Array)
82
+ expect(hash["students"].first).to eq({id:"01293492", "arvosana": "3"})
83
+ end
84
+
85
+ it "can POST students to a course" do
86
+ students = ["012345678", "01293492"]
87
+ ans = Kurki.set_students("581259.2012.K.K.1", students)
88
+ expect(ans).to eq(
89
+ JSON.parse("{\"success\":[{\"id\":\"012345678\"},{\"id\":\"01293492\"}],\"error\":[]}"))
90
+ end
91
+
92
+ it "can POST grades to a course" do
93
+ grades = {"012345678" => "4"}
94
+ ans = Kurki.set_grades("581259.2012.K.K.1", grades)
95
+ expect(ans).to eq(
96
+ JSON.parse("{\"success\":[{\"id\":\"012345678\",\"arvosana\":\"4\"}],\"error\":[]}"))
97
+ end
52
98
  end
53
99
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'bundler/setup'
2
2
  require 'webmock/rspec'
3
-
3
+ require "codeclimate-test-reporter"
4
+ CodeClimate::TestReporter.start
4
5
  Bundler.setup
5
6
 
6
7
  require 'kurki' # and any other gems you need
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kurki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chang Rajani
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-01 00:00:00.000000000 Z
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rest-client
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -89,6 +103,7 @@ extra_rdoc_files: []
89
103
  files:
90
104
  - ".gitignore"
91
105
  - ".rspec"
106
+ - ".travis.yml"
92
107
  - Gemfile
93
108
  - LICENSE.txt
94
109
  - README.md