Class: QRCodeIntent

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

Defined Under Namespace

Classes: Cryptocurrency, Email, Sms, Twitter, WiFi, Zoom

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, event: nil, contact: nil, website: nil, wifi: nil, location: nil, sms: nil, phone: nil, email: nil, bitcoin: nil, twitter: nil, zoom: nil, debug: false) ⇒ QRCodeIntent

Returns a new instance of QRCodeIntent.



65
66
67
68
69
70
71
72
73
74
75
76
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
# File 'lib/qrcode_intent.rb', line 65

def initialize(obj=nil, event: nil, contact: nil, website: nil, 
               wifi: nil, location: nil, sms: nil, phone: nil, 
               email: nil, bitcoin: nil, twitter: nil, 
               zoom: nil, debug: false)
  
  @debug = debug
  
  if website then
  
    add_website website
  
  elsif location
    
    add_location location
    
  elsif email
    
    add_email email
    
  elsif event 
    
    add_event event
    
  elsif contact
    
    add_contact contact

  elsif wifi
    
    add_wifi wifi
    
  elsif sms
    
    add_sms sms
    
  elsif phone
    
    add_tel phone

  elsif bitcoin
    
    add_cryptocurrency bitcoin            
    
  elsif twitter
    
    add_twitter twitter
    
  elsif zoom
    
    add_zoom zoom
          
  else
    
    if obj.is_a? String then
      obj
    elsif obj.is_a? Hash
      obj[:start] ? add_event(obj) : add_contact(obj)
    end
    
  end
  
end

Instance Attribute Details

#to_sObject (readonly)

Returns the value of attribute to_s.



56
57
58
# File 'lib/qrcode_intent.rb', line 56

def to_s
  @to_s
end

Instance Method Details

#add_contact(obj = nil, &blk) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/qrcode_intent.rb', line 128

def add_contact(obj=nil, &blk)
  
  @to_s = if block_given? then
    
    SimpleVpim.new(:contact, &blk).to_vcard.to_s
    
  elsif obj
    
    SimpleVpim.new(obj).to_vcard.to_s
    
  end
  
end

#add_cryptocurrency(obj = nil, &blk) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/qrcode_intent.rb', line 142

def add_cryptocurrency(obj=nil, &blk)
  
  h = if block_given? then
    
    c = Cryptocurrency.new
    yield c
    c.to_h

    
  elsif obj.is_a? Hash
    
    obj
    
  end
  
  # a type can be bitcoin, bitcoincash, ethereum, litecoin, dash etc.
  #
  type = h[:type] || 'bitcoin'
  
  s = "%s:%s?%s" % [type, h[:receiver], h[:amount]]
  s += "&message=%s" % h[:msg] if h[:msg]
  
  @to_s = s
  
end

#add_email(obj = nil, &blk) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/qrcode_intent.rb', line 168

def add_email(obj=nil, &blk)
  
  h = if block_given? then
    
    email = Email.new
    yield email
    email.to_h

    
  elsif obj.is_a? Hash
    
    obj
    
  end
  
  @to_s = "MATMSG:TO:%s;SUB:%s;BODY:%s;;" % [h[:to], h[:subject], h[:body]]
  
end

#add_event(obj = nil, &blk) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/qrcode_intent.rb', line 187

def add_event(obj=nil, &blk)
  
  @to_s = if block_given? then
    
    SimpleVpim.new(:event, &blk).to_vevent
    
  elsif obj
    
    SimpleVpim.new(obj).to_vevent
    
  end
  
end

#add_location(s) ⇒ Object

e.g. add_location ‘55.8835644,-3.2312978’



203
204
205
# File 'lib/qrcode_intent.rb', line 203

def add_location(s)
  @to_s = 'geo:' + s
end

#add_sms(obj = nil, &blk) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/qrcode_intent.rb', line 213

def add_sms(obj=nil, &blk)
  
  h = if block_given? then
    
    sms = Sms.new
    yield sms
    sms.to_h

    
  elsif obj.is_a? Hash
    
    obj
    
  end
  
  @to_s = "SMSTO:%s:%s" % [h[:number], h[:msg]]
  
end

#add_tel(s = nil, number: s) ⇒ Object

e.g. add_tel ‘0131 542 4923’



234
235
236
# File 'lib/qrcode_intent.rb', line 234

def add_tel(s=nil, number: s)
  @to_s = 'tel:' + number
end

#add_twitter(obj = nil, &blk) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/qrcode_intent.rb', line 238

def add_twitter(obj=nil, &blk)
  
  h = if block_given? then
    
    c = Twitter.new
    yield c
    c.to_h

    
  elsif obj.is_a? Hash
    
    obj
    
  end
  
  @to_s = if h[:msg] then
    "https://twitter.com/intent/tweet?text=" % URI.encode(h[:msg])
  elsif h[:username]
    "https://twitter.com/%s" % h[:username]
  end
  
  @to_s = s
  
end

#add_website(s) ⇒ Object

e.g. add_website ‘news.bbc.co.uk/’



209
210
211
# File 'lib/qrcode_intent.rb', line 209

def add_website(s)
  @to_s = s
end

#add_wifi(obj = nil, &blk) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/qrcode_intent.rb', line 282

def add_wifi(obj=nil, &blk)
  
  h = if block_given? then
    
    wifi = WiFi.new(nil, 'WPA', nil)
    yield wifi
    wifi.to_h

    
  elsif obj.is_a? Hash
    
    obj
    
  end
  
  @to_s = "WIFI:S:%s;T:%s;P:%s;;" % [h[:ssid], h[:type], h[:password]]
  
end

#add_zoom(obj = nil, &blk) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/qrcode_intent.rb', line 263

def add_zoom(obj=nil, &blk)
  
  h = if block_given? then
    
    c = Zoom.new
    yield c
    c.to_h

    
  elsif obj.is_a? Hash
    
    obj
    
  end
  
  @to_s = "https://zoom.us/j/%s?pwd=%s" % [h[:meetingid], h[:password]]
  
end

#to_svgObject



301
302
303
# File 'lib/qrcode_intent.rb', line 301

def to_svg()
  RQRCode::QRCode.new(@to_s).as_svg viewbox: true
end