2014-09-02 49 views
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":""} 

    ] 

} 
+0

我对Ruby没有太多了解,但是不应该将'JSON.parse(url_output)'的结果存储在某个地方吗? – 2014-09-02 23:15:53

+0

如果你详细地解释了你自己来了多远 - 这到底是什么工作,什么不工作会很有帮助。并给了我们更直接的问题 - 不是“请给我一般批评”。 – Jesper 2014-09-03 06:53:16

回答

0

我认为这将是很好的进入红宝石的面向对象的思想。因此,我建议这样的骨架(我一直在你所有的进口虽然其中一些似乎是不必要的):

require 'rubygems' 
require 'json' 
require 'net/http' 
require 'highline/import' 
require 'pp' 


USERNAME = 'user' 
PASSWORD = 'password' 
URL  = "http://dev-webhelpdesk.corp:8081/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets?list=group&page=1&limit=25&username=#{USERNAME}&password=#{PASSWORD}" 
PATH  = 'myfile.txt' 

class DataFetcher 
    def initialize(url) 
    @url = url 
    end 

    def ticket_data 
    parse_data(perform_search) 
    end 

    # Parses the data from JSON 
    def parse_data(data) 
    # code goes here 
    end 

    # Do the HTTP-request 
    def perform_search 
    #code goes here 
    end 
end 

class DataWriter 
    def initialize(ticket_data) 
    @ticket_data = ticket_data 
    end 

    def write_to(path) 
    File.write(path, construct_data) 
    end 

    def construct_data 
    dump_json(construct_hash) 
    end 

    def dump_json(data) 
    JSON.pretty_generate(data) 
    end 

    # Construct the hash that will be turned into JSON, 
    # we assume that the DataFetcher has properly formatted it 
    def construct_hash 
    output = { 
     "LastOutput" => Time.now.to_i, 
     "Service" => @ticket_data 
    } 
    end 

end 

# This is where we actually do the work: 
data = DataFetcher.new(URL) 
writer = DataWriter.new(data) 
writer.write_to(PATH) 

您仍然需要连接东西,使运行的cron作业的脚本。我还会为文件名添加时间戳,除非您希望脚本始终覆盖文件。