cobot_client 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.
@@ -0,0 +1,29 @@
1
+ module CobotClient
2
+ module XdmHelper
3
+ def self.included(base)
4
+ if base.ancestors.include?(ActionController::Base)
5
+ base.class_eval do
6
+ before_filter :capture_xdm_params
7
+ helper_method :xdm_params
8
+ end
9
+ end
10
+ end
11
+
12
+ def xdm_params
13
+ session.inject({}) do |hash, element|
14
+ if element.first.match(/xdm\_/)
15
+ hash[element.first] = element.last
16
+ end
17
+ hash
18
+ end
19
+ end
20
+
21
+ def capture_xdm_params
22
+ params.each do |key, value|
23
+ if key.match(/xdm\_/)
24
+ session[key] = value
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ <script>
2
+ $(function() {
3
+ easyXDM.query = <%== xdm_params.to_json %>;
4
+ window.socket = new easyXDM.Socket({
5
+ onReady: function() {
6
+ setTimeout(function() {
7
+ window.socket.postMessage($('body').outerHeight());
8
+ }, 100);
9
+ }
10
+ });
11
+ });
12
+ </script>
data/cobot_client.gemspec CHANGED
@@ -16,5 +16,8 @@ Gem::Specification.new do |gem|
16
16
  gem.version = CobotClient::VERSION
17
17
 
18
18
  gem.add_dependency 'activesupport'
19
+ gem.add_dependency 'virtus'
20
+ gem.add_dependency 'oauth2'
19
21
  gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'rake'
20
23
  end
@@ -0,0 +1,5 @@
1
+ module CobotClient
2
+ class Engine < Rails::Engine
3
+ isolate_namespace CobotClient
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'virtus'
2
+
3
+ class CobotClient::NavigationLink
4
+ include Virtus
5
+
6
+ attribute :section, String
7
+ attribute :label, String
8
+ attribute :iframe_url, String
9
+ attribute :user_url, String
10
+ end
@@ -0,0 +1,60 @@
1
+ require 'oauth2'
2
+
3
+ module CobotClient
4
+ # Used to install links into the Cobot navigation of a space.
5
+ class NavigationLinkService
6
+ include UrlHelper
7
+
8
+ # oauth_client - an OAuth2::Client
9
+ # access_token - an access token string (owner must be admin of the space to be used)
10
+ def initialize(oauth_client, access_token, space_sudomain)
11
+ @oauth_client = oauth_client
12
+ @access_token = access_token
13
+ @subdomain = space_sudomain
14
+ end
15
+
16
+ # Checks if links are already installed and if not installs them.
17
+ #
18
+ # new_links - any number of `CobotClient::NavigationLink`s
19
+ #
20
+ # Returns the links as `[CobotClient::NavigationLink]`
21
+ def install_links(new_links)
22
+ if (links = get_links).empty?
23
+ new_links.each do |link|
24
+ links << create_link(link)
25
+ end
26
+ end
27
+ links
28
+ end
29
+
30
+ private
31
+
32
+ def get_links
33
+ token.get(cobot_url(@subdomain, "/api/navigation_links")).parsed.map do |attributes|
34
+ NavigationLink.new attributes
35
+ end
36
+ end
37
+
38
+ def create_link(link)
39
+ response = token.post(cobot_url(@subdomain, '/api/navigation_links'), body: {
40
+ section: link.section,
41
+ label: link.label,
42
+ iframe_url: link.iframe_url
43
+ })
44
+
45
+ unless successful?(response)
46
+ raise "Error installing link: #{response.body}"
47
+ end
48
+
49
+ NavigationLink.new response.parsed
50
+ end
51
+
52
+ def token
53
+ @token ||= OAuth2::AccessToken.new(@oauth_client, @access_token)
54
+ end
55
+
56
+ def successful?(response)
57
+ [200, 201].include?(response.status)
58
+ end
59
+ end
60
+ end
@@ -1,3 +1,3 @@
1
1
  module CobotClient
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/cobot_client.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  require "cobot_client/version"
2
+ require 'cobot_client/engine' if defined?(Rails)
2
3
 
3
4
  module CobotClient
4
5
  autoload :UrlHelper, 'cobot_client/url_helper'
6
+ autoload :NavigationLink, 'cobot_client/navigation_link'
7
+ autoload :NavigationLinkService, 'cobot_client/navigation_link_service'
8
+ autoload :XdmHelper, 'cobot_client/xdm_helper'
5
9
  end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe CobotClient::NavigationLinkService, '#install_links' do
4
+ let(:service) { CobotClient::NavigationLinkService.new(oauth_client, 'token-1', 'co-up') }
5
+ let(:oauth_client) { stub(:oauth_client) }
6
+
7
+ before(:each) do
8
+ @token = stub(:token).as_null_object
9
+ OAuth2::AccessToken.stub(new: @token)
10
+ end
11
+
12
+ context 'when there are links already' do
13
+ before(:each) do
14
+ @token.stub(:get).with('https://co-up.cobot.me/api/navigation_links') do stub(:response, parsed: [
15
+ {label: 'test link'}])
16
+ end
17
+ end
18
+
19
+ it 'installs no links' do
20
+ @token.should_not_receive(:post)
21
+
22
+ service.install_links [stub(:link)]
23
+ end
24
+
25
+ it 'returns the links' do
26
+ expect(service.install_links([stub(:link)]).map(&:label)).to eql(['test link'])
27
+ end
28
+ end
29
+
30
+ context 'when there are no links installed' do
31
+ let(:link) { stub(:link, section: 'admin/manage', label: 'test link', iframe_url: '/test') }
32
+ before(:each) do
33
+ @token.stub(:get).with('https://co-up.cobot.me/api/navigation_links') { stub(:response, parsed: []) }
34
+ end
35
+
36
+ it 'installs the links' do
37
+ @token.should_receive(:post).with('https://co-up.cobot.me/api/navigation_links', body: {
38
+ section: 'admin/manage', label: 'test link', iframe_url: '/test'
39
+ }) { stub(:response, status: 201, parsed: {}) }
40
+
41
+ service.install_links [link]
42
+ end
43
+
44
+ it 'returns the links created' do
45
+ response = stub(:response, status: 201, parsed: {label: 'test link'})
46
+ @token.stub(:post) { response }
47
+
48
+ expect(service.install_links([link]).map(&:label)).to eql(['test link'])
49
+ end
50
+ end
51
+ 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.0.1
4
+ version: 0.1.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: 2012-12-03 00:00:00.000000000 Z
12
+ date: 2012-12-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: virtus
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: oauth2
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  - !ruby/object:Gem::Dependency
31
63
  name: rspec
32
64
  requirement: !ruby/object:Gem::Requirement
@@ -43,6 +75,22 @@ dependencies:
43
75
  - - ! '>='
44
76
  - !ruby/object:Gem::Version
45
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
46
94
  description: Client for the Cobot API plus helpers
47
95
  email:
48
96
  - alex@cobot.me
@@ -55,10 +103,17 @@ files:
55
103
  - LICENSE
56
104
  - README.md
57
105
  - Rakefile
106
+ - app/assets/javascripts/cobot_client/easyxdm.js
107
+ - app/helpers/cobot_client/xdm_helper.rb
108
+ - app/views/cobot_client/_resize_script.html.erb
58
109
  - cobot_client.gemspec
59
110
  - lib/cobot_client.rb
111
+ - lib/cobot_client/engine.rb
112
+ - lib/cobot_client/navigation_link.rb
113
+ - lib/cobot_client/navigation_link_service.rb
60
114
  - lib/cobot_client/url_helper.rb
61
115
  - lib/cobot_client/version.rb
116
+ - spec/cobot_client/navigation_link_service_spec.rb
62
117
  - spec/cobot_client/url_helper_spec.rb
63
118
  - spec/spec_helper.rb
64
119
  homepage: http://github.com/cobot/cobot_client
@@ -86,6 +141,6 @@ signing_key:
86
141
  specification_version: 3
87
142
  summary: Client for the Cobot API plus helpers
88
143
  test_files:
144
+ - spec/cobot_client/navigation_link_service_spec.rb
89
145
  - spec/cobot_client/url_helper_spec.rb
90
146
  - spec/spec_helper.rb
91
- has_rdoc: