qr4r-0.6.2/0000755000004100000410000000000014467443175012456 5ustar www-datawww-dataqr4r-0.6.2/Gemfile.lock0000644000004100000410000000204614467443175014702 0ustar www-datawww-dataPATH remote: . specs: qr4r (0.6.2) mojo_magick (~> 0.6.5) rqrcode_core (~> 1.0) GEM remote: https://rubygems.org/ specs: ast (2.4.2) json (2.6.3) minitest (5.18.0) mojo_magick (0.6.7) parallel (1.23.0) parser (3.2.2.0) ast (~> 2.4.1) rainbow (3.1.1) rake (13.0.6) regexp_parser (2.8.0) rexml (3.2.5) rqrcode_core (1.2.0) rubocop (1.50.2) json (~> 2.3) parallel (~> 1.10) parser (>= 3.2.0.0) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) rubocop-ast (>= 1.28.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.28.0) parser (>= 3.2.1.0) rubocop-performance (1.17.1) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) ruby-progressbar (1.13.0) unicode-display_width (2.4.2) PLATFORMS ruby DEPENDENCIES minitest (~> 5.14) qr4r! rake (~> 13.0) rubocop (~> 1.0) rubocop-performance (~> 1.8) BUNDLED WITH 2.2.33 qr4r-0.6.2/test/0000755000004100000410000000000014467443175013435 5ustar www-datawww-dataqr4r-0.6.2/test/qr4r_test.rb0000644000004100000410000000661414467443175015720 0ustar www-datawww-datarequire "minitest/autorun" require File.expand_path(File.join(File.dirname(__FILE__), "..", "init")) require "tempfile" class Qr4rTest < MiniTest::Test def test_encode # do something f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("whatever yo", f.path) # assert that it worked assert File.exist?(f.path) r = MojoMagick.get_image_size(f.path) assert r[:height] == 25 * 3 assert r[:width] == 25 * 3 end def test_encode_with_size # do something f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("whatever yo", f.path, size: 4) # assert that it worked assert File.exist?(f) r = MojoMagick.get_image_size(f.path) assert r[:height] == 33 * 3 assert r[:width] == 33 * 3 end def test_encode_with_size_and_border # do something f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("whatever yo", f.path, size: 4, border: 10) # assert that it worked assert File.exist?(f) r = MojoMagick.get_image_size(f.path) assert r[:height] == (33 * 3) + 20 assert r[:width] == (33 * 3) + 20 end def test_encode_with_pixel_size # do something f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("whatever yo", f.path, pixel_size: 5) # assert that it worked assert File.exist?(f) r = MojoMagick.get_image_size(f.path) assert r[:height] == 25 * 5 assert r[:width] == 25 * 5 end def test_encode_with_pixel_size_as_string # do something f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("whatever yo", f.path, pixel_size: "5") # assert that it worked assert File.exist?(f) r = MojoMagick.get_image_size(f.path) assert r[:height] == 25 * 5 assert r[:width] == 25 * 5 end def test_encode_with_size_and_level # do something f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("whatever yo", f.path, size: 4, level: :m) # assert that it worked assert File.exist?(f) r = MojoMagick.get_image_size(f.path) assert r[:height] == 33 * 3 assert r[:width] == 33 * 3 end def test_a_long_string_with_size_thats_too_small caught = false begin f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("this string should also be encodable. don't ya think", f.path, size: 4) rescue StandardError caught = true end assert caught, "Expected an error" end def test_a_long_string_with_size_thats_right f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("this string should also be encodable. don't ya think", f.path, size: 10) assert File.exist?(f) r = MojoMagick.get_image_size(f.path) assert r[:height] == 57 * 3 assert r[:width] == 57 * 3 end def test_a_long_string_without_size f = Tempfile.new(["qr4r", ".png"]) Qr4r.encode("this string should also be encodable. don't ya think", f.path) assert File.exist?(f) r = MojoMagick.get_image_size(f.path) assert r[:height] = 41 * 3 assert r[:width] = 41 * 3 end def test_compute_size test_sizes = [7, 14, 24, 34, 44, 58, 64, 84, 98] test_sizes.each_with_index do |sz, idx| str = "a" * (sz - 1) assert Qr4r.send(:compute_size, str) == idx + 1 str = "a" * sz assert Qr4r.send(:compute_size, str) == idx + 2 end end def test_compute_size_too_big str = "a" * 120 caught = false begin Qr4r.send(:compute_size, str) rescue StandardError => _e caught = true end assert caught, "Expected exception" end end qr4r-0.6.2/README.md0000644000004100000410000000472514467443175013745 0ustar www-datawww-data# QR Encoding via Ruby - with PNG output Leveraging [rqrcode](http://whomwah.github.com/rqrcode/) and [mojo_magick](http://github.com/rcode5/mojo_magick), we've built a very thin gem that generates QR codes in a png file # Include in your project In your 'Gemfile', add gem 'qr4r' In your code, add require 'qr4r' NOTE: you'll need to have ImageMagick installed wherever you plan to run this. The gem depends on [mojo_magick](http://github.com/rcode5/mojo_magick) which uses ImageMagick commandline operations to build the final PNG image. To use it: Qr4r::encode(input_string, output_file_path, options) *input_string* and *output_file_path* should be strings. Options should a list of hash options keyed by symbols. Possible options are: * :pixel_size - specify the size of each 'black' dot in the qrcode. Default = 3 * :border - specify the number of pixels to use for a white border around the outside. Default = 0 To encode the string 'qr codes are the new hotness' like this: require 'qr4r' s = 'qr codes are the new hotness' fname = s.gsub(/\s+/,"_") + ".qr.png" Qr4r::encode(s, fname) Make a bigger QRCode s = 'big qr codes are the new hotness' fname = s.gsub(/\s+/,"_") + ".qr.png" Qr4r::encode(s, fname, :pixel_size => 5) Add a fat border s = 'big qr codes are the new hotness with a border' fname = s.gsub(/\s+/,"_") + ".qr.png" Qr4r::encode(s, fname, :border => 20) You can also checkout the [examples](https://github.com/rcode5/qr4r/tree/master/examples) folder for a little sample commandline script. NOTE: The current implementation or rQRCode (and therefore this wrapper library) supports encoding up to 119 characters. Beyond that, you'll need something a bit more sophisticated. ## Wanna try it out? Check out the Sinatra test app here: https://github.com/rcode5/qr4r_test_app ## Versions/Changelog #### 0.4.0 * Moved to new mojo_magick gem * no longer ruby 1.8.7 compatible * fixed issue with border coming out black ## Authors Original author: [Jon Rogers](http://github.com/bunnymatic) [at rcode5](http://www.rcode5.com) Thanks to [Duncan Robertson](http://whomwah.github.com/rqrcode/) for writing rQRCode ## Contributing * Fork the project * Write a test for your new feature/method * Send a pull request * Don't bump the version or modify the gemspec. I'll do that when I merge in your mods and release a new version. ## Copyright MIT Licence (http://www.opensource.org/licenses/mit-license.html) qr4r-0.6.2/qr4r.gemspec0000644000004100000410000000205714467443175014717 0ustar www-datawww-data$LOAD_PATH.push File.expand_path("lib", __dir__) require "qr4r/version" Gem::Specification.new do |s| s.required_ruby_version = ">= 2.5.0" s.name = "qr4r" s.licenses = ["WTFPL"] s.version = Qr4r::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Jon Rogers"] s.email = ["jon@rcode5.com"] s.homepage = "http://github.com/rcode5/qr4r" s.summary = "qr4r-#{Qr4r::VERSION}" s.description = "QR PNG Generator for Ruby. Leveraging RQRCode and MojoMagick modules" s.rubyforge_project = "qr4r" s.files = `git ls-files`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } s.require_paths = ["lib"] s.add_dependency("mojo_magick", "~> 0.6.5") s.add_dependency("rqrcode_core", "~> 1.0") s.add_development_dependency("minitest", "~> 5.14") s.add_development_dependency("rake", "~> 13.0") s.add_development_dependency("rubocop", "~> 1.0") s.add_development_dependency("rubocop-performance", "~> 1.8") s.metadata["rubygems_mfa_required"] = "true" end qr4r-0.6.2/.rubocop.yml0000644000004100000410000000031314467443175014725 0ustar www-datawww-datarequire: - rubocop-performance inherit_from: - ./.rubocop-common.yml # copied from c5-conventions - ./.rubocop-performance.yml # copied from c5-conventions AllCops: TargetRubyVersion: 2.5 qr4r-0.6.2/init.rb0000644000004100000410000000011314467443175013741 0ustar www-datawww-datarequire File.expand_path(File.join(File.dirname(__FILE__), "lib", "qr4r")) qr4r-0.6.2/.gitignore0000644000004100000410000000001414467443175014441 0ustar www-datawww-data.bundle/ *~ qr4r-0.6.2/.rubocop-performance.yml0000644000004100000410000000004614467443175017227 0ustar www-datawww-dataPerformance/Casecmp: Enabled: false qr4r-0.6.2/.rubocop-common.yml0000644000004100000410000000315414467443175016221 0ustar www-datawww-dataAllCops: NewCops: enable DisplayCopNames: true DisplayStyleGuide: true Exclude: - "bin/*" - "db/schema.rb" - "lib/templates/**/*" - "**/node_modules/**/*" - "tmp/**/*" - "vendor/**/*" - "log/**/*" Layout/CaseIndentation: Enabled: false Layout/FirstArrayElementIndentation: EnforcedStyle: consistent Layout/HashAlignment: Enabled: false Layout/LineLength: Max: 120 Layout/MultilineMethodCallIndentation: EnforcedStyle: indented Lint/AmbiguousBlockAssociation: Enabled: false Lint/ScriptPermission: Exclude: - "Rakefile" Metrics/AbcSize: Max: 35 Exclude: - "spec/**/*" Metrics/BlockLength: CountComments: false Max: 50 Exclude: - "config/**/*" - "spec/**/*" Metrics/ClassLength: Max: 250 Exclude: - "spec/**/*" Metrics/MethodLength: Max: 25 Exclude: - "db/migrate/*" - "spec/**/*" Naming/PredicateName: Enabled: false Security/YAMLLoad: Enabled: false Style/BarePercentLiterals: EnforcedStyle: percent_q Style/BlockDelimiters: EnforcedStyle: braces_for_chaining Style/Documentation: Enabled: false Style/EmptyMethod: EnforcedStyle: expanded Style/FormatStringToken: Enabled: false Style/FrozenStringLiteralComment: EnforcedStyle: never Style/Lambda: EnforcedStyle: literal Style/ModuleFunction: EnforcedStyle: extend_self Style/MutableConstant: Enabled: false Style/PreferredHashMethods: Enabled: false Style/StringLiterals: EnforcedStyle: double_quotes Style/StringLiteralsInInterpolation: EnforcedStyle: double_quotes Style/TernaryParentheses: EnforcedStyle: require_parentheses_when_complex qr4r-0.6.2/examples/0000755000004100000410000000000014467443175014274 5ustar www-datawww-dataqr4r-0.6.2/examples/generate_qr4r.rb0000755000004100000410000000422314467443175017367 0ustar www-datawww-data#!/usr/bin/env ruby require "qr4r" require "optparse" class CmdlineOpts @opts = nil ALLOWED_FORMATS = %w[jpg jpeg gif tif tiff png].freeze attr_reader :options class Options attr_accessor :format, :border, :pixel_size, :verbose def to_h { format: format, border: border, pixel_size: pixel_size, verbose: verbose } end end def initialize(args) @options = Options.new @options.format = "gif" @options.border = 0 @options.pixel_size = 10 @options.verbose = false @opts = setup_options @opts.parse!(args) end # rubocop:disable Metrics/MethodLength def setup_options OptionParser.new do |opts| opts.banner = "Usage: $0 [options] outfile the stuff to encode" opts.separator "" # Mandatory argument. opts.on("-f", "--format FILE_FORMAT", ALLOWED_FORMATS, "Output qrcode image format (default: gif)", " (#{ALLOWED_FORMATS.join ", "})") do |fmt| @options.format = fmt end opts.on("-b", "--border N", "Render with a border of this size") do |border| @options.border = border.to_i end opts.on("-p", "--pixelsize N", "Size for each qrcode pixel") do |px| @options.pixel_size = px.to_i end opts.on("-v", "--[no-]verbose", "Be verbose") do |v| @options.verbose = v end # No argument, shows at tail. This will print an options summary. # Try it and see! opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts end # rubocop:enable Metrics/MethodLength end def help puts @opts end end cmd_options = CmdlineOpts.new(ARGV) if ARGV.length < 2 cmd_options.help else outfile = ARGV.shift to_encode = ARGV.join " " options = cmd_options.options if options.verbose print "Encoding \"#{to_encode}\" to file #{outfile}" print " with border #{options.border}" if options.border.positive? print " and pixel_size #{options.pixel_size}" puts " and format #{options.format}" end Qr4r.encode(to_encode, outfile, cmd_options.options.to_h) end qr4r-0.6.2/.rvmrc0000644000004100000410000000007714467443175013614 0ustar www-datawww-datarvm use --create 1.9.2-p290@qr4r #rvm use --create 1.8.7-p334 qr4r-0.6.2/Rakefile0000644000004100000410000000040314467443175014120 0ustar www-datawww-datarequire "rubygems" require "rake/testtask" task default: :test Rake::TestTask.new do |task| task.pattern = "test/*_test.rb" end task :build do `rm qr4r-*.gem` puts `gem build qr4r.gemspec` end task release: :build do puts `gem push qr4r-*.gem` end qr4r-0.6.2/lib/0000755000004100000410000000000014467443175013224 5ustar www-datawww-dataqr4r-0.6.2/lib/qr4r.rb~0000644000004100000410000000102614467443175014636 0ustar www-datawww-datarequire 'rqrcode' require 'mojo_magick' module QR4R def self.encode(str, outfile) qr = RQRCode::QRCode.new(str) data = [] qr.modules.each_index do |x| qr.modules.each_index do |y| if qr.dark?(x,y) 3.times { data << 0 } else 3.times { data << 255 } end end end MojoMagick::convert do |c| c.blob (data.pack 'C'*data.length), :format => :rgb, :depth => 8, :size => "%dx%d" % [qr.modules.size, qr.modules.size] c.file outfile end end end qr4r-0.6.2/lib/qr4r.rb0000644000004100000410000000350414467443175014443 0ustar www-datawww-datarequire "rqrcode_core" require "mojo_magick" module Qr4r # These come from rqrcode_core - read https://github.com/whomwah/rqrcode_core # for more information SIZE_RESTRICTIONS = [0, 7, 14, 24, 34, 44, 58, 64, 84, 98, 119] def self.encode(str, outfile, *rest) opts = rest[0] unless rest.empty? opts ||= {} opts[:size] = compute_size(str) unless opts[:size] opts[:pixel_size] = 3 unless opts[:pixel_size] qr_code, data = build_qr_code(str, opts) create_image(qr_code, data, outfile, opts) end class << self private def build_qr_code(str, opts) qr = RQRCodeCore::QRCode.new(str, opts) data = [].tap do |px| qr.modules.each_index do |x| qr.modules.each_index do |y| if qr.checked?(x, y) 3.times { px << 0 } else 3.times { px << 255 } end end end end [qr, data] end def create_image(qr_code, data, outfile, opts) MojoMagick.convert do |c| d = data.pack "C" * data.size c.blob(d, format: :rgb, depth: 8, size: format("%dx%d", qr_code.modules.size, qr_code.modules.size)) if opts[:pixel_size] wd = qr_code.modules.size * opts[:pixel_size].to_i c.scale format("%dx%d", wd, wd) end if opts[:border] border = opts[:border].to_i c.bordercolor "white" c.border format("%dx%d", border, border) end c.file outfile end end def compute_size(str) slen = str.size ii = 0 while ii < SIZE_RESTRICTIONS.length break if slen < SIZE_RESTRICTIONS[ii] ii += 1 end if ii > 10 raise "Your string is too big for this encoder. It should be less than #{SIZE_RESTRICTIONS.last} characters" end ii end end end qr4r-0.6.2/lib/qr4r/0000755000004100000410000000000014467443175014114 5ustar www-datawww-dataqr4r-0.6.2/lib/qr4r/version.rb0000644000004100000410000000005314467443175016124 0ustar www-datawww-datamodule Qr4r VERSION = "0.6.2".freeze end qr4r-0.6.2/lib/qr4r/version.rb~0000644000004100000410000000004414467443175016322 0ustar www-datawww-datamodule Qr4r VERSION = '0.4.1' end qr4r-0.6.2/Gemfile0000644000004100000410000000004614467443175013751 0ustar www-datawww-datasource "https://rubygems.org" gemspec qr4r-0.6.2/.ruby-version0000644000004100000410000000000614467443175015117 0ustar www-datawww-data2.6.6 qr4r-0.6.2/.github/0000755000004100000410000000000014467443175014016 5ustar www-datawww-dataqr4r-0.6.2/.github/workflows/0000755000004100000410000000000014467443175016053 5ustar www-datawww-dataqr4r-0.6.2/.github/workflows/ruby.yml0000644000004100000410000000124614467443175017562 0ustar www-datawww-data# This workflow uses actions that are not certified by GitHub. They are # provided by a third-party and are governed by separate terms of service, # privacy policy, and support documentation. # # This workflow will install a prebuilt Ruby version, install dependencies, and # run tests and linters. name: "Ruby CI" on: push: branches: [ '**' ] jobs: ruby-test: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Ruby setup uses: ruby/setup-ruby@v1 with: bundler-cache: true - name: Lint run: bundle exec rubocop - name: Test run: bundle exec rake test qr4r-0.6.2/LICENSE.txt0000644000004100000410000000102314467443175014275 0ustar www-datawww-data DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2020 Jon Rogers Copyright (C) 2004 Sam Hocevar Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.