Last active
August 29, 2015 14:20
-
-
Save sonsongithub/2fc372c869abfdb2719b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# generate HttpStatus.swift from wikipedia | |
# for reddift project | |
# https://github.com/sonsongithub/reddift | |
# | |
# http://en.wikipedia.org/wiki/List_of_HTTP_status_codes | |
# | |
require 'net/http' | |
class HTTPStatus | |
def initialize(code, desc) | |
@code = code.to_i | |
desc = desc.gsub(/\[.+?\]/, "") | |
desc = desc.gsub(/<.+?>/, "") | |
@desc = desc.gsub(/\(.+?\)/, "") | |
@title = @desc.gsub(/\s(\w)/){"#{$1.capitalize}"} | |
@desc = @desc.gsub(/\s+$|/, "") | |
@title = @title.gsub(/\s+$/, "") | |
@title = @title.gsub(/[\-'\/]/, "") | |
end | |
attr_accessor :code, :desc, :title | |
end | |
def main | |
text = "" | |
# text = File::open("./statusCode.txt").read | |
begin | |
url = URI.parse('http://en.wikipedia.org/wiki/List_of_HTTP_status_codes') | |
req = Net::HTTP::Get.new(url.path) | |
res = Net::HTTP.start(url.host, url.port) {|http| | |
http.request(req) | |
} | |
text = res.body | |
rescue => e | |
puts e | |
return | |
else | |
end | |
r = text.scan(/<dt><span id=\"(\d+)\"><\/span>\d+\s(.+?)<\/dt>/) | |
a = [] | |
r.each{|e| | |
a.push(HTTPStatus.new(e[0], e[1])) | |
} | |
text1 = "" | |
text2 = "" | |
a.each{|e| | |
text1 = text1 + sprintf($temp1, e.title, e.code) | |
text2 = text2 + sprintf($temp2, e.title, e.desc) | |
} | |
puts sprintf($source, text1, text2) | |
end | |
$source = <<"EOS" | |
// | |
// HttpStatus.swift | |
// reddift | |
// | |
// Created by HttpStatusGenerator.rb | |
// Generated from wikipedia http://en.wikipedia.org/wiki/List_of_HTTP_status_codes | |
// | |
// https://gist.github.com/sonsongithub/2fc372c869abfdb2719b | |
// | |
import Foundation | |
enum HttpStatus:Int { | |
%s case Unknown = -1 | |
init(code:Int) { | |
let status = HttpStatus(rawValue:code) | |
if let status:HttpStatus = status { | |
self = status | |
} | |
else { | |
self = .Unknown | |
} | |
} | |
var error:NSError { | |
get { | |
return NSError.errorWithCode(self.rawValue, self.description) | |
} | |
} | |
var description:String { | |
get { | |
switch self{ | |
%s default: | |
return "Unknown" | |
} | |
} | |
} | |
} | |
EOS | |
$temp1 = <<"EOS" | |
case %s = %d | |
EOS | |
$temp2 = <<"EOS" | |
case .%s: | |
return "%s" | |
EOS | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment