kobra_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.MD ADDED
@@ -0,0 +1,4 @@
1
+ KOBRA Client
2
+ ============
3
+
4
+ Simple client for KOBRA.
@@ -0,0 +1,46 @@
1
+ require 'json/pure'
2
+ require 'open-uri'
3
+ require 'rest_client'
4
+
5
+ module Kobra
6
+ class Client
7
+
8
+ class BadResponse < RuntimeError; end
9
+ class NotFound < RuntimeError; end
10
+ class AuthError < RuntimeError; end
11
+ class ServerError < RuntimeError; end
12
+
13
+ # Kobra::Client.new(:domain => "kobra.ks.liu.se", :username => "john", :api_key => "a3h93hu393")
14
+ def initialize(settings = {})
15
+ settings[:domain] ||= 'kobra.ks.liu.se'
16
+ @base_url = "http://#{settings[:username]}:#{settings[:api_key]}@#{settings[:domain]}/"
17
+ end
18
+
19
+ # get_student(:liu_id => 'johec890')
20
+ # Parameters: liu_id, email, personal_number, rfid_number, barcode_number
21
+ def get_student(parameters)
22
+ json_post('students/api.json', parameters)
23
+ end
24
+
25
+ private
26
+
27
+ # Posts to path with parameters, returns JSON with symbols
28
+ def json_post(path, parameters = {})
29
+ # Make sure parameters is a Hash
30
+ parameters = {}.merge(parameters)
31
+ url = @base_url + path
32
+
33
+ response = RestClient.post(url, parameters)
34
+
35
+ JSON.parse(response.body, :symbolize_names => true)
36
+ rescue JSON::ParserError
37
+ raise BadResponse, "Can't parse JSON"
38
+ rescue RestClient::ResourceNotFound
39
+ raise NotFound, "Resource not found"
40
+ rescue RestClient::Unauthorized
41
+ raise AuthError, "Authentification failed"
42
+ rescue RestClient::InternalServerError
43
+ raise ServerError, "Server error"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ HTTP/1.1 401 Unauthorized
2
+ Date: Wed, 15 Jun 2011 14:24:56 GMT
3
+ Status: 401 Unauthorized
4
+ WWW-Authenticate: Basic realm="Application"
5
+ Content-Type: text/html; charset=utf-8
6
+ Cache-Control: no-cache
7
+ X-UA-Compatible: IE=Edge,chrome=1
8
+ X-Runtime: 0.005396
9
+ Via: 1.1 kobra.ks.liu.se
10
+ Transfer-Encoding: chunked
11
+
12
+ HTTP Basic: Access denied.
@@ -0,0 +1,10 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Wed, 15 Jun 2011 09:57:52 GMT
3
+ Status: 200 OK
4
+ Content-Type: application/json; charset=utf-8
5
+ ETag: "115ea511d19e8f40b9fbbb47b5a2bc0f"
6
+ Cache-Control: max-age=0, private, must-revalidate
7
+ Via: 1.1 kobra.ks.liu.se
8
+ Transfer-Encoding: chunked
9
+
10
+ ocked":null,"last_name":"DOE","email":"johdoXXX@student.liu.se","first_name":"JOHN","personal_number":"887711-0011","rfid_number":"9999999999","barcode_number":"888888888888888","union":"LinTek","liu_id:"johdoXXX"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 500 Internal Server Error
2
+ Server: nginx/0.8.53
3
+ Date: Wed, 15 Jun 2011 14:38:47 GMT
4
+ Content-Type: text/html
5
+ Connection: keep-alive
6
+ Keep-Alive: timeout=20
7
+ Content-Length: 27213
8
+
@@ -0,0 +1,10 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Wed, 15 Jun 2011 09:57:52 GMT
3
+ Status: 200 OK
4
+ Content-Type: application/json; charset=utf-8
5
+ ETag: "115ea511d19e8f40b9fbbb47b5a2bc0f"
6
+ Cache-Control: max-age=0, private, must-revalidate
7
+ Via: 1.1 kobra.ks.liu.se
8
+ Transfer-Encoding: chunked
9
+
10
+ {"blocked":null,"last_name":"DOE","email":"johdoXXX@student.liu.se","first_name":"JOHN","personal_number":"887711-0011","rfid_number":"9999999999","barcode_number":"888888888888888","union":"LinTek","liu_id":"johdoXXX"}
@@ -0,0 +1,10 @@
1
+ HTTP/1.1 404 Not Found
2
+ Date: Wed, 15 Jun 2011 14:25:31 GMT
3
+ Status: 404 Not Found
4
+ Content-Type: text/html; charset=utf-8
5
+ Cache-Control: no-cache
6
+ X-UA-Compatible: IE=Edge,chrome=1
7
+ X-Runtime: 0.051747
8
+ Via: 1.1 kobra.ks.liu.se
9
+ Transfer-Encoding: chunked
10
+
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+ require 'kobra/client'
3
+
4
+ describe Kobra::Client do
5
+ describe '#get_student' do
6
+ it 'obtains student information by email' do
7
+ stub_request(:post, "http://john:a9b8c7@kobra.ks.liu.se/students/api.json").
8
+ with(:body => "email=johdoXXX%40student.liu.se").
9
+ to_return(fixture('student_found.txt'))
10
+
11
+ kobra = Kobra::Client.new(:username => 'john', :api_key => 'a9b8c7')
12
+ student = kobra.get_student(:email => 'johdoXXX@student.liu.se')
13
+
14
+ student[:email].should == 'johdoXXX@student.liu.se'
15
+ student[:liu_id].should == 'johdoXXX'
16
+ student[:rfid_number].should == '9999999999'
17
+ student[:first_name].should == 'JOHN'
18
+ student[:last_name].should == 'DOE'
19
+ student[:blocked].should == nil
20
+ student[:barcode_number].should == '888888888888888'
21
+ student[:union].should == 'LinTek'
22
+ student[:personal_number].should == '887711-0011'
23
+ end
24
+
25
+ it 'fails to obtain student information by email' do
26
+ stub_request(:post, "http://john:a9b8c7@kobra.ks.liu.se/students/api.json").
27
+ with(:body => "email=johdoXXX%40student.liu.se").
28
+ to_return(fixture('student_not_found.txt'))
29
+
30
+ kobra = Kobra::Client.new(:username => 'john', :api_key => 'a9b8c7')
31
+ expect { kobra.get_student(:email => 'johdoXXX@student.liu.se') }.to raise_error Kobra::Client::NotFound
32
+ end
33
+
34
+ it 'fails to authorize' do
35
+ stub_request(:post, "http://john:wrong@kobra.ks.liu.se/students/api.json").
36
+ with(:body => "email=johdoXXX%40student.liu.se").
37
+ to_return(fixture('auth_failure.txt'))
38
+
39
+ kobra = Kobra::Client.new(:username => 'john', :api_key => 'wrong')
40
+ expect { kobra.get_student(:email => 'johdoXXX@student.liu.se') }.to raise_error Kobra::Client::AuthError
41
+ end
42
+
43
+ it 'gets garbage' do
44
+ stub_request(:post, "http://john:a9b8c7@kobra.ks.liu.se/students/api.json").
45
+ with(:body => "email=johdoXXX%40student.liu.se").
46
+ to_return(fixture('garbage.txt'))
47
+
48
+ kobra = Kobra::Client.new(:username => 'john', :api_key => 'a9b8c7')
49
+ expect { kobra.get_student(:email => 'johdoXXX@student.liu.se') }.to raise_error Kobra::Client::BadResponse
50
+ end
51
+
52
+ it 'gets server error' do
53
+ stub_request(:post, "http://john:a9b8c7@kobra.ks.liu.se/students/api.json").
54
+ with(:body => "email=johdoXXX%40student.liu.se").
55
+ to_return(fixture('server_error.txt'))
56
+
57
+ kobra = Kobra::Client.new(:username => 'john', :api_key => 'a9b8c7')
58
+ expect { kobra.get_student(:email => 'johdoXXX@student.liu.se') }.to raise_error Kobra::Client::ServerError
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+ require 'webmock/rspec'
5
+
6
+ def fixture_path
7
+ File.expand_path(File.join('..', 'fixtures'), __FILE__)
8
+ end
9
+
10
+ def fixture(file)
11
+ File.new(File.join(fixture_path, file))
12
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kobra_client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Johan Eckerstroem
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-15 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json_pure
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rake
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: webmock
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: Used for access to KOBRA, API-account needed
72
+ email: johan@duh.se
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - README.MD
79
+ files:
80
+ - lib/kobra/client.rb
81
+ - spec/fixtures/auth_failure.txt
82
+ - spec/fixtures/garbage.txt
83
+ - spec/fixtures/server_error.txt
84
+ - spec/fixtures/student_found.txt
85
+ - spec/fixtures/student_not_found.txt
86
+ - spec/kobra_client_spec.rb
87
+ - spec/spec_helper.rb
88
+ - README.MD
89
+ has_rdoc: true
90
+ homepage: http://kobra.ks.liu.se/
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.6.2
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Client library for KOBRA.
117
+ test_files: []
118
+