Class: Configuration::RequestState

Inherits:
Hash
  • Object
show all
Includes:
ClassLogging
Defined in:
lib/httpimagestore/configuration/handler.rb

Defined Under Namespace

Classes: Images

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = '', matches = {}, path = '', query_string = {}, memory_limit = MemoryLimit.new, headers = {}) ⇒ RequestState

Returns a new instance of RequestState.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/httpimagestore/configuration/handler.rb', line 63

def initialize(body = '', matches = {}, path = '', query_string = {}, memory_limit = MemoryLimit.new, headers = {})
	super() do |request_state, name|
		# note that request_state may be different object when useing with_locals that creates duplicate
		request_state[name] = request_state.generate_meta_variable(name) or raise VariableNotDefinedError.new(name)
	end

	self[:path] = path
	merge! matches
	self[:query_string_options] = query_string.sort.map{|kv| kv.join(':')}.join(',')

	log.debug "processing request with body length: #{body.bytesize} bytes and variables: #{map{|k,v| "#{k}: '#{v}'"}.join(', ')}"

	@body = body
	@images = Images.new(memory_limit)
	@memory_limit = memory_limit
	@output_callback = nil

	@headers = headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



83
84
85
# File 'lib/httpimagestore/configuration/handler.rb', line 83

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



86
87
88
# File 'lib/httpimagestore/configuration/handler.rb', line 86

def headers
  @headers
end

#imagesObject (readonly)

Returns the value of attribute images.



84
85
86
# File 'lib/httpimagestore/configuration/handler.rb', line 84

def images
  @images
end

#memory_limitObject (readonly)

Returns the value of attribute memory_limit.



85
86
87
# File 'lib/httpimagestore/configuration/handler.rb', line 85

def memory_limit
  @memory_limit
end

Instance Method Details

#fetch_base_variable(name, base_name) ⇒ Object



101
102
103
# File 'lib/httpimagestore/configuration/handler.rb', line 101

def fetch_base_variable(name, base_name)
	fetch(base_name, nil) or generate_meta_variable(base_name) or raise NoVariableToGenerateMetaVariableError.new(base_name, name)
end

#generate_meta_variable(name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/httpimagestore/configuration/handler.rb', line 105

def generate_meta_variable(name)
	log.debug  "generating meta variable: #{name}"
	val = case name
	when :basename
		path = Pathname.new(fetch_base_variable(name, :path))
		path.basename(path.extname).to_s
	when :dirname
		Pathname.new(fetch_base_variable(name, :path)).dirname.to_s
	when :extension
		Pathname.new(fetch_base_variable(name, :path)).extname.delete('.')
	when :digest # deprecated
		@body.empty? and raise NoRequestBodyToGenerateMetaVariableError.new(name)
		Digest::SHA2.new.update(@body).to_s[0,16]
	when :input_digest
		@body.empty? and raise NoRequestBodyToGenerateMetaVariableError.new(name)
		Digest::SHA2.new.update(@body).to_s[0,16]
	when :input_sha256
		@body.empty? and raise NoRequestBodyToGenerateMetaVariableError.new(name)
		Digest::SHA2.new.update(@body).to_s
	when :input_image_width
		@images['input'].width or raise NoImageDataForVariableError.new('input', name)
	when :input_image_height
		@images['input'].height or raise NoImageDataForVariableError.new('input', name)
	when :input_image_mime_extension
		@images['input'].mime_extension or raise NoImageDataForVariableError.new('input', name)
	when :image_digest
		Digest::SHA2.new.update(@images[fetch_base_variable(name, :image_name)].data).to_s[0,16]
	when :image_sha256
		Digest::SHA2.new.update(@images[fetch_base_variable(name, :image_name)].data).to_s
	when :mimeextension # deprecated
		image_name = fetch_base_variable(name, :image_name)
		@images[image_name].mime_extension or raise NoImageDataForVariableError.new(image_name, name)
	when :image_mime_extension
		image_name = fetch_base_variable(name, :image_name)
		@images[image_name].mime_extension or raise NoImageDataForVariableError.new(image_name, name)
	when :image_width
		image_name = fetch_base_variable(name, :image_name)
		@images[image_name].width or raise NoImageDataForVariableError.new(image_name, name)
	when :image_height
		image_name = fetch_base_variable(name, :image_name)
		@images[image_name].height or raise NoImageDataForVariableError.new(image_name, name)
	when :uuid
		SecureRandom.uuid
	end
	if val
		log.debug  "generated meta variable '#{name}': #{val}"
	else
		log.debug  "could not generated meta variable '#{name}'"
	end
	val
end

#output(&callback) ⇒ Object



93
94
95
# File 'lib/httpimagestore/configuration/handler.rb', line 93

def output(&callback)
	@output_callback = callback
end

#output_callbackObject



97
98
99
# File 'lib/httpimagestore/configuration/handler.rb', line 97

def output_callback
	@output_callback or fail 'no output callback'
end

#with_locals(locals) ⇒ Object



88
89
90
91
# File 'lib/httpimagestore/configuration/handler.rb', line 88

def with_locals(locals)
	log.debug "using additional local variables: #{locals}"
	self.dup.merge!(locals)
end