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