class SwishQr
Interface for generating Swish QR codes developer.getswish.se/qr-api-manual/4-create-qr-codes-using-swish-qr-code-generator-apis/#4-1-1-prefilled developer.getswish.se/content/uploads/2017/04/Guide-Swish-QR-code-design-specification_v1.5.pdf
Constants
- COMPLEX_VALUES
- SIMPLE_VALUES
- SWISH_QR_API_ENDPOINT
- VALID_FORMATS
Attributes
data_hash[RW]
Public Class Methods
new(args)
click to toggle source
# File lib/swish_qr.rb, line 18 def initialize(args) check_args(args) @result = HTTParty.post(SWISH_QR_API_ENDPOINT, :body => build_data_hash(args), :headers => { 'Content-Type' => 'application/json' } ) end
Public Instance Methods
error()
click to toggle source
# File lib/swish_qr.rb, line 37 def error @result.parsed_response['error'] end
http_response_code()
click to toggle source
# File lib/swish_qr.rb, line 33 def http_response_code @result.code end
image()
click to toggle source
# File lib/swish_qr.rb, line 25 def image @result.body end
success?()
click to toggle source
# File lib/swish_qr.rb, line 29 def success? @result.code == 200 end
Private Instance Methods
build_data_hash(args)
click to toggle source
# File lib/swish_qr.rb, line 66 def build_data_hash(args) @image_format = args[:format] @data_hash = get_simple_values_from_args(args) @data_hash.merge!(get_complex_values_from_args(args)) sanitise_amount @data_hash.to_json end
check_args(args)
click to toggle source
# File lib/swish_qr.rb, line 43 def check_args(args) check_format(args) check_jpeg_transparency(args) check_border(args) check_size(args) end
check_border(args)
click to toggle source
# File lib/swish_qr.rb, line 54 def check_border(args) raise ArgumentError, "Max border is 4" if (args[:border].to_i > 4) end
check_format(args)
click to toggle source
# File lib/swish_qr.rb, line 62 def check_format(args) raise ArgumentError, "Invalid format: '#{args[:format]}', must specify one of: #{VALID_FORMATS}" unless VALID_FORMATS.include?(args[:format]) end
check_jpeg_transparency(args)
click to toggle source
# File lib/swish_qr.rb, line 50 def check_jpeg_transparency(args) raise ArgumentError, "JPEG can't be transparent" if (args[:format] == 'jpg' && args[:transparent]) end
check_size(args)
click to toggle source
# File lib/swish_qr.rb, line 58 def check_size(args) raise ArgumentError, "Size (minimum 300) must be specified for #{args[:format]}" unless (args[:format] == 'svg' || args[:size].to_i >= 300) end
get_complex_values_from_args(args)
click to toggle source
# File lib/swish_qr.rb, line 89 def get_complex_values_from_args(args) ret = {} COMPLEX_VALUES.each do |k| ret[k] = { value: args[k], editable: ((args[:editable]||[]).include?(k)) } if args[k] end ret end
get_simple_values_from_args(args)
click to toggle source
# File lib/swish_qr.rb, line 85 def get_simple_values_from_args(args) args.select{|k,v| SIMPLE_VALUES.include?(k)} end
sanitise_amount()
click to toggle source
# File lib/swish_qr.rb, line 74 def sanitise_amount return unless @data_hash[:amount] # Delete amount if it's blank / 0, to ensure that the request is valid if @data_hash[:amount][:value].to_i == 0 @data_hash.delete(:amount) return end # This is to support BigDecimal, which would otherwise get misrepresented as a string in the JSON @data_hash[:amount][:value] = @data_hash[:amount][:value].to_f unless @data_hash[:amount][:value].is_a?(Integer) end