cobot_client 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -62,6 +62,12 @@ There is a module `CobotClient::UrlHelper`. After you include it you can call `c
62
62
  cobot_url('co-up', '/api/user') # => 'https://co-up.cobot.me/api/user'
63
63
  cobot_url('co-up', '/api/user', params: {x: 'y'}) # => 'https://co-up.cobot.me/api/user?x=y'
64
64
 
65
+ ### Calling the API
66
+
67
+ CobotClient::ApiClient.new('<access token>').list_resources('<subdomain>')
68
+
69
+ For more details see the specs.
70
+
65
71
  ## Contributing
66
72
 
67
73
  1. Fork it
data/cobot_client.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.add_dependency 'activesupport'
19
19
  gem.add_dependency 'virtus'
20
20
  gem.add_dependency 'oauth2'
21
+ gem.add_dependency 'rest-client'
22
+ gem.add_dependency 'json'
21
23
  gem.add_development_dependency 'rspec'
22
24
  gem.add_development_dependency 'rake'
23
25
  end
data/lib/cobot_client.rb CHANGED
@@ -2,8 +2,9 @@ require "cobot_client/version"
2
2
  require 'cobot_client/engine' if defined?(Rails)
3
3
 
4
4
  module CobotClient
5
- autoload :UrlHelper, 'cobot_client/url_helper'
5
+ autoload :ApiClient, 'cobot_client/api_client'
6
6
  autoload :NavigationLink, 'cobot_client/navigation_link'
7
7
  autoload :NavigationLinkService, 'cobot_client/navigation_link_service'
8
+ autoload :UrlHelper, 'cobot_client/url_helper'
8
9
  autoload :XdmHelper, 'cobot_client/xdm_helper'
9
10
  end
@@ -0,0 +1,55 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+
4
+ module CobotClient
5
+ class ApiClient
6
+ include UrlHelper
7
+
8
+ def initialize(access_token)
9
+ @access_token = access_token
10
+ end
11
+
12
+ def get_resources(subdomain)
13
+ get subdomain, '/resources'
14
+ end
15
+
16
+ def create_booking(subdomain, resource_id, attributes)
17
+ post subdomain, "/resources/#{resource_id}/bookings", attributes
18
+ end
19
+
20
+ def update_booking(subdomain, id, attributes)
21
+ put subdomain, "/bookings/#{id}", attributes
22
+ end
23
+
24
+ def delete_booking(subdomain, id)
25
+ delete subdomain, "/bookings/#{id}"
26
+ end
27
+
28
+ private
29
+
30
+ def put(subdomain, path, params)
31
+ JSON.parse RestClient.put(cobot_url(subdomain, "/api#{path}"), params, auth_headers).body,
32
+ symbolize_names: true
33
+ end
34
+
35
+ def delete(subdomain, path)
36
+ RestClient.delete(cobot_url(subdomain, "/api#{path}"), auth_headers)
37
+ end
38
+
39
+ def post(subdomain, path, params)
40
+ JSON.parse RestClient.post(cobot_url(subdomain, "/api#{path}"), params, auth_headers).body,
41
+ symbolize_names: true
42
+ end
43
+
44
+ def get(subdomain, path)
45
+ JSON.parse(
46
+ RestClient.get(cobot_url(subdomain, "/api#{path}"), auth_headers).body,
47
+ symbolize_names: true
48
+ )
49
+ end
50
+
51
+ def auth_headers
52
+ {'Authorization' => "Bearer #{@access_token}"}
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module CobotClient
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe CobotClient::ApiClient do
4
+ let(:api_client) { CobotClient::ApiClient.new('token-123') }
5
+ let(:default_response) { stub(:default_response, body: '{}') }
6
+
7
+ context 'listing resources' do
8
+ it 'calls rest client' do
9
+ RestClient.should_receive(:get).with('https://co-up.cobot.me/api/resources',
10
+ 'Authorization' => 'Bearer token-123') { default_response }
11
+
12
+ api_client.get_resources 'co-up'
13
+ end
14
+
15
+ it 'returns the json' do
16
+ RestClient.stub(:get) { stub(:response, body: [{id: 'resource-1'}].to_json) }
17
+
18
+ resources = api_client.get_resources 'co-up'
19
+
20
+ expect(resources).to eql([{id: 'resource-1'}])
21
+ end
22
+ end
23
+
24
+ context 'creating a booking' do
25
+ it 'calls rest client' do
26
+ RestClient.should_receive(:post).with('https://co-up.cobot.me/api/resources/res-1/bookings',
27
+ {title: 'meeting'},
28
+ 'Authorization' => 'Bearer token-123') { default_response }
29
+
30
+ api_client.create_booking 'co-up', 'res-1', title: 'meeting'
31
+ end
32
+
33
+ it 'returns the json' do
34
+ RestClient.stub(:post) { stub(:response, body: {title: 'meeting'}.to_json) }
35
+
36
+ booking = api_client.create_booking 'co-up', 'res-1', title: 'meeting'
37
+
38
+ expect(booking).to eql({title: 'meeting'})
39
+ end
40
+ end
41
+
42
+ context 'updating a booking' do
43
+ it 'calls rest client' do
44
+ RestClient.should_receive(:put).with('https://co-up.cobot.me/api/bookings/booking-1',
45
+ {title: 'meeting'},
46
+ 'Authorization' => 'Bearer token-123') { default_response }
47
+
48
+ api_client.update_booking 'co-up', 'booking-1', title: 'meeting'
49
+ end
50
+
51
+ it 'returns the json' do
52
+ RestClient.stub(:put) { stub(:response, body: {title: 'meeting'}.to_json) }
53
+
54
+ booking = api_client.update_booking 'co-up', 'booking-1', title: 'meeting'
55
+
56
+ expect(booking).to eql({title: 'meeting'})
57
+ end
58
+ end
59
+
60
+ context 'deleting a booking' do
61
+ it 'calls rest client' do
62
+ RestClient.should_receive(:delete).with('https://co-up.cobot.me/api/bookings/booking-1',
63
+ 'Authorization' => 'Bearer token-123') { default_response }
64
+
65
+ api_client.delete_booking 'co-up', 'booking-1'
66
+ end
67
+ end
68
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cobot_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-14 00:00:00.000000000 Z
12
+ date: 2013-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -59,6 +59,38 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rest-client
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
62
94
  - !ruby/object:Gem::Dependency
63
95
  name: rspec
64
96
  requirement: !ruby/object:Gem::Requirement
@@ -108,11 +140,13 @@ files:
108
140
  - app/views/cobot_client/_resize_script.html.erb
109
141
  - cobot_client.gemspec
110
142
  - lib/cobot_client.rb
143
+ - lib/cobot_client/api_client.rb
111
144
  - lib/cobot_client/engine.rb
112
145
  - lib/cobot_client/navigation_link.rb
113
146
  - lib/cobot_client/navigation_link_service.rb
114
147
  - lib/cobot_client/url_helper.rb
115
148
  - lib/cobot_client/version.rb
149
+ - spec/cobot_client/api_client_spec.rb
116
150
  - spec/cobot_client/navigation_link_service_spec.rb
117
151
  - spec/cobot_client/url_helper_spec.rb
118
152
  - spec/spec_helper.rb
@@ -141,6 +175,7 @@ signing_key:
141
175
  specification_version: 3
142
176
  summary: Client for the Cobot API plus helpers
143
177
  test_files:
178
+ - spec/cobot_client/api_client_spec.rb
144
179
  - spec/cobot_client/navigation_link_service_spec.rb
145
180
  - spec/cobot_client/url_helper_spec.rb
146
181
  - spec/spec_helper.rb