lib/ringcentral_sdk/platform/platform.rb in ringcentral_sdk-0.2.0 vs lib/ringcentral_sdk/platform/platform.rb in ringcentral_sdk-0.3.0
- old
+ new
@@ -19,11 +19,11 @@
attr_accessor :server_url
attr_reader :client
attr_reader :token
- attr_reader :ua
+ attr_reader :user_agent
def initialize(app_key='', app_secret='', server_url=RingCentralSdk::Sdk::RC_SERVER_SANDBOX)
@app_key = app_key
@app_secret = app_secret
@server_url = server_url
@@ -34,28 +34,48 @@
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,
:headers => { 'Authorization' => 'Basic ' + get_api_key() } })
set_token(token)
end
def set_token(token)
if token.is_a?(Hash)
- oauth2client = get_oauth2_client()
- token = OAuth2::AccessToken::from_hash(oauth2client, token)
+ token = OAuth2::AccessToken::from_hash(new_oauth2_client(), token)
end
unless token.is_a?(OAuth2::AccessToken)
- raise "Token is a OAuth2::AccessToken"
+ raise "Token is not a OAuth2::AccessToken"
end
@token = token
@client = Faraday.new(:url => get_api_version_url()) do |conn|
@@ -67,10 +87,10 @@
conn.response :json, :content_type => 'application/json'
conn.adapter Faraday.default_adapter
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)
end