Created
April 7, 2013 09:57
-
-
Save ykhs/5329842 to your computer and use it in GitHub Desktop.
www.colourlovers.com の palette を取得して Sass の変数定義の形へ整形
This file contains hidden or 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
require 'net/http' | |
require 'rexml/document' | |
class Palette | |
attr_accessor :name, :url, :colors | |
def initialize(id) | |
@name = String.new | |
@url = String.new | |
@colors = Array.new | |
http = Net::HTTP.new("www.colourlovers.com", 80) | |
http.start do |http| | |
req = Net::HTTP::Get.new("/api/palette/#{id}") | |
res = http.request(req) | |
xml = REXML::Document.new(res.body) | |
@name = xml.elements["palettes/palette/title"].text | |
@url = xml.elements["palettes/palette/url"].text | |
xml.elements.each("palettes/palette/colors/hex") do |element| | |
hex = element.text | |
color = Color.new(hex) | |
@colors.push(color) | |
end | |
end | |
end | |
end | |
class Color | |
attr_accessor :name, :hex | |
def initialize(hex) | |
@name = String.new | |
@hex = String.new | |
http = Net::HTTP.new("www.colourlovers.com", 80) | |
http.start do |http| | |
req = Net::HTTP::Get.new("/api/color/#{hex}") | |
res = http.request(req) | |
xml = REXML::Document.new(res.body) | |
name = xml.elements["colors/color/title"].text | |
@name = name.downcase.gsub("\s", "_") | |
@hex = hex | |
end | |
end | |
end | |
id = ARGV.shift || 113451 | |
palette = Palette.new(id) | |
result = <<"EOS" | |
// Color Palette | |
// @name #{palette.name} | |
// @url #{palette.url} | |
EOS | |
palette.colors.each do |color| | |
result += "$#{color.name}: \##{color.hex};\n" | |
end | |
puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment