Skip to content

Commit

Permalink
add subscription support
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Oct 8, 2015
1 parent a5c8646 commit 5832df8
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 13 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: false
cache: bundler
language: ruby

rvm:
- 1.8.7
- 1.9.3
Expand All @@ -9,3 +10,14 @@ rvm:
- 2.1.6
- 2.2.0
- 2.2.2
- ruby-head
- jruby
- jruby-head
- rbx-2

matrix:
fast_finish: true
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
- rvm: rbx-2
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CHANGELOG
---------
- **2015-10-07**: 0.3.0
- Add subscription support via PubNub
- **2015-09-14**: 0.2.0
- Refactor platform class to remove `authorized` method in favor of `set_token`
- Add `User-Agent` header support
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ RingCentral SDK
1. [API Requests](#api-requests)
1. [SMS Example](#sms-example)
2. [Fax Example](#fax-example)
1. [Subscriptions](#subscriptions)
5. [Change Log](#change-log)
6. [Links](#links)
7. [Contributions](#contributions)
Expand Down Expand Up @@ -212,9 +213,30 @@ response = client.post do |req|
end
```

### Subscriptions

To make subscriptions with RingCentral, use the SDK object to create subscription Observer object and then add observers to it.

```ruby
sub = rcsdk.create_subscription()

sub.subscribe(['/restapi/v1.0/account/~/extension/~/presence'])

class MyListener
def update(message)
puts "Subscription Message Received"
puts JSON.dump(message)
end
end

sub.add_observer(MyListener.new())

sub.destroy() # end the subscription
```

## Versioning

This project is currently in development and the API can change. During development (Version 0.x.x), breaking changes will be indicated by a change in minor version (second number).
This project is currently in development and the API can change. During initial development (Version 0.x.x), minor version changes will indicate either substantial feature inclusion or breaking changes.

Once the project is version 1.0.0 or above, it will use [semantic versioning](http://semver.org/). At this time, breaking changes will be indicated by a change in major version.

Expand Down
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.3.0
2 changes: 2 additions & 0 deletions lib/ringcentral_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
module RingCentralSdk
autoload :Helpers, 'ringcentral_sdk/helpers'
autoload :Platform, 'ringcentral_sdk/platform'
autoload :PubnubFactory, 'ringcentral_sdk/pubnub_factory'
autoload :Sdk, 'ringcentral_sdk/sdk'
autoload :Subscription, 'ringcentral_sdk/subscription'
end
2 changes: 1 addition & 1 deletion lib/ringcentral_sdk/platform.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RingCentralSdk::Platform
autoload :Platform, 'ringcentral_sdk/platform/platform'
autoload :Platform, 'ringcentral_sdk/platform/platform'
end
27 changes: 24 additions & 3 deletions lib/ringcentral_sdk/platform/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,29 @@ def get_api_version_url()
return @server_url + URL_PREFIX + '/' + API_VERSION
end

def create_url(url, add_server=false, add_method=nil, add_token=false)
built_url = ''
has_http = !url.index('http://').nil? && !url.index('https://').nil?

if add_server && ! has_http
built_url += @server_url
end

if url.index(URL_PREFIX).nil? && ! has_http
built_url += URL_PREFIX + '/' + API_VERSION
end

built_url += url

return built_url
end

def login(username, extension, password)
return authorize(username, extension, password)
end

def authorize(username='', extension='', password='', remember=false)
oauth2client = get_oauth2_client()
oauth2client = new_oauth2_client()

token = oauth2client.password.get_token(username, password, {
:extension => extension,
Expand All @@ -48,7 +69,7 @@ def authorize(username='', extension='', password='', remember=false)

def set_token(token)
if token.is_a?(Hash)
token = OAuth2::AccessToken::from_hash(get_oauth2_client(), token)
token = OAuth2::AccessToken::from_hash(new_oauth2_client(), token)
end

unless token.is_a?(OAuth2::AccessToken)
Expand All @@ -68,7 +89,7 @@ def set_token(token)
end
end

def get_oauth2_client()
def new_oauth2_client()
return OAuth2::Client.new(@app_key, @app_secret,
:site => @server_url,
:token_url => TOKEN_ENDPOINT)
Expand Down
33 changes: 33 additions & 0 deletions lib/ringcentral_sdk/pubnub_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'logger'
require 'pubnub'

module RingCentralSdk
class PubnubFactory

def initialize(use_mock=false)
@_use_mock = use_mock
end

def pubnub(subscribe_key='', ssl_on=false, publish_key='', my_logger=nil)
if @_use_mock
raise 'PubNub Mock is not implemented'
end

my_logger = Logger.new(STDOUT) if my_logger.nil?

pubnub = Pubnub.new(
:subscribe_key => subscribe_key.to_s,
:publish_key => publish_key.to_s,
:error_callback => lambda { |msg|
puts "Error callback says: #{msg.inspect}"
},
:connect_callback => lambda { |msg|
puts "CONNECTED: #{msg.inspect}"
},
:logger => my_logger
)

return pubnub
end
end
end
12 changes: 9 additions & 3 deletions lib/ringcentral_sdk/sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ class Sdk
RC_SERVER_PRODUCTION = 'https://platform.ringcentral.com'
RC_SERVER_SANDBOX = 'https://platform.devtest.ringcentral.com'

#attr_reader :parser
attr_reader :platform

def initialize(app_key=nil,app_secret=nil,server_url=nil,username=nil,extension=nil,password=nil)
#@parser = RingCentralSdk::Platform::Parser.new
use_pubnub_mock = false

@platform = RingCentralSdk::Platform::Platform.new(app_key, app_secret, server_url)
if not username.nil? and not password.nil?
@platform.authorize(username, extension, password)
end

@_pubnub_factory = RingCentralSdk::PubnubFactory.new(use_pubnub_mock)
end

def request(helper=nil)
return @platform.request(helper)
end

def create_subscription()
return RingCentralSdk::Subscription.new(@platform, @_pubnub_factory)
end
end
end
end
Loading

0 comments on commit 5832df8

Please sign in to comment.