Class: ImageReq

Inherits:
Object
  • Object
show all
Defined in:
lib/profilepic/builder.rb

Overview

(Generate) Image Request

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, attributes:) ⇒ ImageReq

Returns a new instance of ImageReq.



73
74
75
76
# File 'lib/profilepic/builder.rb', line 73

def initialize( name:, attributes: )
   @name       = name
   @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



71
72
73
# File 'lib/profilepic/builder.rb', line 71

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



71
72
73
# File 'lib/profilepic/builder.rb', line 71

def name
  @name
end

Class Method Details

._build_attributes(params, spec) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/profilepic/builder.rb', line 101

def self._build_attributes( params, spec )
  attributes = []
  spec.each do |name,h|

    value = params[name]

    if value.nil? || value.empty?
      required =  h[:none] == true   ## check if value is required or optional
      if required
         raise ArgumentError, "required attribute/param >#{name}< missing"
      else
         next   ## skip optional attributes
      end
    else
        key = _norm_key( value )
        attributes << key       if key != 'none'
    end
  end
  attributes
end

._norm_key(str) ⇒ Object

(static) helpers



97
98
99
# File 'lib/profilepic/builder.rb', line 97

def self._norm_key( str )
  str.downcase.strip
end

._parse_attributes(str) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/profilepic/builder.rb', line 123

def self._parse_attributes( str )
  # convert attributes to array
  ##   allow various separators
  attributes = str.split( %r{[,;|+/]+} )
  attributes = attributes.map { |attr| attr.strip }
  attributes = attributes.select { |attr| !attr.empty?}   ## remove empty strings (if any)
  attributes
end

.build(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/profilepic/builder.rb', line 9

def self.build( params )
  puts "==> image request params:"
  pp params

   name       = _norm_key( params[:t] || 'punk'  )
   attributes = _parse_attributes( params[:attributes] || '' )

    new( name: name,
         attributes: attributes )
end

.build_doge(params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/profilepic/builder.rb', line 44

def self.build_doge( params )
  puts "==> image request params:"
  pp params

    name = 'doge'

    attributes = _build_attributes( params, DOGE )

    new( name: name,
         attributes:  attributes )
end

.build_marc(params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/profilepic/builder.rb', line 20

def self.build_marc( params )
  puts "==> image request params:"
  pp params

    name = 'marc'
    attributes = _build_attributes( params, MARC )

    new( name: name,
         attributes:  attributes )
end

.build_saudi(params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/profilepic/builder.rb', line 32

def self.build_saudi( params )
  puts "==> image request params:"
  pp params

    name = 'saudi'
    attributes = _build_attributes( params, SAUDI )

    new( name: name,
         attributes:  attributes )
end

.build_yeoldepunk(params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/profilepic/builder.rb', line 57

def self.build_yeoldepunk( params )
  puts "==> image request params:"
  pp params

    name = 'yeoldepunk'

    attributes = _build_attributes( params, YEOLDEPUNK )

    new( name: name,
         attributes:  attributes )
end

Instance Method Details

#imageObject



78
79
80
81
# File 'lib/profilepic/builder.rb', line 78

def image
  ## check - cache / memoize image - why? why not?
  Original::Image.fabricate( @name, *@attributes )
end

#image_blobObject Also known as: blob



83
# File 'lib/profilepic/builder.rb', line 83

def image_blob() image.to_blob;  end

#image_keyObject Also known as: key



86
87
88
89
# File 'lib/profilepic/builder.rb', line 86

def image_key
   @key ||= "#{@name}#{Time.now.to_i}"
   @key
end