Class: Configuration::RequestState

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

Defined Under Namespace

Classes: Images

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, matches, path, query_string, request_uri, request_headers, memory_limit, forward_headers) ⇒ RequestState

Returns a new instance of RequestState.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/httpimagestore/configuration/request_state.rb', line 27

def initialize(body, matches, path, query_string, request_uri, request_headers, memory_limit, forward_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

	# it is OK to overwrite path with a match
	self[:path] = path

	merge! matches

	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)
	@query_string = query_string
	@request_uri = request_uri
	@request_headers = request_headers
	@memory_limit = memory_limit
	@output_callback = nil

	@forward_headers = forward_headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



51
52
53
# File 'lib/httpimagestore/configuration/request_state.rb', line 51

def body
  @body
end

#forward_headersObject (readonly)

Returns the value of attribute forward_headers.



57
58
59
# File 'lib/httpimagestore/configuration/request_state.rb', line 57

def forward_headers
  @forward_headers
end

#imagesObject (readonly)

Returns the value of attribute images.



52
53
54
# File 'lib/httpimagestore/configuration/request_state.rb', line 52

def images
  @images
end

#memory_limitObject (readonly)

Returns the value of attribute memory_limit.



53
54
55
# File 'lib/httpimagestore/configuration/request_state.rb', line 53

def memory_limit
  @memory_limit
end

#query_stringObject (readonly)

Returns the value of attribute query_string.



54
55
56
# File 'lib/httpimagestore/configuration/request_state.rb', line 54

def query_string
  @query_string
end

#request_headersObject (readonly)

Returns the value of attribute request_headers.



56
57
58
# File 'lib/httpimagestore/configuration/request_state.rb', line 56

def request_headers
  @request_headers
end

#request_uriObject (readonly)

Returns the value of attribute request_uri.



55
56
57
# File 'lib/httpimagestore/configuration/request_state.rb', line 55

def request_uri
  @request_uri
end

Instance Method Details

#fetch_base_variable(name, base_name) ⇒ Object



73
74
75
# File 'lib/httpimagestore/configuration/request_state.rb', line 73

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/httpimagestore/configuration/request_state.rb', line 77

def generate_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
	when :query_string_options
		query_string.sort.map{|kv| kv.join(':')}.join(',')
	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



65
66
67
# File 'lib/httpimagestore/configuration/request_state.rb', line 65

def output(&callback)
	@output_callback = callback
end

#output_callbackObject



69
70
71
# File 'lib/httpimagestore/configuration/request_state.rb', line 69

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

#with_locals(*locals) ⇒ Object



59
60
61
62
63
# File 'lib/httpimagestore/configuration/request_state.rb', line 59

def with_locals(*locals)
	locals = locals.reduce{|a, b| a.merge(b)}
	log.debug "using additional local variables: #{locals}"
	self.dup.merge!(locals)
end