Class: GoogleQR

Inherits:
Object
  • Object
show all
Defined in:
lib/google-qr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GoogleQR

Returns a new instance of GoogleQR.



7
8
9
10
11
12
13
14
# File 'lib/google-qr.rb', line 7

def initialize(opts={})
  options = {
    :size => "100x100",
    :use_https => true
  }.merge!(opts)

  options.each {|key,value| self.send("#{key}=", value) }
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/google-qr.rb', line 5

def data
  @data
end

#encodingObject

Returns the value of attribute encoding.



5
6
7
# File 'lib/google-qr.rb', line 5

def encoding
  @encoding
end

#error_correctionObject

Returns the value of attribute error_correction.



5
6
7
# File 'lib/google-qr.rb', line 5

def error_correction
  @error_correction
end

#html_optionsObject

Returns the value of attribute html_options.



5
6
7
# File 'lib/google-qr.rb', line 5

def html_options
  @html_options
end

#marginObject

Returns the value of attribute margin.



5
6
7
# File 'lib/google-qr.rb', line 5

def margin
  @margin
end

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/google-qr.rb', line 5

def size
  @size
end

#use_httpsObject

Returns the value of attribute use_https.



5
6
7
# File 'lib/google-qr.rb', line 5

def use_https
  @use_https
end

Instance Method Details

#renderObject



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

def render
  if self.size
    height, width = self.size.split('x')
    dimensions = " height='#{height}' width='#{width}' "
  else
    dimensions = nil
  end

  if self.html_options
    html_options = ""

    self.html_options.each do |k, v|
      if k.to_s.length > 0 && v.to_s.length > 0
        html_options += k.to_s + "='" + v.to_s + "' "
      end
    end
  else
    html_options = nil
  end

  "<img src='#{self.to_s}'#{dimensions}#{html_options}/>"
end

#to_sObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/google-qr.rb', line 16

def to_s
  if self.data
    params = ["chl=#{escape_string(self.data)}"]
    params << "chs=#{self.size}"
    params << "choe=#{self.encoding}" if self.encoding
    params << error_correction_params if error_correction_param?
    base_url + params.join("&")
  else
    raise "Attribute @data is required for GoogleQR code"
  end
end