Sha256: c7ec8c3635d0f75bc2e230e571c2dc650034ad639a37bb49e1bf7abdd75097d2

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module JiraSync
    require 'fileutils'
    require 'json'


    class LocalIssueRepository

        def initialize(path)
            @path = path
            FileUtils::mkdir_p @path
        end

        def save(issue)
            json = JSON.pretty_generate(issue)
            file_path = "#{@path}/#{issue['key']}.json"
            File.write(file_path, json)

            updateTime = DateTime.parse(issue['fields']['updated'])

            File.utime(DateTime.now.to_time, updateTime.to_time, file_path)
        end

        def state_exists?
            file_path = "#{@path}/sync_state.json"
            File.exist?(file_path)
        end

        def save_state(state)
            json = JSON.pretty_generate(state)
            file_path = "#{@path}/sync_state.json"
            File.write(file_path, json)
        end

        def load_state()
            file_path = "#{@path}/sync_state.json"
            if (!File.exists?(file_path))
                raise ("File '#{file_path}' with previous sync state could not be found\n" +
                          "please run an initial fetch first")
            end
            s = IO.read(file_path)
            JSON.parse(s)
        end

        def save_attachment(issue, attachment, data)
            FileUtils::mkdir_p("#{@path}/attachments")
            file_path = "#{@path}/attachments/#{issue['key']}-#{attachment['id']}-#{attachment['filename'].gsub(" ", "_")}"
            File.write(file_path, data, {:mode => 'wb'})
        end
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jirasync-0.4.6 lib/jirasync/local_repository.rb
jirasync-0.4.5 lib/jirasync/local_repository.rb
jirasync-0.4.4 lib/jirasync/local_repository.rb