Skip to content

Commit

Permalink
Changes to comply with Standard linter
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbranson committed Aug 8, 2021
1 parent 45219b1 commit 4dd57b0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 80 deletions.
36 changes: 18 additions & 18 deletions .coverage/lcov.info
Expand Up @@ -66,25 +66,27 @@ DA:55,16
DA:56,16
DA:57,16
DA:60,1
DA:61,1
DA:62,1
DA:64,1
DA:63,16
DA:64,16
DA:65,16
DA:66,16
DA:67,16
DA:68,16
DA:70,16
DA:69,16
DA:71,16
DA:72,16
DA:73,16
DA:74,16
DA:75,16
DA:76,16
DA:79,1
DA:77,1
DA:78,33
DA:79,99
DA:80,33
DA:81,99
DA:82,33
DA:85,1
DA:86,780
DA:89,1
DA:83,1
DA:84,780
DA:87,1
DA:88,130
DA:89,130
DA:90,130
DA:91,130
DA:92,130
Expand All @@ -97,11 +99,9 @@ DA:98,130
DA:99,130
DA:100,130
DA:101,130
DA:102,130
DA:103,130
DA:106,1
DA:107,16
DA:108,80
DA:111,1
DA:112,16
DA:104,1
DA:105,16
DA:106,80
DA:109,1
DA:110,16
end_of_record
32 changes: 16 additions & 16 deletions lib/ruby_identicon.rb
Expand Up @@ -25,10 +25,10 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++

require 'ruby_identicon/version'
require 'chunky_png'
require 'siphash'
require 'base64'
require_relative "ruby_identicon/version"
require_relative "siphash"
require "chunky_png"
require "base64"

#
# A Ruby implementation of go-identicon
Expand Down Expand Up @@ -68,26 +68,26 @@ module RubyIdenticon
# create an identicon png and save it to the given filename
#
# Example:
# >> RubyIdenticon.create_and_save('identicons are great!', 'test_identicon.png')
# >> RubyIdenticon.create_and_save("identicons are great!", "test_identicon.png")
# => result (Boolean)
#
# @param title [string] the string value to be represented as an identicon
# @param filename [string] the full path and filename to save the identicon png to
# @param options [hash] additional options for the identicon
#
def self.create_and_save(title, filename, options = {})
raise 'filename cannot be nil' if filename == nil
raise "filename cannot be nil" if filename.nil?

blob = create(title, options)
return false if blob == nil
return false if blob.nil?

File.open(filename, 'wb') { |f| f.write(blob) }
File.open(filename, "wb") { |f| f.write(blob) }
end

# create an identicon png and return it as a binary string
#
# Example:
# >> RubyIdenticon.create('identicons are great!')
# >> RubyIdenticon.create("identicons are great!")
# => binary blob (String)
#
# @param title [string] the string value to be represented as an identicon
Expand All @@ -96,16 +96,16 @@ def self.create_and_save(title, filename, options = {})
def self.create(title, options = {})
options = DEFAULT_OPTIONS.merge(options)

raise 'title cannot be nil' if title == nil
raise 'key is nil or less than 16 bytes' if options[:key] == nil || options[:key].length < 16
raise 'grid_size must be between 4 and 9' if options[:grid_size] < 4 || options[:grid_size] > 9
raise 'invalid border size' if options[:border_size] < 0
raise 'invalid square size' if options[:square_size] < 0
raise "title cannot be nil" if title.nil?
raise "key is nil or less than 16 bytes" if options[:key].nil? || options[:key].length < 16
raise "grid_size must be between 4 and 9" if options[:grid_size] < 4 || options[:grid_size] > 9
raise "invalid border size" if options[:border_size] < 0
raise "invalid square size" if options[:square_size] < 0

hash = SipHash.digest(options[:key], title)

png = ChunkyPNG::Image.new((options[:border_size] * 2) + (options[:square_size] * options[:grid_size]),
(options[:border_size] * 2) + (options[:square_size] * options[:grid_size]), options[:background_color])
(options[:border_size] * 2) + (options[:square_size] * options[:grid_size]), options[:background_color])

# set the foreground color by using the first three bytes of the hash value
color = ChunkyPNG::Color.rgba((hash & 0xff), ((hash >> 8) & 0xff), ((hash >> 16) & 0xff), 0xff)
Expand Down Expand Up @@ -139,6 +139,6 @@ def self.create(title, options = {})
end

def self.create_base64(title, options = {})
Base64.encode64(self.create(title, options)).force_encoding('UTF-8')
Base64.encode64(create(title, options)).force_encoding("UTF-8")
end
end
2 changes: 1 addition & 1 deletion lib/ruby_identicon/version.rb
@@ -1,3 +1,3 @@
module RubyIdenticon
VERSION = '0.0.6'
VERSION = "0.0.6"
end
10 changes: 4 additions & 6 deletions lib/siphash.rb
Expand Up @@ -28,7 +28,7 @@ def self.digest(key, msg)
iter = len / 8

iter.times do |i|
m = msg.slice(i * 8, 8).unpack('Q<')[0]
m = msg.slice(i * 8, 8).unpack1("Q<")
s.apply_block(m)
end

Expand All @@ -42,7 +42,7 @@ def self.digest(key, msg)
private

def self.last_block(msg, len, iter)
last = (len << 56) & State::MASK_64;
last = (len << 56) & State::MASK_64

r = len % 8
off = iter * 8
Expand All @@ -58,17 +58,15 @@ def self.last_block(msg, len, iter)
end

class State

MASK_64 = 0xffffffffffffffff

def initialize(key)
@v0 = 0x736f6d6570736575
@v1 = 0x646f72616e646f6d
@v2 = 0x6c7967656e657261
@v3 = 0x7465646279746573

k0 = key.slice(0, 8).unpack('Q<')[0]
k1 = key.slice(8, 8).unpack('Q<')[0]
k0 = key.slice(0, 8).unpack1("Q<")
k1 = key.slice(8, 8).unpack1("Q<")

@v0 ^= k0
@v1 ^= k1
Expand Down
4 changes: 2 additions & 2 deletions ruby_identicon.rb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

require "ruby_identicon.rb"
require_relative "lib/ruby_identicon"

abort "Please run with a filename as an argument" unless ARGV[0]
filename = ARGV[0].clone
Expand All @@ -11,4 +11,4 @@
identstring = gets

blob = RubyIdenticon.create(identstring, grid_size: 5, square_size: 70, background_color: 0xf0f0f0ff, key: "1234567890123456")
File.open(filename, "wb") do |f| f.write(blob) end
File.open(filename, "wb") { |f| f.write(blob) }
10 changes: 5 additions & 5 deletions spec/helper.rb
@@ -1,9 +1,9 @@
require 'simplecov'
require 'simplecov-lcov'
require "simplecov"
require "simplecov-lcov"

SimpleCov::Formatter::LcovFormatter.config do |config|
config.report_with_single_file = true
config.single_report_path = '.coverage/lcov.info'
config.single_report_path = ".coverage/lcov.info"
end

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
Expand All @@ -13,5 +13,5 @@

SimpleCov.start

require 'rspec'
require 'ruby_identicon'
require "rspec"
require "ruby_identicon"
64 changes: 32 additions & 32 deletions spec/ruby_identicon_spec.rb
@@ -1,75 +1,75 @@
require 'helper'
require "helper"

describe RubyIdenticon do
before(:each) do
Dir.mkdir('tmp') unless File.directory?('tmp')
Dir.mkdir("tmp") unless File.directory?("tmp")
end

# general parameter tests
it 'creates a binary string image blob' do
expect(RubyIdenticon.create('RubyIdenticon')).to be_a_kind_of(String)
it "creates a binary string image blob" do
expect(RubyIdenticon.create("RubyIdenticon")).to be_a_kind_of(String)
end

it 'does not create a binary string image blob with an invalid title' do
lambda { RubyIdenticon.create(nil) }.should raise_exception('title cannot be nil')
it "does not create a binary string image blob with an invalid title" do
lambda { RubyIdenticon.create(nil) }.should raise_exception("title cannot be nil")
end

it 'does not create a binary string image blob with an invalid key' do
lambda { RubyIdenticon.create('identicons are great!', key: "\x00\x11\x22\x33\x44") }.should raise_exception('key is nil or less than 16 bytes')
it "does not create a binary string image blob with an invalid key" do
lambda { RubyIdenticon.create("identicons are great!", key: "\x00\x11\x22\x33\x44") }.should raise_exception("key is nil or less than 16 bytes")
end

it 'does not create a binary string image blob with an invalid grid size' do
lambda { RubyIdenticon.create('RubyIdenticon', grid_size: 2) }.should raise_exception('grid_size must be between 4 and 9')
lambda { RubyIdenticon.create('RubyIdenticon', grid_size: 20) }.should raise_exception('grid_size must be between 4 and 9')
it "does not create a binary string image blob with an invalid grid size" do
lambda { RubyIdenticon.create("RubyIdenticon", grid_size: 2) }.should raise_exception("grid_size must be between 4 and 9")
lambda { RubyIdenticon.create("RubyIdenticon", grid_size: 20) }.should raise_exception("grid_size must be between 4 and 9")
end

it 'does not create a binary string image blob with an invalid square_size size' do
lambda { RubyIdenticon.create('RubyIdenticon', square_size: -2) }.should raise_exception('invalid square size')
it "does not create a binary string image blob with an invalid square_size size" do
lambda { RubyIdenticon.create("RubyIdenticon", square_size: -2) }.should raise_exception("invalid square size")
end

it 'does not create a binary string image blob with an invalid border_size size' do
lambda { RubyIdenticon.create('RubyIdenticon', border_size: -2) }.should raise_exception('invalid border size')
it "does not create a binary string image blob with an invalid border_size size" do
lambda { RubyIdenticon.create("RubyIdenticon", border_size: -2) }.should raise_exception("invalid border size")
end

# blob creation tests
it 'creates a png image file' do
blob = RubyIdenticon.create('RubyIdenticon')
result = File.open('tmp/ruby_identicon.png', 'wb') { |f| f.write(blob) }
it "creates a png image file" do
blob = RubyIdenticon.create("RubyIdenticon")
result = File.open("tmp/ruby_identicon.png", "wb") { |f| f.write(blob) }
expect(result).to be_truthy
end

it 'creates a png image file of grid size 5, square size 70 and grey background' do
blob = RubyIdenticon.create('RubyIdenticon', grid_size: 5, square_size: 70, background_color: 0xf0f0f0ff, key: '1234567890123456')
result = File.open('tmp/ruby_identicon_gs5_white.png', 'wb') { |f| f.write(blob) }
it "creates a png image file of grid size 5, square size 70 and grey background" do
blob = RubyIdenticon.create("RubyIdenticon", grid_size: 5, square_size: 70, background_color: 0xf0f0f0ff, key: "1234567890123456")
result = File.open("tmp/ruby_identicon_gs5_white.png", "wb") { |f| f.write(blob) }
expect(result).to be_truthy
end

it 'creates 10 png image files' do
it "creates 10 png image files" do
10.times do |count|
blob = RubyIdenticon.create("RubyIdenticon_#{count}")
result = File.open("tmp/ruby_identicon_#{count}.png", 'wb') { |f| f.write(blob) }
result = File.open("tmp/ruby_identicon_#{count}.png", "wb") { |f| f.write(blob) }
expect(result).to be_truthy
end
end

# file creation tests
it 'creates a png image file via create_and_save' do
result = RubyIdenticon.create_and_save('RubyIdenticon is fun', 'tmp/test_identicon.png')
it "creates a png image file via create_and_save" do
result = RubyIdenticon.create_and_save("RubyIdenticon is fun", "tmp/test_identicon.png")
expect(result).to be_truthy
end

it 'does not create a png image file via create_and_save with an invalid filename' do
lambda { RubyIdenticon.create_and_save('RubyIdenticon is fun', nil) }.should raise_exception('filename cannot be nil')
it "does not create a png image file via create_and_save with an invalid filename" do
lambda { RubyIdenticon.create_and_save("RubyIdenticon is fun", nil) }.should raise_exception("filename cannot be nil")
end

it 'does not create a png image file via create_and_save with an invalid title' do
lambda { RubyIdenticon.create_and_save(nil, 'tmp/test_identicon.png') }.should raise_exception('title cannot be nil')
it "does not create a png image file via create_and_save with an invalid title" do
lambda { RubyIdenticon.create_and_save(nil, "tmp/test_identicon.png") }.should raise_exception("title cannot be nil")
end

# Base64 creation tests
it 'creates base64 version of the image' do
blob = RubyIdenticon.create('RubyIdenticon')
base64 = RubyIdenticon.create_base64('RubyIdenticon')
it "creates base64 version of the image" do
blob = RubyIdenticon.create("RubyIdenticon")
base64 = RubyIdenticon.create_base64("RubyIdenticon")
expect(Base64.decode64(base64)).to eq blob
end
end

0 comments on commit 4dd57b0

Please sign in to comment.