shift_planning 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca87d9153c5e9db23c2e970284df203b53bfb3be
4
- data.tar.gz: c9dcecebb9c5b77e56e24b7fa166b0ca5b36bef4
3
+ metadata.gz: 336bfa5c60eae23b1b4f90198d2f009cc0a3dd95
4
+ data.tar.gz: 97642f94520ee1b5ff78516fd4c925fdfc2801e7
5
5
  SHA512:
6
- metadata.gz: 922f34965feeb62c14e1753d5984e32038548172dc5d6e24ec0846ab2dbd6a8965215cac61b62ab92c640341d8c7bf443ed3bfd1b20affcc942015075ccd18cf
7
- data.tar.gz: 972218116bf791697af08555903f390527d82a9bb98404f74ef40f0abfcf6fa96a0b68b3e8cce5ea39f36be216bc1a99fee41c221b077b7c97b8ab75cbd444ee
6
+ metadata.gz: fbe92410be5dd32f5c06a81a4364c3634dad1e4fee96cb77bb406a9a5fe60f007d917df9617bb15b4c2b09518e0b24ffab4515d84bd15cb59f9364b0557b015b
7
+ data.tar.gz: 49d100494d4b9922e325a9f2bf3789d89f18d0f931e3daa896f0b5e219e8f364e87d8c2b0174cb997a1fd25502220eb4319bad4e1a076a2803ccd42bcd7967c8
data/.gitignore CHANGED
File without changes
data/.ruby-version CHANGED
File without changes
data/.travis.yml CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
@@ -1,52 +1,53 @@
1
1
  require "shift_planning/version"
2
+ require "shift_planning/api_error"
2
3
  require "shift_planning/client"
3
4
 
4
5
  module ShiftPlanning
5
6
  extend self
6
7
 
7
8
  def init(options={})
8
- @@client = Client.new(options)
9
+ @@client = ShiftPlanning::Client.new(options)
9
10
  end
10
11
 
11
12
  def get(api_module, request={})
12
- @@client.get(api_module, request)
13
+ @@client.request("GET", api_module, request)
13
14
  end
14
15
 
15
16
  def create(api_module, request={})
16
- @@client.create(api_module, request)
17
+ @@client.request("CREATE", api_module, request)
17
18
  end
18
19
 
19
20
  def update(api_module, request={})
20
- @@client.update(api_module, request)
21
+ @@client.request("UPDATE", api_module, request)
21
22
  end
22
23
 
23
24
  def delete(api_module, request={})
24
- @@client.delete(api_module, request)
25
+ @@client.request("DELETE", api_module, request)
25
26
  end
26
27
 
27
- # convience methods
28
+ # convenience methods
28
29
 
29
30
  def skills
30
- @@client.get('staff.skills')
31
+ get('staff.skills')
31
32
  end
32
33
 
33
34
  def employees
34
- @@client.get('staff.employees')
35
+ get('staff.employees')
35
36
  end
36
37
 
37
38
  def employee(employee_id)
38
- @@client.get('staff.employee', "id" => employee_id)
39
+ get('staff.employee', "id" => employee_id)
39
40
  end
40
41
 
41
42
  def add_skill(employee_id, skill_id)
42
- @@client.update('staff.employee', {
43
+ update('staff.employee', {
43
44
  "id" => employee_id,
44
45
  "addskill" => skill_id
45
46
  })
46
47
  end
47
48
 
48
49
  def remove_skill(employee_id, skill_id)
49
- @@client.update('staff.employee', {
50
+ update('staff.employee', {
50
51
  "id" => employee_id,
51
52
  "removeskill" => skill_id
52
53
  })
File without changes
@@ -1,17 +1,22 @@
1
1
  require "http"
2
2
  require "json"
3
3
  require "base64"
4
- require_relative "api_error"
5
4
 
6
5
  module ShiftPlanning
7
6
  class Client
7
+ attr_accessor :strict
8
+
8
9
  def initialize(config = {})
9
- raise ArgumentError.new('Missing username') unless config.key? :username
10
+ raise ArgumentError.new('Missing username') unless config.key?(:username)
10
11
  @username = config[:username]
11
- raise ArgumentError.new('Missing password') unless config.key? :password
12
+
13
+ raise ArgumentError.new('Missing password') unless config.key?(:password)
12
14
  @password = config[:password]
13
- raise ArgumentError.new('Missing api key') unless config.key? :key
15
+
16
+ raise ArgumentError.new('Missing api key') unless config.key?(:key)
14
17
  @key = config[:key]
18
+
19
+ @strict = config.key?(:strict) ? config[:strict] : true
15
20
  @url = 'http://www.shiftplanning.com/api/'
16
21
  @headers = {
17
22
  "Content-Type" => "application/x-www-form-urlencoded"
@@ -36,24 +41,6 @@ module ShiftPlanning
36
41
  @token = JSON.parse(response)["token"]
37
42
  end
38
43
 
39
- def get(api_module, request={})
40
- request("GET", api_module, request)
41
- end
42
-
43
- def create(api_module, request={})
44
- request("CREATE", api_module, request)
45
- end
46
-
47
- def update(api_module, request={})
48
- request("UPDATE", api_module, request)
49
- end
50
-
51
- def delete(api_module, request={})
52
- request("DELETE", api_module, request)
53
- end
54
-
55
- private
56
-
57
44
  def request(method, api_module, request)
58
45
  authenticate unless authenticated?
59
46
 
@@ -65,7 +52,7 @@ module ShiftPlanning
65
52
  })
66
53
  response = HTTP.with(@headers).post(@url, body)
67
54
  result = JSON.parse(response)
68
- raise ApiError.new(result) if is_error_response? result
55
+ raise ApiError.new(result) if @strict && is_error_response?(result)
69
56
  result
70
57
  end
71
58
 
@@ -1,3 +1,3 @@
1
1
  module ShiftPlanning
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
File without changes
data/test/test_helper.rb CHANGED
File without changes
@@ -1,37 +1,35 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe 'ApiError' do
4
- describe '#constructor' do
5
- def check(status, message)
6
- error = ShiftPlanning::ApiError.new("status" => status)
7
- assert_equal message, error.message
8
- end
4
+ it "Maps status codes" do
5
+ assert_status_code(-3, "Flagged API Key - Pemanently Banned")
6
+ assert_status_code(-2, "Flagged API Key - Too Many invalid access attempts - contact us")
7
+ assert_status_code(-1, "Flagged API Key - Temporarily Disabled - contact us")
8
+ assert_status_code(1, "Success -")
9
+ assert_status_code(2, "Invalid API key - App must be granted a valid key by ShiftPlanning")
10
+ assert_status_code(3, "Invalid token key - Please re-authenticate")
11
+ assert_status_code(4, "Invalid Method - No Method with that name exists in our API")
12
+ assert_status_code(5, "Invalid Module - No Module with that name exists in our API")
13
+ assert_status_code(6, "Invalid Action - No Action with that name exists in our API")
14
+ assert_status_code(7, "Authentication Failed - You do not have permissions to access the service")
15
+ assert_status_code(8, "Missing parameters - Your request is missing a required parameter")
16
+ assert_status_code(9, "Invalid parameters - Your request has an invalid parameter type")
17
+ assert_status_code(10, "Extra parameters - Your request has an extra/unallowed parameter type")
18
+ assert_status_code(12, "Create Failed - Your CREATE request failed")
19
+ assert_status_code(13, "Update Failed - Your UPDATE request failed")
20
+ assert_status_code(14, "Delete Failed - Your DELETE request failed")
21
+ assert_status_code(15, "Get Failed - Your GET request failed")
22
+ assert_status_code(20, "Incorrect Permissions - You don't have the proper permissions to access this")
23
+ assert_status_code(90, "Suspended API key - Access for your account has been suspended, please contact ShiftPlanning")
24
+ assert_status_code(91, "Throttle exceeded - You have exceeded the max allowed requests. Try again later.")
25
+ assert_status_code(98, "Bad API Paramaters - Invalid POST request. See Manual.")
26
+ assert_status_code(99, "Service Offline - This service is temporarily offline. Try again later.")
27
+ assert_status_code(100, "Can not connect to LDAP - host or port are incorect")
28
+ assert_status_code(101, "Can not connect to LDAP - username or password are incorrect")
29
+ end
9
30
 
10
- it "handles api status" do
11
- check(-3, "Flagged API Key - Pemanently Banned")
12
- check(-2, "Flagged API Key - Too Many invalid access attempts - contact us")
13
- check(-1, "Flagged API Key - Temporarily Disabled - contact us")
14
- check(1, "Success -")
15
- check(2, "Invalid API key - App must be granted a valid key by ShiftPlanning")
16
- check(3, "Invalid token key - Please re-authenticate")
17
- check(4, "Invalid Method - No Method with that name exists in our API")
18
- check(5, "Invalid Module - No Module with that name exists in our API")
19
- check(6, "Invalid Action - No Action with that name exists in our API")
20
- check(7, "Authentication Failed - You do not have permissions to access the service")
21
- check(8, "Missing parameters - Your request is missing a required parameter")
22
- check(9, "Invalid parameters - Your request has an invalid parameter type")
23
- check(10, "Extra parameters - Your request has an extra/unallowed parameter type")
24
- check(12, "Create Failed - Your CREATE request failed")
25
- check(13, "Update Failed - Your UPDATE request failed")
26
- check(14, "Delete Failed - Your DELETE request failed")
27
- check(15, "Get Failed - Your GET request failed")
28
- check(20, "Incorrect Permissions - You don't have the proper permissions to access this")
29
- check(90, "Suspended API key - Access for your account has been suspended, please contact ShiftPlanning")
30
- check(91, "Throttle exceeded - You have exceeded the max allowed requests. Try again later.")
31
- check(98, "Bad API Paramaters - Invalid POST request. See Manual.")
32
- check(99, "Service Offline - This service is temporarily offline. Try again later.")
33
- check(100, "Can not connect to LDAP - host or port are incorect")
34
- check(101, "Can not connect to LDAP - username or password are incorrect")
35
- end
31
+ def assert_status_code(status, message)
32
+ error = ShiftPlanning::ApiError.new("status" => status)
33
+ assert_equal message, error.message
36
34
  end
37
35
  end
@@ -8,8 +8,8 @@ describe 'Client' do
8
8
  :key => 'e145a81787a46fc24802f1626befb20dcd76fd7b'
9
9
  })
10
10
  stub_request(:post, "http://www.shiftplanning.com/api/")
11
- .with(:body => {"data"=>"{\"key\":\"e145a81787a46fc24802f1626befb20dcd76fd7b\",\"request\":{\"module\":\"staff.login\",\"method\":\"GET\",\"username\":\"devapi\",\"password\":\"password\"}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
12
- .to_return(:status => 200, :body => "{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\"}", :headers => {})
11
+ .with(:body => {"data"=>"{\"key\":\"e145a81787a46fc24802f1626befb20dcd76fd7b\",\"request\":{\"module\":\"staff.login\",\"method\":\"GET\",\"username\":\"devapi\",\"password\":\"password\"}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
12
+ .to_return(:status => 200, :body => "{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\"}", :headers => {})
13
13
  end
14
14
 
15
15
  describe '#initialize' do
@@ -17,21 +17,21 @@ describe 'Client' do
17
17
  err = -> {
18
18
  ShiftPlanning::Client.new(:password => '', :key => '')
19
19
  }.must_raise ArgumentError
20
- err.message.must_match /username/
20
+ err.message.must_match(/username/)
21
21
  end
22
22
 
23
23
  it 'password is a required field' do
24
24
  err = -> {
25
25
  ShiftPlanning::Client.new(:username => '', :key => '')
26
26
  }.must_raise ArgumentError
27
- err.message.must_match /password/
27
+ err.message.must_match(/password/)
28
28
  end
29
29
 
30
30
  it 'key is a required field' do
31
31
  err = -> {
32
32
  ShiftPlanning::Client.new(:username => '', :password => '')
33
33
  }.must_raise ArgumentError
34
- err.message.must_match /key/
34
+ err.message.must_match(/key/)
35
35
  end
36
36
  end
37
37
 
@@ -42,27 +42,31 @@ describe 'Client' do
42
42
  end
43
43
  end
44
44
 
45
- describe '#get' do
46
- it 'authenticates and makes api request' do
45
+ describe '#request' do
46
+ it 'authenticates and posts request body' do
47
47
  stub_request(:post, "http://www.shiftplanning.com/api/")
48
48
  .with(:body => {"data"=>"{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\",\"method\":\"GET\",\"module\":\"staff.employee\",\"request\":{\"id\":1}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
49
49
  .to_return(:status => 200, :body => "{\"status\":1, \"id\":\"1\"}", :headers => {})
50
- employee = @client.get('staff.employee', "id" => 1)
50
+ employee = @client.request('GET', 'staff.employee', "id" => 1)
51
51
  assert 1, employee[:id]
52
52
  end
53
- end
54
53
 
55
- describe '#update' do
56
- it 'authenticates and makes api request' do
54
+ it 'raises errors on failure code' do
57
55
  stub_request(:post, "http://www.shiftplanning.com/api/")
58
- .with(:body => {"data"=>"{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\",\"method\":\"UPDATE\",\"module\":\"staff.employee\",\"request\":{\"id\":1,\"addskill\":2}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
59
- .to_return(:status => 200, :body => "{\"status\":1, \"id\":\"1\"}", :headers => {})
56
+ .with(:body => {"data"=>"{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\",\"method\":\"GET\",\"module\":\"staff.employee\",\"request\":{\"id\":1}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
57
+ .to_return(:status => 200, :body => "{\"status\":8}", :headers => {})
58
+ err = -> {
59
+ @client.request('GET', 'staff.employee', "id" => 1)
60
+ }.must_raise ShiftPlanning::ApiError
61
+ err.message.must_match(/Missing parameters/)
62
+ end
60
63
 
61
- employee = @client.update('staff.employee', {
62
- "id" => 1,
63
- "addskill" => 2
64
- })
65
- assert 1, employee[:id]
64
+ it 'ignores errors when strict is false' do
65
+ @client.strict = false
66
+ stub_request(:post, "http://www.shiftplanning.com/api/")
67
+ .with(:body => {"data"=>"{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\",\"method\":\"GET\",\"module\":\"staff.employee\",\"request\":{\"id\":1}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
68
+ .to_return(:status => 200, :body => "{\"status\":8}", :headers => {})
69
+ @client.request('GET', 'staff.employee', "id" => 1)
66
70
  end
67
71
  end
68
72
  end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ describe 'ShiftPlanning' do
4
+ before do
5
+ ShiftPlanning.init({
6
+ :strict => false,
7
+ :username => 'devapi',
8
+ :password => 'password',
9
+ :key => 'e145a81787a46fc24802f1626befb20dcd76fd7b'
10
+ })
11
+ stub_request(:post, "http://www.shiftplanning.com/api/")
12
+ .with(:body => {"data"=>"{\"key\":\"e145a81787a46fc24802f1626befb20dcd76fd7b\",\"request\":{\"module\":\"staff.login\",\"method\":\"GET\",\"username\":\"devapi\",\"password\":\"password\"}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
13
+ .to_return(:status => 200, :body => "{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\"}", :headers => {})
14
+ end
15
+
16
+ describe '#get' do
17
+ it 'authenticates and makes api request' do
18
+ stub_request(:post, "http://www.shiftplanning.com/api/")
19
+ .with(:body => {"data"=>"{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\",\"method\":\"GET\",\"module\":\"staff.employee\",\"request\":{\"id\":1}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
20
+ .to_return(:status => 200, :body => "{\"status\":1, \"id\":\"1\"}", :headers => {})
21
+ employee = ShiftPlanning.get('staff.employee', "id" => 1)
22
+ assert 1, employee[:id]
23
+ end
24
+ end
25
+
26
+ describe '#update' do
27
+ it 'authenticates and makes api request' do
28
+ stub_request(:post, "http://www.shiftplanning.com/api/")
29
+ .with(:body => {"data"=>"{\"token\":\"1714d482a0f3a5e3472fb51c481dc571fd6724e1\",\"method\":\"UPDATE\",\"module\":\"staff.employee\",\"request\":{\"id\":1,\"addskill\":2}}"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Host'=>'www.shiftplanning.com', 'User-Agent'=>'RubyHTTPGem/0.5.0'})
30
+ .to_return(:status => 200, :body => "{\"status\":1, \"id\":\"1\"}", :headers => {})
31
+
32
+ employee = ShiftPlanning.update('staff.employee', {
33
+ "id" => 1,
34
+ "addskill" => 2
35
+ })
36
+ assert 1, employee[:id]
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shift_planning
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Mercier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-26 00:00:00.000000000 Z
11
+ date: 2014-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -102,6 +102,7 @@ files:
102
102
  - test/test_helper.rb
103
103
  - test/unit/api_error_test.rb
104
104
  - test/unit/client_test.rb
105
+ - test/unit/shift_planning_test.rb
105
106
  homepage: ''
106
107
  licenses:
107
108
  - MIT
@@ -130,3 +131,4 @@ test_files:
130
131
  - test/test_helper.rb
131
132
  - test/unit/api_error_test.rb
132
133
  - test/unit/client_test.rb
134
+ - test/unit/shift_planning_test.rb