0
我对Ruby有点新,但是我正在研究一个脚本,它将数据从队列中票证的票务系统列表中提取出来,他们的状态并生成一个文件具有以下JSON结构:如何使更新值的Json格式
{"LastUpdate":1373409010,
"Service":[
{"time":"07-09-2013 19:22:02 GMT","region":"","description":"All Systems OK.","service":""},
{"time":"07-09-2013 11:04:02 GMT","region":"","description":"All Systems OK.","service":""}
]
}
我已经拥有了从源头拉动然而我在建设这个结构难度数据的脚本。
require 'rubygems'
require 'json'
require 'net/http'
require 'highline/import'
require 'pp'
@usersol='user'
@passol='password'
@urlsol= "http://dev-webhelpdesk.corp:8081/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets?list=group&page=1&limit=25&username=#{@usersol}&password=#{@passol}"
def ticket_search #looks for tickets in solarwinds
resp = Net::HTTP.get_response(URI.parse(@urlsol))
url_output = resp.body
JSON.parse(url_output)
end
def ticket_data(result) #gets data needed from the search
result.each do | data |
final = data.values_at('id', 'lastUpdated', 'shortSubject', 'shortDetail')
end
end
def messages_content(looking) #gets content of ticket
looking.each do | messages |
ticket = messages.has_key? 'id'
if ticket
content = messages.values_at('shortDetail')
end
pp content
end
end
def lastupdate_time(last) #gets content of lastupdate of the ticket
last.each do | time_check |
ticket = time_check.has_key? 'id'
if ticket
lastupdate = time_check.values_at('lastUpdated')
end
pp lastupdate
end
end
def datastructure(format)
format.each do |lastup|
reference = lastup.has_key? 'id'
if reference
timeid = lastup.values_at('lastUpdated')
timeid.each do |lines|
result = ({time:"#{lines}", region:'', id:'', description:'All Systems OK', service:''})
puts result
end
end
end
所以我很愿意就如何解决或改善我的剧本任何建议,然后的事情,我想知道一个是我怎么能做出这样的结构,因此该脚本将通过一个cronjob进行部署每3小时一次,因此结构中的LastUpdate行需要反映该时间戳,然后每次检查队列中的票证列表时都应更新Service的内容。
例子:
# first time that it run it find 2 tickets with the time when they were ack
{"LastUpdate":1373409010,
"Service":[
{"time":"07-09-2013 19:22:02 GMT","region":"","description":"All Systems OK.","service":""},
{"time":"07-09-2013 11:04:02 GMT","region":"","description":"All Systems OK.","service":""}
]
}
# second time that runs and so on
{"LastUpdate":1373409011,
"Service":[
{"time":"07-09-2013 19:22:02 GMT","region":"","description":"All Systems OK.","service":""},
{"time":"07-09-2013 19:22:02 GMT","region":"","description":"All Systems OK.","service":""}
]
}
我对Ruby没有太多了解,但是不应该将'JSON.parse(url_output)'的结果存储在某个地方吗? – 2014-09-02 23:15:53
如果你详细地解释了你自己来了多远 - 这到底是什么工作,什么不工作会很有帮助。并给了我们更直接的问题 - 不是“请给我一般批评”。 – Jesper 2014-09-03 06:53:16