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 +4 -4
- data/lib/kurki.rb +63 -23
- data/lib/kurki/version.rb +1 -1
- data/spec/kurki_spec.rb +51 -2
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: dfd88f581b6697927541b0bd0271fd001cb6e3bf
         | 
| 4 | 
            +
              data.tar.gz: 50daa7d8c536c2d16379ac220e23693cd7ecdd4f
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 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 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 71 | 
            +
              def self.parse_exercise_points(points)
         | 
| 72 | 
            +
                parse(points, lambda {|x,y| {id: x, exercises: y}})
         | 
| 73 | 
            +
              end
         | 
| 50 74 |  | 
| 51 | 
            -
             | 
| 52 | 
            -
                  parse( | 
| 53 | 
            -
             | 
| 75 | 
            +
              def self.parse_exam_points(points)
         | 
| 76 | 
            +
                  parse(points, lambda {|x,y| { id: x, exams: y } })
         | 
| 77 | 
            +
              end
         | 
| 54 78 |  | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 79 | 
            +
              def self.read_csv(file_path, delimiter)
         | 
| 80 | 
            +
                CSV.read(file_path, col_sep: delimiter).drop(1)
         | 
| 81 | 
            +
              end
         | 
| 58 82 |  | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
                  json
         | 
| 63 | 
            -
                end
         | 
| 83 | 
            +
              def self.parse_students(student_ids)
         | 
| 84 | 
            +
                parse(student_ids, lambda { |x| {id: x} })
         | 
| 85 | 
            +
              end
         | 
| 64 86 |  | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 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 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
                 | 
| 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
    
    
    
        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({ | 
| 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",  | 
| 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. | 
| 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:  | 
| 12 | 
            +
            date: 2017-10-22 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: bundler
         |