Class: ThumbnailSpec

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

Defined Under Namespace

Classes: EditSpec, InvalidArgumentValueError, InvalidFormatError, MissingArgumentError, MissingOptionKeyNameError, MissingOptionKeyValueError, MissingOptionKeyValuePairError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, width, height, format, options = {}, edits = []) ⇒ ThumbnailSpec

Returns a new instance of ThumbnailSpec.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 115

def initialize(method, width, height, format, options = {}, edits = [])
  method.nil? or method.empty? and raise MissingArgumentError, 'method'
  width.nil? or width.empty? and raise MissingArgumentError, 'width'
  height.nil? or height.empty? and raise MissingArgumentError, 'height'
  format.nil? or format.empty? and raise MissingArgumentError, 'format'

  width !~ /^([0-9]+|input)$/ and raise InvalidArgumentValueError.new('width', width, "an integer or 'input'")
  height !~ /^([0-9]+|input)$/ and raise InvalidArgumentValueError.new('height', height, "an integer or 'input'")

  width = width == 'input' ? :input : width.to_i
  height = height == 'input' ? :input : height.to_i

  format = format == 'input' ? :input : format.upcase

  @method = method
  @width = width
  @height = height
  @format = format
  @options = options
  @edits = edits
end

Instance Attribute Details

#editsObject (readonly)

Returns the value of attribute edits.



99
100
101
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 99

def edits
  @edits
end

#formatObject (readonly)

Returns the value of attribute format.



99
100
101
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 99

def format
  @format
end

#heightObject (readonly)

Returns the value of attribute height.



99
100
101
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 99

def height
  @height
end

#methodObject (readonly)

Returns the value of attribute method.



99
100
101
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 99

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



99
100
101
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 99

def options
  @options
end

#widthObject (readonly)

Returns the value of attribute width.



99
100
101
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 99

def width
  @width
end

Class Method Details

.from_string(string) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 101

def self.from_string(string)
  edits = split_edits(string)
  spec = edits.shift
  args = split_args(spec)
  method, width, height, format, *options = *args

  options = parse_options(options)
  edits = edits.map{|e| EditSpec.from_string(e)}

  new(method, width, height, format, options, edits)
rescue InvalidFormatError => error
  raise error.in_spec(string)
end

.options_to_s(options) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 167

def self.options_to_s(options)
  options.sort_by{|k,v| k}.map do |key, value|
    raise MissingOptionKeyNameError, value if key.nil? or key.to_s.empty?
    raise MissingOptionKeyValueError, key if value.nil? or value.to_s.empty?
    key = key.to_s.gsub('_', '-') if key.kind_of? Symbol
    "#{key}:#{value}"
  end
end

.parse_options(options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 155

def self.parse_options(options)
  Hash[options.map.with_index do |pair, index|
    pair.empty? and raise MissingOptionKeyValuePairError, index
    pair.split(':', 2)
  end].tap do |map|
    map.each do |key, value|
      key.nil? or key.empty? and raise MissingOptionKeyNameError, value
      value.nil? or value.empty? and raise MissingOptionKeyValueError, key
    end
  end
end

.partition_args_options(args) ⇒ Object



149
150
151
152
153
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 149

def self.partition_args_options(args)
  options = args.drop_while{|a| not a.include?(':')}
  args = args.take_while{|a| not a.include?(':')}
  [args, options]
end

.split_args(string) ⇒ Object



145
146
147
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 145

def self.split_args(string)
  string.split(',')
end

.split_edits(string) ⇒ Object



141
142
143
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 141

def self.split_edits(string)
  string.split('!')
end

Instance Method Details

#to_sObject



137
138
139
# File 'lib/httpthumbnailer/thumbnail_specs.rb', line 137

def to_s
  [[@method, @width, @height, @format, *self.class.options_to_s(@options)].join(','), *@edits.map(&:to_s)].join('!')
end