Class: Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/Helper.rb

Defined Under Namespace

Classes: Version

Class Method Summary collapse

Class Method Details

.compareVersion(version1, version2) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/Helper.rb', line 150

def self.compareVersion(version1, version2)
    if version1.major > version2.major
        true
    else
        if version1.minor > version2.minor
            true
        else
            if version1.patch > version2.patch
                true
            else
                false
            end
        end
    end
end

.createDirIfNotExist(dirPath) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/Helper.rb', line 28

def self.createDirIfNotExist(dirPath)
    dirs = dirPath.split("/")
    currentDir = ""
    begin
        dir = dirs.shift
        currentDir = "#{currentDir}/#{dir}"
        Dir.mkdir(currentDir) unless File.exists?(currentDir)
    end while dirs.length > 0
end

.createPostInfo(postInfo) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/Helper.rb', line 87

def self.createPostInfo()
    result = "---\n"
    result += "title: #{.title}\n"
    result += "author: #{.creator}\n"
    result += "date: #{.firstPublishedAt.strftime('%Y-%m-%dT%H:%M:%S.%LZ')}\n"
    result += "tags: [#{.tags.join(",")}]\n"
    result += "---\n"
    result += "\r\n"

    result
end

.createWatermark(postURL) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/Helper.rb', line 167

def self.createWatermark(postURL)
    text = "\r\n\r\n\r\n"
    text += "+-----------------------------------------------------------------------------------+"
    text += "\r\n"
    text += "\r\n"
    text += "| **[View original post on Medium](#{postURL}) - Converted by [ZhgChgLi](https://blog.zhgchg.li)/[ZMediumToMarkdown](https://github.com/ZhgChgLi/ZMediumToMarkdown)** |"
    text += "\r\n"
    text += "\r\n"
    text += "+-----------------------------------------------------------------------------------+"
    text += "\r\n"
    
    text
end

.downloadLatestVersionObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/Helper.rb', line 48

def self.downloadLatestVersion()
    apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases'
    versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] }

    version = nil
    index = 0
    while version == nil do
        thisVersion = versions[index]
        if thisVersion["prerelease"] == false
            version = thisVersion
            next
        end
        index += 1
    end
    
    zipFilePath = version["zipball_url"]
    puts "Downloading latest version from github..."
    open('latest.zip', 'wb') do |fo|
        fo.print open(zipFilePath).read
    end

    puts "Unzip..."
    Zip::File.open("latest.zip") do |zipfile|
        zipfile.each do |file|
            fileNames = file.name.split("/")
            fileNames.shift
            filePath = fileNames.join("/")
            if filePath != ''
                puts "Unzinp...#{filePath}"
                zipfile.extract(file, filePath) { true }
            end
        end
    end
    File.delete("latest.zip")

    tagName = version["tag_name"]
    puts "Update to version #{tagName} successfully!"
end

.getLocalVersionFromGemspecObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/Helper.rb', line 108

def self.getLocalVersionFromGemspec()
    rootPath = File.expand_path('../', File.dirname(__FILE__))
    
    result = nil
    if File.file?("#{rootPath}/ZMediumToMarkdown.gemspec")
        gemspecContent = File.read("#{rootPath}/ZMediumToMarkdown.gemspec")
        result = gemspecContent[/(gem\.version){1}\s+(\=)\s+(\'){1}(\d+(\.){1}\d+(\.){1}\d+){1}(\'){1}/, 4]
    else
        result = Gem.loaded_specs["ZMediumToMarkdown"].version.version
    end

    if !result.nil?
        versions = result.split(".")
        Version.new(versions[0],versions[1],versions[2])
    else
        nil
    end
end

.getRemoteVersionFromGithubObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/Helper.rb', line 127

def self.getRemoteVersionFromGithub()
    apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases'
    versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] }

    tagName = nil
    index = 0
    while tagName == nil do
        thisVersion = versions[index]
        if thisVersion["prerelease"] == false
            tagName = thisVersion["tag_name"]
            next
        end
        index += 1
    end

    if !tagName.nil?
        versions = (tagName.downcase.gsub! 'v','') .split(".")
        Version.new(versions[0],versions[1],versions[2])
    else
        nil
    end
end

.makeWarningText(message) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/Helper.rb', line 38

def self.makeWarningText(message)
    puts "####################################################\n"
    puts "#WARNING:\n"
    puts "##{message}\n"
    puts "#--------------------------------------------------#\n"
    puts "#Please feel free to open an Issue or submit a fix/contribution via Pull Request on:\n"
    puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown\n"
    puts "####################################################\n"
end

.printNewVersionMessageIfExistsObject



99
100
101
102
103
104
105
106
# File 'lib/Helper.rb', line 99

def self.printNewVersionMessageIfExists() 
    if Helper.compareVersion(Helper.getRemoteVersionFromGithub(), Helper.getLocalVersionFromGemspec())
        puts "##########################################################"
        puts "#####           New Version Available!!!             #####"
        puts "##### Please type `bin/ZMediumToMarkdown -n` to update!!##"
        puts "##########################################################"
    end
end