Class: HTTPThumbnailerClient::ThumbnailSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/httpthumbnailer-client/thumbnail_spec.rb

Overview

TODO: support for escaping of ! and ,

Defined Under Namespace

Classes: Builder, 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.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 122

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'")
  @method = method
  @width = width
  @height = height
  @format = format
  @options = options
  @edits = edits
end

Instance Attribute Details

#editsObject (readonly)

Returns the value of attribute edits.



105
106
107
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 105

def edits
  @edits
end

#formatObject (readonly)

Returns the value of attribute format.



105
106
107
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 105

def format
  @format
end

#heightObject (readonly)

Returns the value of attribute height.



105
106
107
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 105

def height
  @height
end

#methodObject (readonly)

Returns the value of attribute method.



105
106
107
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 105

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



105
106
107
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 105

def options
  @options
end

#widthObject (readonly)

Returns the value of attribute width.



105
106
107
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 105

def width
  @width
end

Class Method Details

.from_string(string) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 107

def self.from_string(string)
  edits = split_edits(string)
  spec = edits.shift
  args = split_args(spec)
  args, options = partition_args_options(args)
  method, width, height, format = *args.shift(4) # ignore extra 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



168
169
170
171
172
173
174
175
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 168

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



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 156

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



150
151
152
153
154
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 150

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



146
147
148
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 146

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

.split_edits(string) ⇒ Object



142
143
144
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 142

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

Instance Method Details

#to_sObject



138
139
140
# File 'lib/httpthumbnailer-client/thumbnail_spec.rb', line 138

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