I'm currently building a rails project that reaches out to PagerDuty to pull who is oncall and their contact information. I have a rails controller that contains a function like:
def findOncallInfo(arg1)
primary_query = {
'escalation_policy_ids' => ["MY_ID"],
'schedule_ids' => [arg1]
}
headers = {
'Content-Type' => 'application/json',
'Accept' => 'application/vnd.pagerduty+json;version=2',
'Authorization' => "Token token=keklolr0flc0pt3r"
}
ocresponse = HTTParty.get(
'https://api.pagerduty.com/oncalls',
:query => primary_query,
:headers => headers
)
rawtext = ocresponse.body
text = JSON.parse(rawtext)
policies = text["oncalls"]
policies.each do |policy|
oncall = policy["user"]
oncall.each do |key, value|
if key == 'id'
@oncallID = value
elsif key == 'summary'
@oncallName = value
end
end
end
inforesponse = HTTParty.get(
"https://api.pagerduty.com/users/#{@oncallID}/contact_methods",
headers: {
'Content-Type' => 'application/json',
'Accept' => 'application/vnd.pagerduty+json;version=2',
'Authorization' => "Token token=keklolr0flc0pt3r"
}
)
rawtext = inforesponse.body
text = JSON.parse(rawtext)
methods = text["contact_methods"]
methods.each do |method|
if method['type'] == 'email_contact_method'
@oncallEmail = method['address']
elsif method['type'] == 'phone_contact_method'
@oncallPhone = method['address']
end
end
@oncallfinal = [@oncallName, @oncallPhone, @oncallEmail]
end
Now, when I call this from my view using <%= findOncallInfo('MY_SCHEDULE') %> it will return the last line of the function prior to [end]. For example, pulling up the page would display:
["My Name", "0000000000", "[email protected]"]
Now, if I add @oncallEmail after @oncallfinal (or instead of @oncallfinal), it would display just the email address.
What I'm trying to do is call all of the variables in different places on the page, but when I do so, all I receive is 'nil' on the page. For example:
<h1>Test</h1>
<table>
<%= findOncallInfo('MY_SCHEDULE_ID') %> #displayed only for testing purposes
<tr>
<td><%= @oncallName %></td>
<td><%= @oncallPhone %></td>
<td><%= @oncallEmail %></td>
</tr>
</table>
Results in:
Test
["My Name", "0000000000", "[email protected]"]
nil nil nil
Could someone give some insight into where I might be going wrong with getting these to display?