sugarcrb 0.1.4 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 724125dd9da860eda1fddf0926c9448aadc8359b
4
- data.tar.gz: cfcbc997dcdae9141c15e0206b2bccb1604e0210
3
+ metadata.gz: 28c88bf9e8829171b432d71cc1bf0ff43ccd59ab
4
+ data.tar.gz: f20fef0403de24481eafb2b10450d777ce16a530
5
5
  SHA512:
6
- metadata.gz: f006c2cc07a3a27c86cbff41cecb71ba1fe916d6c28a175b354f47fd41241e06dc8e7c1a2c09d35ba39192a244830abe3d0f1d69975f4acb8635c0bf6b78f3e7
7
- data.tar.gz: 488520c13918540a27543c282f1a9593ca2b379a287ffc0e66379101780a5708879360f5f56bb021e2b9f701491bb1d542fe8e6a837ccae92098461887cc23b4
6
+ metadata.gz: bd5eef173e0f044bda6213d437efab9c6fca3e926f959ea4b51a45dba14f89854b0e4388af2602977e9b8f6560ad07f4e6e8b9a4dd6ffff26f6b9a33b364a192
7
+ data.tar.gz: 11be8450b19cf68628691b7b36016afc5db86aba1528f20646c5e7e877f62b1c5a43c83ef03f0359ff3f40cece5fd94e87270f31725c9e09a31298092b28adc6
@@ -1,3 +1,3 @@
1
1
  module Sugarcrb
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/sugarcrb.rb CHANGED
@@ -2,33 +2,49 @@ require "sugarcrb/version"
2
2
  require 'rest-client'
3
3
 
4
4
  module Sugarcrb
5
- class << self
6
5
 
7
- def initialize(host, username, password, platform, client_id, client_secret)
8
- # Instance variables
9
- @host = host
10
- @username = username
11
- @password = password
12
- @platform = platform
13
- @client_secret = client_secret
14
- @client_id = client_id
15
- @access_token = ""
16
- @refresh_token = ""
17
- end
6
+ def initialize(host, username, password, platform, client_id, client_secret)
7
+ # Instance variables
8
+ @host = host
9
+ @username = username
10
+ @password = password
11
+ @platform = platform
12
+ @client_secret = client_secret
13
+ @client_id = client_id
14
+ @access_token = ""
15
+ @refresh_token = ""
16
+ end
17
+
18
+ attr_accessor :username
19
+ attr_accessor :access_token
20
+ attr_accessor :refresh_token
18
21
 
19
- attr_accessor :username
20
- attr_accessor :access_token
21
- attr_accessor :refresh_token
22
+ def oauth2_token (refresh = false)
23
+ data = {
24
+ "grant_type": "password",
25
+ "client_id": @client_id,
26
+ "client_secret": @client_secret,
27
+ "username": @username,
28
+ "password": @password,
29
+ "platform": @platform
30
+ }
31
+ response = RestClient.post "#{@host}/rest/v10/oauth2/token", data
32
+ response_json = JSON.load(response)
33
+ if (response.code == 200) then
34
+ @access_token = response_json['access_token']
35
+ @refresh_token = response_json['refresh_token']
36
+ end
37
+ return response
38
+ end
22
39
 
23
- def oauth2_token (refresh = false)
24
- data = {
25
- "grant_type": "password",
26
- "client_id": @client_id,
27
- "client_secret": @client_secret,
28
- "username": @username,
29
- "password": @password,
30
- "platform": @platform
31
- }
40
+ def oauth2_refresh_token
41
+ data = {
42
+ "grant_type": "refresh_token",
43
+ "refresh_token": @refresh_token,
44
+ "client_id": @client_id,
45
+ "client_secret": @client_secret,
46
+ }
47
+ begin
32
48
  response = RestClient.post "#{@host}/rest/v10/oauth2/token", data
33
49
  response_json = JSON.load(response)
34
50
  if (response.code == 200) then
@@ -36,44 +52,26 @@ module Sugarcrb
36
52
  @refresh_token = response_json['refresh_token']
37
53
  end
38
54
  return response
55
+ rescue RestClient::Unauthorized => err
56
+ return self.oauth2_token
39
57
  end
58
+ end
40
59
 
41
- def oauth2_refresh_token
42
- data = {
43
- "grant_type": "refresh_token",
44
- "refresh_token": @refresh_token,
45
- "client_id": @client_id,
46
- "client_secret": @client_secret,
47
- }
48
- begin
49
- response = RestClient.post "#{@host}/rest/v10/oauth2/token", data
50
- response_json = JSON.load(response)
51
- if (response.code == 200) then
52
- @access_token = response_json['access_token']
53
- @refresh_token = response_json['refresh_token']
54
- end
55
- return response
56
- rescue RestClient::Unauthorized => err
57
- return self.oauth2_token
60
+ def call (method, endpoint, data = false, reintents = 0)
61
+ begin
62
+ response = case method
63
+ when "post" then RestClient.post "#{@host}/rest/v10/#{endpoint}", data, headers={"OAuth-Token" => @access_token}
64
+ when "get" then RestClient.get "#{@host}/rest/v10/#{endpoint}", headers={"OAuth-Token" => @access_token}
65
+ when "put" then RestClient.put "#{@host}/rest/v10/#{endpoint}", data, headers={"OAuth-Token" => @access_token}
66
+ when "delete" then RestClient.delete "#{@host}/rest/v10/#{endpoint}", headers={"OAuth-Token" => @access_token}
58
67
  end
59
- end
60
68
 
61
- def call (method, endpoint, data = false, reintents = 0)
62
- begin
63
- response = case method
64
- when "post" then RestClient.post "#{@host}/rest/v10/#{endpoint}", data, headers={"OAuth-Token" => @access_token}
65
- when "get" then RestClient.get "#{@host}/rest/v10/#{endpoint}", headers={"OAuth-Token" => @access_token}
66
- when "put" then RestClient.put "#{@host}/rest/v10/#{endpoint}", data, headers={"OAuth-Token" => @access_token}
67
- when "delete" then RestClient.delete "#{@host}/rest/v10/#{endpoint}", headers={"OAuth-Token" => @access_token}
68
- end
69
-
70
- return response
71
- rescue RestClient::Unauthorized => err
72
- if reintents < 3 then
73
- self.oauth2_refresh_token
74
- reintents = reintents + 1
75
- self.call(method, endpoint, data, reintents)
76
- end
69
+ return response
70
+ rescue RestClient::Unauthorized => err
71
+ if reintents < 3 then
72
+ self.oauth2_refresh_token
73
+ reintents = reintents + 1
74
+ self.call(method, endpoint, data, reintents)
77
75
  end
78
76
  end
79
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - betobaz