Class: PlutoraRest::Calls

Inherits:
Object
  • Object
show all
Defined in:
lib/plutora_rest.rb,
lib/plutora_rest/sections/changes.rb,
lib/plutora_rest/sections/systems.rb,
lib/plutora_rest/sections/releases.rb,
lib/plutora_rest/sections/lookupfields.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Calls

—- Initialize —-



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/plutora_rest.rb', line 18

def initialize(opts = {})
#- Use to initilze the object and connect to plutora
 # => token
 #
  oauth_url = opts[:oauthURL] || 'https://usoauth.plutora.com/oauth/token'

  params = {'grant_type' => 'password', 
    'client_id' => opts[:client_id], 
    'client_secret' => opts[:client_secret], 
    'username' => opts[:username], 
    'password' => opts[:password]}

  headers = {'Accept' => 'application/json', 
    'Content-Type' => 'application/x-www-form-urlencoded'}

  @base_url= opts[:baseURL] || "https://usapi.plutora.com/"

  rConnect=plutora_post({:url => oauth_url, :params => params, :headers => headers})
  rConnect=JSON.parse(rConnect)

  @token=rConnect['access_token']
  @expires_in=rConnect['expires_in']
  @expiresAt=DateTime.now + @expires_in.to_i.seconds
  @refresh_token=rConnect['refresh_token']

  @oauthURL=oauth_url
  @clientID=opts[:client_id]
  @clientSecret=opts[:client_secret]

  return @token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



15
16
17
# File 'lib/plutora_rest.rb', line 15

def token
  @token
end

Instance Method Details

#create_Change(opts = {}) ⇒ Object

—- Create Change —- TODO: Validate functionality



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/plutora_rest/sections/changes.rb', line 146

def create_Change(opts = {})
	if opts[:params] == nil
		raise "Create Change requires Parameters"
	end

	url = "#{@base_url}Changes"

	headers = {
		'Authorization' => "Bearer #{(opts[:token] == nil ? @token : opts[:token])}", 
		'Accept' => 'application/json', 
		'Content-Type' => 'application/x-www-form-urlencoded'
	}

		resp=plutora_post({:url => url, :params => opts[:params], :headers => headers})
		return (resp.code == 204 ? true : false)
end

#formatResp(opts) ⇒ Object

—- Format Response into JSON —-



115
116
117
118
# File 'lib/plutora_rest.rb', line 115

def formatResp(opts)
  json_convert=Hash.from_xml(opts[:xml]).to_json
  return JSON.parse(json_convert)
end

#get_AdditionalInformation_FieldID(opts = {}) ⇒ Object

—- Returns the Additional Information (Id) Value



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/plutora_rest/sections/changes.rb', line 168

def get_AdditionalInformation_FieldID(opts = {})
	if opts[:fieldname] == nil
raise "get Additional Information (Field ID) options variable 'fieldname' requires a value."
		end

		sId = nil

		if opts[:data].class == Hash
if opts[:data]['Name'] == opts[:fieldname]
	sID = opts[:data]['Id']
end
		else
opts[:data].each do |fields|
	if fields['Name'] == opts[:fieldname]
		sID = fields['Id']
	end
end
		end

		return (sID == nil ? false : sID)
end

#get_AdditionalInformation_FieldValue(opts = {}) ⇒ Object

—- Returns the Additional Information (fields) Value



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/plutora_rest/sections/changes.rb', line 191

def get_AdditionalInformation_FieldValue(opts = {})
	if opts[:fieldname] == nil
		raise "get Additional Information (Field Value) options variable 'fieldname' requires a value."
	end

	sValue=nil

	if opts[:data].class == Hash
		if opts[:data]['Name'] == opts[:fieldname]
			case opts[:data]['DataType']
			when 'DatePicker'
				if !opts[:data]['Date'].class == Hash
					sValue = opts[:data]['Date']
				end
			when 'ListField'
				if opts[:data]['ListItem'].class == Hash
					if opts[:data]['ListItem'].size > 1
						sValue = opts[:data]['ListItem']['Value']
					end
				end
			when 'FreeText'
				if opts[:data]['Text'].class == String
					sValue = opts[:data]['Text']
				end
			when 'TimePicker'
				if !opts[:data]['Time'].class == Hash
					sValue = opts[:data]['Time']
				end
			when 'Number'
				if opts[:data]['Number'].class == String
					sValue = opts[:data]['Number']
				end
			when 'DateTimePicker'
				if !opts[:data]['DateTime'].class == Hash
					sValue = opts[:data]['DateTime']
				end
			when 'ListSelect'
				if !opts[:data]['MultilListItem'].class == Hash
					sValue = opts[:data]['MultilListItem']
				end
			else
				sValue=nil
			end
		end
	else
		opts[:data].each do |items|
			if items['Name'] == opts[:fieldname]
				case items['DataType']
				when 'DatePicker'
					if !items['Date'].class == Hash
						sValue = items['Date']
					end
				when 'ListField'
					if items['ListItem'].class == Hash
						if items['ListItem'].size > 1
							sValue = items['ListItem']['Value']
						end
					end
				when 'FreeText'
					if items['Text'].class == String
						sValue = items['Text']
					end
				when 'TimePicker'
					if !items['Time'].class == Hash
						sValue = items['Time']
					end
				when 'Number'
					if items['Number'].class == String
						sValue = items['Number']
					end
				when 'DateTimePicker'
					if !items['DateTime'].class == Hash
						sValue = items['DateTime']
					end
				when 'ListSelect'
					if !items['MultilListItem'].class == Hash
						sValue = items['MultilListItem']
					end
				else
					sValue=nil
				end
				break
			end
		end
	end

	return (sValue == nil ? false : sValue)
end

#get_Changes(opts = {}) ⇒ Object

—- Get Changes —-



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/plutora_rest/sections/changes.rb', line 13

def get_Changes(opts = {})
	vRetry=0
	begin
		token=opts[:token] || @token

		url = "#{@base_url}Changes"

		headers = {'Authorization' => 'Bearer ' + token}

		resp=plutora_get({:url => url, :headers => headers})
		resp=formatResp({:xml => resp})
		return resp
	rescue
		puts "-----> Get Changes Error <-----"
		puts $!
		puts $@
		puts "<----------------------------->"
		if $! == "500 Internal Server Error"
        vRetry+=1
        retry if vRetry < 2
      end
	end
end

#get_ChangesById(opts = {}) ⇒ Object

—- Get Changes By Id —-



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/plutora_rest/sections/changes.rb', line 38

def get_ChangesById(opts = {})
	vRetry=0
	begin
		if opts[:id] == nil
			raise "Changes By Id requires an ID value!"
		end

		url = "#{@base_url}Changes/#{opts[:id]}"

		headers = {'Authorization' => 'Bearer ' + (opts[:token] == nil ? @token : opts[:token])}

		resp=formatResp({:xml => plutora_get({:url => url, :headers => headers})})
		return resp
	rescue
		puts "-----> Get Changes By Id Error <-----"
		puts $!
		puts $@
		puts "<----------------------------------->"
		if $! == "500 Internal Server Error"
        vRetry+=1
        retry if vRetry < 2
      end
	end
end

#get_ChangesById_AdditionalInformation(opts = {}) ⇒ Object

—- Get Changes By Id Additional Informations —-



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/plutora_rest/sections/changes.rb', line 64

def get_ChangesById_AdditionalInformation(opts = {})
	if opts[:id] == nil
		raise "Changes By Id - Additional Information requires an ID value!"
	end

	url = "#{@base_url}Changes/#{opts[:id]}/additionalInformation"

	headers = {'Authorization' => "Bearer #{(opts[:token] == nil ? @token : opts[:token])}"}

		resp=formatResp({:xml => plutora_get({:url => url, :headers => headers})})
		return resp
end

#get_ChangesFilter(opts = {}) ⇒ Object

—- Get Changes by Filter —-



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/plutora_rest/sections/changes.rb', line 78

def get_ChangesFilter(opts = {})
	if opts[:searchfilter] == nil
		raise "Change by Filter - Search Filters required!"
	end

	headers = {'Authorization' => "Bearer #{(opts[:token] == nil ? @token : opts[:token])}",
     	'Content-Type' => 'application/json'}

     url = "#{@base_url}Changes/filter"
     
     resp=formatResp({:xml=> plutora_post({:url => url, :params => opts[:searchfilter], :headers => headers})})
     # resp=plutora_post({:url => url, :params => opts[:searchfilter], :headers => headers})
     # resp=pluotra_post_json({:url => url, :params => opts[:searchfilter], :headers => headers})
     return resp
end

#get_LookupField_ValueID(opts = {}) ⇒ Object

—- Return Lookup Field Id by Type —-



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/plutora_rest/sections/lookupfields.rb', line 31

def get_LookupField_ValueID(opts = {})
	if opts[:type] == nil
		raise "Get Lookup Field Value Id requires a Type value."
	end
	if opts[:value] == nil
		raise "Get Lookup Field Value Id requires a Value to lookup."
	end

	sId = ''

	if opts[:data].class == Hash
if opts[:data]['Type'] == opts[:type]
	if opts[:data]['Value'] == opts[:value]
		sID = opts[:data]['Id']
	end
end
		else
opts[:data].each do |fields|
	if fields['Type'] == opts[:type]
		if fields['Value'] == opts[:value]
			sID = fields['Id']
		end
	end
end
		end

		return (sID == '' ? false : sID)
end

#get_lookupfields_byType(opts = {}) ⇒ Object

—- Get Lookupfields by Type —-



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/plutora_rest/sections/lookupfields.rb', line 13

def get_lookupfields_byType(opts = {})
	if opts[:type] == nil
		raise "Get Lookup Fields, Type value is required!"
	end

	url = "#{@base_url}lookupfields/#{opts[:type]}"

	headers = {'Authorization' => 'Bearer ' + (opts[:token] == nil ? @token : opts[:token])}

	resp=formatResp({:xml => plutora_get({:url => url, :headers => headers})})
	return resp
end

#get_ReleaseById_Systems(opts = {}) ⇒ Object

—- Get Release By Id Systems —-



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/plutora_rest/sections/releases.rb', line 54

def get_ReleaseById_Systems(opts = {})
  if opts[:id] == nil
    raise "Release By Id Systems requires an ID value!"
  end

  url "#{@base_url}releases/#{opts[:id]}/system"

  headers = {'Authorization' => 'Bearer ' + (opts[:token] == nil ? @token : opts[:token])}

  resp=formatResp({:xml => plutora_get({:url => url, :headers => headers})})
  return resp
end

#get_Releases(opts = {}) ⇒ Object

—- Get Releases —-



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/plutora_rest/sections/releases.rb', line 13

def get_Releases(opts = {})
	token=opts[:token] || @token

	url = "#{@base_url}releases"

	headers = {'Authorization' => 'Bearer ' + token}

	resp=plutora_get({:url => url, :headers => headers})
	resp=formatResp({:xml => resp})
	return resp
end

#get_ReleasesById(opts = {}) ⇒ Object

—- Get Releases By Id —-



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/plutora_rest/sections/releases.rb', line 26

def get_ReleasesById(opts = {})
	if opts[:id] == nil
		raise "Releases By Id requires an ID value!"
	end

	url = "#{@base_url}releases/#{opts[:id]}"

	headers = {'Authorization' => 'Bearer ' + (opts[:token] == nil ? @token : opts[:token])}

	resp=formatResp({:xml => plutora_get({:url => url, :headers => headers})})
	return resp
end

#get_ReleasesById_AdditionalInformation(opts = {}) ⇒ Object

—- Get Releases By Id Additional Information —-



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/plutora_rest/sections/releases.rb', line 40

def get_ReleasesById_AdditionalInformation(opts = {})
  if opts[:id] == nil
    raise "Release By Id Additional Information requires an ID value!"
  end

  url "#{@base_url}releases/#{opts[:id]}/additionalInformation"

  headers = {'Authorization' => 'Bearer ' + (opts[:token] == nil ? @token : opts[:token])}

  resp=formatResp({:xml => plutora_get({:url => url, :headers => headers})})
  return resp
end

#get_Systems(opts = {}) ⇒ Object

—- Get Releases —-



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/plutora_rest/sections/systems.rb', line 13

def get_Systems(opts = {})
	vRetry=0
   begin
     token=opts[:token] || @token

 		url = "#{@base_url}systems"

 		headers = {'Authorization' => 'Bearer ' + token}

 		resp=plutora_get({:url => url, :headers => headers})
 		resp=formatResp({:xml => resp})
 		return resp
   rescue
     puts "-----> Get Systems Error <-----"
     puts $!
     puts $@
     puts "<----------------------------->"
     if $! == "500 Internal Server Error"
       vRetry+=1
       retry if vRetry < 2
     end
   end
end

#get_Systems_ByRoleType(opts = {}) ⇒ Object

—- Returns the System names of the indicated System Role Type



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/plutora_rest/sections/changes.rb', line 281

def get_Systems_ByRoleType(opts = {})
	if opts[:roletype] == nil
		raise "get Systems By Role Type requires a Role Type value."
	end

	sSystems=''

	if opts[:data].class == Hash
		if opts[:data]['SystemRoleType'] == opts[:roletype]
			sSystems= opts[:data]['System']
		end
	else
		opts[:data].each do |systems|
			if systems['SystemRoleType'] == opts[:roletype]
				sSystems.concat(";#{systems['System']}")
			end
		end
		sSystems = sSystems[1..-1]
	end

	if sSystems == ''
		return false
	else
		return sSystems
	end
end

#get_SystemsById(opts = {}) ⇒ Object

—- Get Releases By Id —-



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/plutora_rest/sections/systems.rb', line 38

def get_SystemsById(opts = {})
	if opts[:id] == nil
		raise "Systems By Id requires an ID value!"
	end

	url = "#{@base_url}systems/#{opts[:id]}"

	headers = {'Authorization' => 'Bearer ' + (opts[:token] == nil ? @token : opts[:token])}

	resp=formatResp({:xml => plutora_get({:url => url, :headers => headers})})
	return resp
end

#pluotra_post_json(opts = {}) ⇒ Object



95
96
97
98
99
100
# File 'lib/plutora_rest.rb', line 95

def pluotra_post_json(opts = {})
  body=opts[:params].to_json
  puts body
  return RestClient.post(opts[:url], body, opts[:headers])
  # return RestClient.post(opts[:url], "{}", opts[:headers])
end

#plutora_get(opts) ⇒ Object

—- Get REST Call —-



103
104
105
106
# File 'lib/plutora_rest.rb', line 103

def plutora_get(opts)
  resp=RestClient.get(opts[:url], opts[:headers])
  return resp
end

#plutora_post(opts) ⇒ Object

—- POST REST Call —-



90
91
92
93
# File 'lib/plutora_rest.rb', line 90

def plutora_post(opts)
  resp=RestClient.post(opts[:url], opts[:params], opts[:headers])
  return resp
end

#plutora_put(opts) ⇒ Object

—- PUT REST Call —-



109
110
111
112
# File 'lib/plutora_rest.rb', line 109

def plutora_put(opts)
  resp=RestClient.put(opts[:url], opts[:params], opts[:headers])
  return resp
end

#refresh_token_if_expiredObject

—- Refresh Token If Expired —-



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/plutora_rest.rb', line 51

def refresh_token_if_expired
  begin
    if token_expired?
      params={'grant_type' => 'refresh_token',
        'refresh_token' => @refresh_token,
        'client_id' => @clientID,
        'client_secret' => @clientSecret
      }

      headers = {'Accept' => 'application/json', 
        'Content-Type' => 'application/x-www-form-urlencoded'
      }

      refreshConnect=plutora_post({:url => @oauthURL, :params => params, :headers => headers})
      refreshConnect=JSON.parse(refreshConnect)

      @token=refreshConnect['access_token']
      @expires_in=refreshConnect['expires_in']
      @expiresAt=DateTime.now + @expires_in.to_i.seconds
      @refresh_token=refreshConnect['refresh_token']

      return @token
    end
  rescue
    puts "<----- Refresh Token If Expired ----->"
    puts $!
    puts $@
    puts "<------------------------------------>"
  end
end

#token_expired?Boolean

—- Token Expired? —-

Returns:

  • (Boolean)


83
84
85
86
87
# File 'lib/plutora_rest.rb', line 83

def token_expired?
  expiry=Time.at(@expiresAt)
  return true if expiry < Time.now
  false
end

#update_ChangesById(opts = {}) ⇒ Object

—- Update Changes By ID —-



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/plutora_rest/sections/changes.rb', line 99

def update_ChangesById(opts = {})
	if opts[:id] == nil
		raise "Changes By Id requires an ID value!"
	end
	if opts[:params] == nil
		raise "Changes By Id requires Parameters!"
	end

	url = "#{@base_url}Changes/#{opts[:id]}"

	headers = {
		'Authorization' => "Bearer #{(opts[:token] == nil ? @token : opts[:token])}", 
		'Accept' => 'application/json', 
		'Content-Type' => 'application/x-www-form-urlencoded'
		#'Content-Type' => 'application/json'
	}

	resp=plutora_put({:url => url, :params => opts[:params], :headers => headers})
	return (resp.code == 204 ? true : false)
end

#update_ChangesById_AdditionalInformation(opts = {}) ⇒ Object

—- Update Changes By Id Additional Informations —-



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/plutora_rest/sections/changes.rb', line 121

def update_ChangesById_AdditionalInformation(opts = {})
	if opts[:id] == nil
		raise "Changes By Id - Additional Information requires an ID value!"
	end
	if opts[:params] == nil
		raise "Changes By Id - Additional Information requires Parameters!"
	end

	url = "#{@base_url}Changes/#{opts[:id]}/additionalInformation"

	headers = {
		'Authorization' => "Bearer #{(opts[:token] == nil ? @token : opts[:token])}",
		'Content-Type' => 'application/json'
	}

		resp=plutora_put({:url => url, :params => opts[:params], :headers => headers})
		return (resp.code == 204 ? true : false)
end

#update_ReleasesById(opts = {}) ⇒ Object

—- Put Releases By Id —-



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/plutora_rest/sections/releases.rb', line 72

def update_ReleasesById(opts = {})
  if opts[:id] == nil
    raise "Update Releases By Id requires an ID value!"
  end
  if opts[:params] == nil
    raise "Update Releasee By Id requires Parameters!"
  end

  url = "#{@base_url}releases/#{opts[:id]}"

  headers = {
    'Authorization' => "Bearer #{(opts[:token] == nil ? @token : opts[:token])}", 
    'Accept' => 'application/json', 
    'Content-Type' => 'application/x-www-form-urlencoded'
    #'Content-Type' => 'application/json'
  }

  resp=plutora_put({:url => url, :params => opts[:params], :headers => headers})
  return (resp.code == 204 ? true : false)
end

#update_ReleasesById_AdditionalInformation(opts = {}) ⇒ Object

—- Put Releases By Id Additional Information —-



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/plutora_rest/sections/releases.rb', line 94

def update_ReleasesById_AdditionalInformation(opts = {})
  if opts[:id] == nil
    raise "Update Releases By Id requires an ID value!"
  end
  if opts[:params] == nil
    raise "Update Releasee By Id requires Parameters!"
  end

  url = "#{@base_url}releases/#{opts[:id]}/additionalInformation"

  headers = {
    'Authorization' => "Bearer #{(opts[:token] == nil ? @token : opts[:token])}", 
    'Accept' => 'application/json', 
    'Content-Type' => 'application/x-www-form-urlencoded'
    #'Content-Type' => 'application/json'
  }

  resp=plutora_put({:url => url, :params => opts[:params], :headers => headers})
  return (resp.code == 204 ? true : false)
end