galakei 0.6.6 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ class Galakei::SpacerController < ActionController::Base
2
2
  def create
3
3
  respond_to do |format|
4
4
  format.gif do
5
- send_data(Galakei::Spacer.new(params[:color]).create, :disposition => "inline", :type => :gif)
5
+ send_data(Galakei::Spacer.create_gif(params[:color]), :disposition => "inline", :type => :gif)
6
6
  end
7
7
  end
8
8
  end
@@ -120,7 +120,7 @@ EOD
120
120
  def self.apply(element, property,value)
121
121
  color = value.split(/\s/).last.split('#').last
122
122
  thickness = value.match(/(\d+)px/)[1]
123
- img = Galakei::Spacer.new(color).img_tag(:height => thickness)
123
+ img = Galakei::Spacer.img_tag(:color => color, :height => thickness)
124
124
 
125
125
  case property
126
126
  when 'border'
@@ -131,6 +131,8 @@ EOD
131
131
  when 'border-bottom'
132
132
  element.after(img)
133
133
  end
134
+ rescue => err
135
+ Rails.logger.warn("could not insert spacer gif: #{err}")
134
136
  end
135
137
  end
136
138
  end
@@ -1,54 +1,46 @@
1
1
  module Galakei
2
- class Spacer
3
- HEX = [
4
- '47494638396101000100f70000',
5
- 'f'*1530,
6
- '2c000000000100010000080400010404003b'
7
- ]
2
+ module Spacer
8
3
 
9
- def initialize(color)
10
- @color = color.gsub('#','')
11
- end
4
+ GIF_DATA_PREFIX = ['47494638396101000100f70000'].pack('H*')
5
+ GIF_DATA_POSTFIX = ['f'*1530 + '2c000000000100010000080400010404003b'].pack('H*')
12
6
 
13
- def create
14
- num = hex.size
15
- raise 'invalid hex' if num % 2 != 0
16
- bin = ''
17
- (num/2).times do |i|
18
- bin += (hex[2 * (i + 1) - 2,2]).to_i(16).chr
19
- end
20
- return bin
21
- end
7
+ NAMED_COLORS = {
8
+ 'maroon' => '800000',
9
+ 'red' => 'ff0000',
10
+ 'orange' => 'ffA500',
11
+ 'yellow' => 'ffff00',
12
+ 'olive' => '808000',
13
+ 'purple' => '800080',
14
+ 'fuchsia' => 'ff00ff',
15
+ 'white' => 'ffffff',
16
+ 'lime' => '00ff00',
17
+ 'green' => '008000',
18
+ 'navy' => '000080',
19
+ 'blue' => '0000ff',
20
+ 'aqua' => '00ffff',
21
+ 'teal' => '008080',
22
+ 'black' => '000000',
23
+ 'silver' => 'c0c0c0',
24
+ 'gray' =>'808080',
25
+ 'black' => '000000' }
22
26
 
23
- def hex
24
- hex_color = case @color
25
- when 'maroon'; '800000'
26
- when 'red'; 'ff0000'
27
- when 'orange'; 'ffA500'
28
- when 'yellow'; 'ffff00'
29
- when 'olive'; '808000'
30
- when 'purple'; '800080'
31
- when 'fuchsia'; 'ff00ff'
32
- when 'white'; 'ffffff'
33
- when 'lime'; '00ff00'
34
- when 'green'; '008000'
35
- when 'navy'; '000080'
36
- when 'blue'; '0000ff'
37
- when 'aqua'; '00ffff'
38
- when 'teal'; '008080'
39
- when 'black'; '000000'
40
- when 'silver'; 'c0c0c0'
41
- when 'gray'; '808080'
42
- when 'black'; '000000'
43
- else @color
44
- end
45
- HEX[0] + hex_color + HEX[1] + HEX[2]
27
+ def self.create_gif(hex_color)
28
+ raise "invalid color '#{hex_color}'" unless /[0-9a-f]{6}/i =~ hex_color
29
+ GIF_DATA_PREFIX + [hex_color].pack('H*') + GIF_DATA_POSTFIX
46
30
  end
47
31
 
48
- def img_tag(options = {})
32
+ def self.img_tag(options = {})
49
33
  width = options[:width] || '100%'
50
34
  height = options[:height] || 1
51
- "<img src='/galakei/spacer/#{@color}' width='#{width}' height='#{height}'>"
35
+ hex_color = color2hex(options[:color])
36
+ "<img src='/galakei/spacer/#{hex_color}' width='#{width}' height='#{height}'>"
37
+ end
38
+
39
+ def self.color2hex(color)
40
+ hex_color = NAMED_COLORS[color] || color.gsub('#','').downcase
41
+ hex_color = hex_color.sub(/(.)(.)(.)/, '\1\1\2\2\3\3') if hex_color.size == 3
42
+ raise "invalid color '#{color}'" unless /[0-9a-f]{6}/i =~ hex_color
43
+ hex_color
52
44
  end
53
45
  end
54
46
  end
@@ -1,3 +1,3 @@
1
1
  module Galakei
2
- VERSION = "0.6.6"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -223,6 +223,15 @@ EOD
223
223
  it_should_behave_like 'border'
224
224
  end
225
225
 
226
+ context 'border invalid color' do
227
+ let(:css) { "div { border: 1px solid #blubb; } "}
228
+ it "should apply nothing" do
229
+ elm = subject
230
+ elm.previous_sibling.to_s.should == ''
231
+ elm.next_sibling.to_s.should == ''
232
+ end
233
+ end
234
+
226
235
  context 'border-top' do
227
236
  let(:css) { "div { border-top: 1px solid #000000; } "}
228
237
  it_should_behave_like 'border top'
@@ -1,16 +1,29 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Galakei::Spacer do
4
- context "#000000" do
5
- subject { described_class.new("#000000") }
6
- it { subject.img_tag.should == %Q[<img src='/galakei/spacer/000000' width='100%' height='1'>] }
7
- it { subject.create.should_not be_nil }
4
+
5
+ context "img_tag" do
6
+ { "#000000" => "000000",
7
+ "black" => "000000",
8
+ "aqua" => "00ffff",
9
+ "F0d" => "ff00dd",
10
+ }.each do |name, col|
11
+ it "should create correct markup for color #{name}" do
12
+ subject.img_tag(:color => name).should == %Q[<img src='/galakei/spacer/#{col}' width='100%' height='1'>]
13
+ end
14
+ end
15
+
16
+ it "shouldn't allow invalid colors" do
17
+ lambda { subject.img_tag(:color => "bla") }.should raise_error(RuntimeError)
18
+ end
8
19
  end
9
- %w[000000 aqua black blue fuchsia gray green lime maroon navy olive orange purple red silver teal white yellow].each do |s|
10
- context s do
11
- subject { described_class.new(s) }
12
- it { subject.img_tag.should == %Q[<img src='/galakei/spacer/#{s}' width='100%' height='1'>] }
13
- it { subject.create.should_not be_nil }
20
+
21
+ BLACK_GIF = "GIF89a\x01\x00\x01\x00\xF7\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF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\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF,\x00\x00\x00\x00\x01\x00\x01\x00\x00\b\x04\x00\x01\x04\x04\x00;"
22
+
23
+ context "create gif" do
24
+ it("should create a gif for hex") { subject.create_gif("000000").should == BLACK_GIF }
25
+ it("shouldn't allow invalid colors") do
26
+ lambda { subject.create_gif("bla") }.should raise_error(RuntimeError)
14
27
  end
15
- end
28
+ end
16
29
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: galakei
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.6
5
+ version: 0.7.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paul McMahon