putqr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +50 -0
  4. data/bin/putqr +15 -0
  5. data/lib/putqr.rb +74 -0
  6. data/lib/putqr/version.rb +3 -0
  7. metadata +63 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 894df85665fbab9c06ad27bff808f806ff9e552915ad0ad0776d078bee5581d4
4
+ data.tar.gz: '006614608e3afd5b73943854e8811fd05fe08a49c60b8cdbb4dce5ac30536c0e'
5
+ SHA512:
6
+ metadata.gz: 24045838704daf36f8ad32221e4477a85a1818890619a45f475e5392184e7c502af475b436218e1911e6526980f1a03c60b6450c068812ab04422fdcd2db0095
7
+ data.tar.gz: e4bae898597198756aad237d9f831f092eacf977ed6207008e5e45faee5ece117ce5361a1430674815f31267185d49a6250a71bafb74f0d4c8fc907ccd8c0ebc
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Liam Cooke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # putqr
2
+
3
+ Display a QR code in your terminal.
4
+
5
+ If you're using [iTerm2](https://iterm2.com) on macOS,
6
+ the QR code will be displayed as an image.
7
+
8
+ ## Install
9
+
10
+ Install `putqr` from [RubyGems](https://rubygems.org/):
11
+
12
+ ```
13
+ gem install putqr
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Run the `putqr` command with the string you want to encode, like:
19
+
20
+ ```
21
+ putqr https://example.com/
22
+ ```
23
+
24
+ Run the command without arguments to read the data from standard input:
25
+
26
+ ```
27
+ putqr < example.txt
28
+ ```
29
+
30
+ ## Development
31
+
32
+ To run the latest source code, check out the repository from GitHub:
33
+
34
+ ```
35
+ git clone https://github.com/ljcooke/putqr.git
36
+ ```
37
+
38
+ Install the dependencies using Bundler:
39
+
40
+ ```
41
+ gem install bundler
42
+ bundle install
43
+ ```
44
+
45
+ Run the tests and the `putqr` command using Bundler:
46
+
47
+ ```
48
+ bundle exec rspec
49
+ bundle exec putqr
50
+ ```
data/bin/putqr ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'putqr'
5
+
6
+ if ARGV.any?
7
+ content = ARGV.first
8
+ STDERR.puts 'warning: only encoding first argument' if ARGV.size > 1
9
+ else
10
+ content = STDIN.read
11
+ end
12
+
13
+ output = PutQR::QRCode.new(content).render
14
+ exit(1) if output.nil?
15
+ puts output
data/lib/putqr.rb ADDED
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'rqrcode'
5
+
6
+ require 'putqr/version'
7
+
8
+ module PutQR
9
+ # Display a QR code in your terminal.
10
+ class QRCode
11
+ attr_reader :qrcode
12
+
13
+ # Initialize the QR code with a string.
14
+ def initialize(content)
15
+ @qrcode = QRCode.generate_qrcode(content)
16
+ end
17
+
18
+ # Can the string be encoded as a QR code?
19
+ def valid?
20
+ !qrcode.nil?
21
+ end
22
+
23
+ # Render the QR code for display in the terminal.
24
+ # Returns a string.
25
+ def render
26
+ if ENV['TERM_PROGRAM'].start_with? 'iTerm'
27
+ render_image_iterm2
28
+ else
29
+ render_ansi
30
+ end
31
+ end
32
+
33
+ # Render the QR code using ANSI escape codes.
34
+ # Returns a string.
35
+ def render_ansi
36
+ qrcode.as_ansi if valid?
37
+ end
38
+
39
+ # Render the QR code as an inline image for iTerm2.
40
+ # Returns a string.
41
+ def render_image_iterm2
42
+ return nil unless valid?
43
+
44
+ png = qrcode.as_png(size: 600)
45
+ png_base64 = Base64.encode64(png.to_s).chomp
46
+ options = 'inline=1'
47
+ "\033]1337;File=#{options}:#{png_base64}\007"
48
+ end
49
+
50
+ def self.generate_qrcode(content)
51
+ # Try each size until one fits
52
+ (min_qr_version(content.size)..40).each do |version|
53
+ begin
54
+ return RQRCode::QRCode.new(content, level: :h, size: version)
55
+ rescue RQRCode::QRCodeRunTimeError
56
+ next
57
+ end
58
+ end
59
+ nil
60
+ end
61
+
62
+ def self.min_qr_version(input_size)
63
+ # Skip the lower QR code versions where the input is known to be too
64
+ # long. These figures are based on the maximum number of characters
65
+ # that can be encoded for a numeric string with high error correction.
66
+ if input_size >= 2_524 then 36
67
+ elsif input_size >= 1_897 then 31
68
+ elsif input_size >= 969 then 21
69
+ elsif input_size >= 331 then 11
70
+ else 1
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module PutQR
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: putqr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Liam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rqrcode
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.1
27
+ description: putqr displays a QR code in your terminal.
28
+ email: putqr@liamcooke.com
29
+ executables:
30
+ - putqr
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - bin/putqr
37
+ - lib/putqr.rb
38
+ - lib/putqr/version.rb
39
+ homepage: https://github.com/ljcooke/putqr
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.7.7
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Displays a QR code in your terminal.
63
+ test_files: []