如何制作更新值的Json格式

时间:2014-09-02 23:09:13

标签: ruby json

我对Ruby很新,但我正在制作一个脚本,将数据从队列中的票证系统列表,创建时间及其状态中提取出来,并生成一个包含以下内容的文件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

所以我对如何修复或改进我的脚本有任何建议,然后我想知道的一件事是我如何制作这个结构,所以这个脚本将每隔3小时由一个cronjob部署,所以结构中的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":""}

    ]

}

1 个答案:

答案 0 :(得分:0)

我认为进入ruby的面向对象思想会很好。所以我会推荐这样的骨架(我保留了所有的进口,虽然其中一些似乎没必要):

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作业运行。我还要为文件名添加时间戳,除非您打算让脚本始终覆盖该文件。