How to generate code_128 barcode for string with Barby - ruby-on-rails

I'm currently using Barby gem with wrapper gem has_barcode to generate a bar code for a string I have : j5xvvcz.
Gemfile gems (mostly for those who are new to this solution)
#for barcode generation
gem "barby", "~> 0.5.0"
gem "has_barcode", "~> 0.2.0"
gem "rqrcode", "~> 0.4.2" #for qr code support
Code i have in my model
include HasBarcode
has_barcode :barcode,
:outputter => :svg,
:type => :code_93,
:value => Proc.new { |p| "#{p.number}" }
Where i render it to screen:
If I try to generate a qr_code or a code_93, it all works, but nor code_128 or code_39 work, getting a data not valid message.
My worries is that code_93 won't get recognized in some devices since it seems it is not so widely adopted (from what i read here 128 would be the best solution for this)
This seems to be something i might be doing wrong, since the code is valid for code_128 aparently as i tested it here.
Anyone knows what might be wrong with my approach?

Apparently mode 'A' for code_128 doesn't suit for smallcaps with Barby.
So i had to had mode B working.
Current published gem version of has_barcode forces mode 'A', so I added a little "home-patch", while adding a suggestion to github.
Here is my final has_barcode.rb file :
require "rubygems"
require "i18n"
require "active_support"
require "active_support/hash_with_indifferent_access.rb"
require "active_support/inflector.rb"
require "barby"
require "has_barcode/configuration"
module HasBarcode
def self.included(base)
base.send(:extend, ClassMethods)
end
module ClassMethods
def has_barcode(*args)
options = args.extract_options!
##barcode_configurations ||= {}
##barcode_configurations[args.first] = HasBarcode::Configuration.new(options)
define_method args.first do
if options[:type] == :code_128
##barcode_configurations[args.first].barcode_class.new(options[:value].call(self), options[:code128])
else
##barcode_configurations[args.first].barcode_class.new(options[:value].call(self))
end
end
define_method "#{args.first}_data" do |opts|
if opts
send(args.first).send("to_#{options[:outputter]}", opts)
else
send(args.first).send("to_#{options[:outputter]}")
end
end
end
def barcode_configurations
##barcode_configurations
end
end
end
My model :
has_barcode :barcode,
outputter: :svg,
type: :code_128,
code128: 'B',
value: Proc.new { |p| "#{p.number}" }
and my view :
<%= my_model.barcode_data(xdim:2, height:60).html_safe %>
Do notice that at current date (2012-12-05) current gem isn't updated with latest changes that allow to pass outputter arguments like xdim and height.
This answer is based on latest code updates (issue related) and my own suggestion, and can be found here
While this solution isn't embedded into the has_barcode gem, i will be using Barby directly:
In my model added :
def get_barcode(number)
require 'barby'
require 'barby/barcode/qr_code'
require 'barby/outputter/svg_outputter'
barcode = Barby::Code128B.new("#{number}")
barcode.to_svg(xdim:2, height:60)
end
in the view :
<%= my_model.get_barcode(my_model.number).html_safe %>

Related

Geocoder::Result::Geoip2#country returns nil even though location.data['country']['names']['en'] is not

I can't get Ahoy to work with Geocoder and the problem is related to this Ahoy line:
module Ahoy
class GeocodeV2Job < ActiveJob::Base
[...]
if location && location.country.present? # <= but my location.country is never present...
This is really odd because, from my console, I get this:
> heroku run rails c
> visit = Ahoy::Visit.last
> location = Geocoder.search(visit.ip).first
=> #<Geocoder::Result::Geoip2:0x00000006e14910 #data=#<MaxMindDB::Result:0x00000006e150b8 #raw={"city"=>{"geoname_id"=>3170918, "names"=>{"de"=>"Piasco", "en"=>"Piasco", "fr"=>"Piasco", "pt-BR"=>"Piasco"}}, "continent"=>{"code"=>"EU", "geoname_id"=>6255148, "names"=>{"de"=>"Europa", "en"=>"Europe", "es"=>"Europa", "fr"=>"Europe", "ja"=>"ヨーロッパ", "pt-BR"=>"Europa", "ru"=>"Европа", "zh-CN"=>"欧洲"}}, "country"=>{"geoname_id"=>3175395, "is_in_european_union"=>true, "iso_code"=>"IT", "names"=>{"de"=>"Italien", "en"=>"Italy", "es"=>"Italia", "fr"=>"Italie", "ja"=>"イタリア共和国", "pt-BR"=>"Itália", "ru"=>"Италия", "zh-CN"=>"意大利"}}, "location"=>{"accuracy_radius"=>50, "latitude"=>44.5611, "longitude"=>7.4442, "time_zone"=>"Europe/Rome"}, "postal"=>{"code"=>"12026"}, "registered_country"=>{"geoname_id"=>3175395, "is_in_european_union"=>true, "iso_code"=>"IT", "names"=>{"de"=>"Italien", "en"=>"Italy", "es"=>"Italia", "fr"=>"Italie", "ja"=>"イタリア共和国", "pt-BR"=>"Itália", "ru"=>"Италия", "zh-CN"=>"意大利"}}, "subdivisions"=>[{"geoname_id"=>3170831, "iso_code"=>"21", "names"=>{"de"=>"Piemont", "en"=>"Piedmont", "es"=>"Piamonte", "fr"=>"Piémont", "ja"=>"ピエモンテ州", "ru"=>"Пьемонт", "zh-CN"=>"皮埃蒙特"}}, {"geoname_id"=>3177699, "iso_code"=>"CN", "names"=>{"en"=>"Provincia di Cuneo", "fr"=>"Coni"}}], "network"=>"85.159.180.0/23"}>, #cache_hit=nil>
So location is not nil!
But then:
> location.country
=> ""
Even though: 👀
irb(main):008:0> location.data['country']['names']['en']
=> "Italy"
Please note that location is a:
> location.class
=> Geocoder::Result::Geoip2
I'm riding:
> Rails.version
=> "4.2.8"
> Ahoy::VERSION
=> "2.2.0"
Using geocoder 1.5.0.
And these are my initializers:
module Ahoy
class Store < Ahoy::DatabaseStore
def track_visit(data)
data[:ip] = request.headers['CF-Connecting-IP'] if request.headers['CF-Connecting-IP'].present? # Because I'm using Cloudflare
super(data)
end
def track_event(data)
data[:properties][:subdomain] = request.subdomain
data[:properties][:domain] = request.domain
super(data)
end
end
end
Ahoy.api = true
Ahoy.geocode = true
Geocoder.configure(
lookup: :location_iq,
api_key: ENV['LOCATION_IQ_ACCESS_TKN'],
language: 'it',
cache: Rails.cache,
ip_lookup: :geoip2,
geoip2: {
file: Rails.root.join('lib', 'geocoder', 'GeoLite2-City.mmdb')
}
)
What am I missing?
Thank you!
Ok, I figured this out. The problem, as usual, is my homeland ;-)
In other words... in my geocoder initializer I set 'it' as the default language but the GeoLite2-City.mmdb database, embedded in the gem 'maxminddb', doesn't support Italian.
So I just have to figure out how to make just the ip_lookup work in English and it all be fine :)

Brakeman not skipping Gemfile.lock with --skip-files param

I'm adding Brakeman to a Rails product but I'm running into an issue. I want it to ignore my Gemfile and Gemfile.lock but when I run it with a command like
brakeman --skip-files Gemfile.lock,Gemfile
it's still touching the files. We use other systems to monitor our gems, but is it not possible to ignore the gem files completely? I can use a brakeman.ignore file of course but would prefer not to. Thanks for any assistance.
I believe this is the check to which you are referring:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/scanner.rb#L39-L40
Brakeman.notify "Processing gems..."
process_gems
The process_gems function is defined here:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/scanner.rb#L131-L152
#Process Gemfile
def process_gems
gem_files = {}
if #app_tree.exists? "Gemfile"
gem_files[:gemfile] = { :src => parse_ruby(#app_tree.read("Gemfile")), :file => "Gemfile" }
elsif #app_tree.exists? "gems.rb"
gem_files[:gemfile] = { :src => parse_ruby(#app_tree.read("gems.rb")), :file => "gems.rb" }
end
if #app_tree.exists? "Gemfile.lock"
gem_files[:gemlock] = { :src => #app_tree.read("Gemfile.lock"), :file => "Gemfile.lock" }
elsif #app_tree.exists? "gems.locked"
gem_files[:gemlock] = { :src => #app_tree.read("gems.locked"), :file => "gems.locked" }
end
if gem_files[:gemfile] or gem_files[:gemlock]
#processor.process_gems gem_files
end
rescue => e
Brakeman.notify "[Notice] Error while processing Gemfile."
tracker.error e.exception(e.message + "\nWhile processing Gemfile"), e.backtrace
end
The AppTree::exists? function is defined here:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/app_tree.rb#L82-L84
def exists?(path)
File.exist?(File.join(#root, path))
end
The GemProcessor::process_gems function is defined here:
https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman/processors/gem_processor.rb#L11
...lots of code...
I don't see any code that would skip this functionality if a certain switch is provided to brakeman. It also looks like the AppTree::exists? function does not take into account if a file was provided to the --skip-files option.
Unfortunately, I believe the current answer is that you can not ignore the gem files completely.
You could create a PR to do what you want and see if the Brakeman team includes it in the next build:
https://brakemanscanner.org/docs/contributing/
Let us know if you discover a way to solve your problem.

What could cause performance issue(s) when using roadie-rails for our case?

Originally posted as
https://github.com/Mange/roadie-rails/issues/75
We are seeing performance issue for our daily email jobs
By using NewRelic custom instrumentation,
we found out that most time is spent in calling Roadies
Screenshot of our NewRelic data for an example worker:
The integration code:
# frozen_string_literal: true
require "rails"
require "action_controller"
require "contracts"
require "memoist"
require "roadie"
require "roadie-rails"
require "new_relic/agent/method_tracer"
module Shared::MailerMixins
module WithRoadieIntegration
# I don't want to include the constants into the class as well
module Concern
def self.included(base)
base.extend ClassMethods
end
include ::NewRelic::Agent::MethodTracer
def mail(*args, &block)
super.tap do |m|
options = roadie_options
next unless options
trace_execution_scoped(
[
[
"WithRoadieIntegration",
"Roadie::Rails::MailInliner.new(m, options).execute",
].join("/"),
],
) do
Roadie::Rails::MailInliner.new(m, options).execute
end
end
end
private
def roadie_options
::Rails.application.config.roadie.tap do |options|
options.asset_providers = [UserAssetsProvider.new]
options.external_asset_providers = [UserAssetsProvider.new]
options.keep_uninlinable_css = false
options.url_options = url_options.slice(*[
:host,
:port,
:path,
:protocol,
:scheme,
])
end
end
add_method_tracer(
:roadie_options,
"WithRoadieIntegration/roadie_options",
)
end
class UserAssetsProvider
extend(
::Memoist,
)
include(
::Contracts::Core,
::Contracts::Builtin,
)
include ::NewRelic::Agent::MethodTracer
ABSOLUTE_ASSET_PATH_REGEXP = /\A#{Regexp.escape("//")}.+#{Regexp.escape("/assets/")}/i
Contract String => Maybe[Roadie::Stylesheet]
def find_stylesheet(name)
return nil unless file_exists?(name)
Roadie::Stylesheet.new("whatever", stylesheet_content(name))
end
add_method_tracer(
:find_stylesheet,
"UserAssetsProvider/find_stylesheet",
)
Contract String => Roadie::Stylesheet
def find_stylesheet!(name)
stylesheet = find_stylesheet(name)
if stylesheet.nil?
raise Roadie::CssNotFound.new(
name,
"does not exists",
self,
)
end
stylesheet
end
add_method_tracer(
:find_stylesheet!,
"UserAssetsProvider/find_stylesheet!",
)
private
def file_exists?(name)
if assets_precompiled?
File.exists?(local_file_path(name))
else
sprockets_asset(name)
end
end
memoize :file_exists?
# If on-the-fly asset compilation is disabled, we must be precompiling assets.
def assets_precompiled?
!Rails.configuration.assets.compile
rescue
false
end
def local_file_path(name)
asset_path = asset_path(name)
if asset_path.match(ABSOLUTE_ASSET_PATH_REGEXP)
asset_path.gsub!(ABSOLUTE_ASSET_PATH_REGEXP, "assets/")
end
File.join(Rails.public_path, asset_path)
end
memoize :local_file_path
add_method_tracer(
:local_file_path,
"UserAssetsProvider/local_file_path",
)
def sprockets_asset(name)
asset_path = asset_path(name)
if asset_path.match(ABSOLUTE_ASSET_PATH_REGEXP)
asset_path.gsub!(ABSOLUTE_ASSET_PATH_REGEXP, "")
end
# Strange thing is since rails 4.2
# name is passed in like
# `/assets/mailer-a9c96bd713d0b091297b82053ccd9155b933c00a53595812d755825d1747f42d.css`
# Before any processing
# And since `sprockets_asset` is used for preview
# We just "fix" the name by removing the
#
# Regexp taken from gem `asset_sync`
# https://github.com/AssetSync/asset_sync/blob/v1.2.1/lib/asset_sync/storage.rb#L142
#
# Modified to match what we need here (we need `.css` suffix)
if asset_path =~ /-[0-9a-fA-F]{32,}\.css$/
asset_path.gsub!(/-[0-9a-fA-F]{32,}\.css$/, ".css")
end
Rails.application.assets.find_asset(asset_path)
end
add_method_tracer(
:sprockets_asset,
"UserAssetsProvider/sprockets_asset",
)
def asset_path(name)
name.gsub(%r{^[/]?assets/}, "")
end
Contract String => String
def stylesheet_content(name)
if assets_precompiled?
File.read(local_file_path(name))
else
# This will compile and return the asset
sprockets_asset(name).to_s
end.strip
end
memoize :stylesheet_content
add_method_tracer(
:stylesheet_content,
"UserAssetsProvider/stylesheet_content",
)
end
end
end
I would like to report my own findings
With NewRelic data, we think most of the time is spent on
Roadies::Inliner/selector_elements => Roadie::Inliner/elements_matching_selector
And it seems a stylesheet with more style rules will make the style inlining takes longer
Benchmark code will be something like:
# frozen_string_literal: true
require "benchmark/ips"
class TestMailer < ::ActionMailer::Base
def show(benchmark_file_path:)
return mail(
from: "somewhere#test.com",
to: ["somewhere#test.com"],
subject: "some subject",
# This is trying to workaround a strange bug in `mail` gem
# https://github.com/mikel/mail/issues/912#issuecomment-156186383
content_type: "text/html",
) do |format|
format.html do
render(
file: benchmark_file_path,
layout: false,
)
end
end
end
end
Benchmark.ips do |x|
x.warmup = 5
x.time = 60
options = Roadie::Rails::Options.new(
# Use your own provider or use built-in providers
# I use a custom provider which can be used inside a rails app,
# See https://github.com/Mange/roadie for built-in providers
#
# options.asset_providers = [UserAssetsProvider.new]
# options.external_asset_providers = [UserAssetsProvider.new]
options.keep_uninlinable_css = false
)
# Need to prepare html_file yourself with
# different stylesheet tag pointing to two different stylesheet files
x.report("fat") do
message = ::TestMailer.
show(
benchmark_file_path: "benchmark-fat-stylesheet.html",
).message.tap do |m|
Roadie::Rails::MailInliner.new(m, options).execute
end
if message.body.to_s =~ /stylesheet/
raise "stylesheet not processed"
end
end
x.report("slim") do
message = ::TestMailer.
show(
benchmark_file_path: "benchmark-slim-stylesheet.html",
).message
if message.body.to_s =~ /stylesheet/
raise "stylesheet not processed"
end
end
# Compare the iterations per second of the various reports!
x.compare!
end

private method issues

I am having issues with private method capabilities.
Here is a my sample env.rb file
require 'rspec/expectations'
require 'selenium-webdriver'
#APP_PATH = 'PlainNote.app'
def capabilities
{
'automationName' => 'XCUITest',
'platformName' => 'iOS',
'deviceName' => 'iPhone Simulator',
'platform' => 'Mac',
'version' => '9.2',
'app' => '/Users/shafiq.malik/Documents/Projects/nuff-class-booking-mobile/platforms/ios/build/emulator/HelloCordova.app'}
end
#def absolute_app_path
# File.join(File.dirname(__FILE__), APP_PATH)
#end
def server_url
"http://localhost:8000/index.html"
end
def selenium
#driver ||= Selenium::WebDriver.for(:remote, :desired_capabilities =>
capabilities, :url => server_url)
end
After { #driver.quit }
However I am getting the below error message
Given I load the hello world app # features/step_definitions/hello_world.rb:1
private method `capabilities' called for #<Hash:0x007fb9b07c7da8> (NoMethodError)
./features/support/env.rb:25:in `selenium'
I completely new to mobile testing. So it may be a very straight forward solution. Can somebody please be kind enough to advise where I am going wrong.
You should not use capabilities as method name in that situation.
Maybe it's defined in Kernel or anywhere else.
Sometimes that could happen when we use some reserved names for our needs. You could check where that method come from via p method(:capabilities) before your definition.

Monkey Patch Guard Cucumber Notification Formatter not working

I want to make a simple change to the Guard Cucumber Notification Formatter to pass a priority, a bit like Guard Rspec does, so that growl styling can be improved. Small thing really.
I have tried this as a monkey patch in an initializer file but it won't work. I've tried all kinds of things but for whatever reason I cannot get it to recognize my monkey-patch when running the tests. No change I make in the patch seems to make a difference. I've tried all kinds of variants on the namespacing in case I'm making an error there - and I think that's most likely. Here's what I'm trying to apply:
initializers/guard_cucumber_patch.rb
require 'guard'
require 'guard/notifier'
require 'cucumber/formatter/console'
require 'cucumber/formatter/io'
module Guard
class Cucumber
class NotificationFormatter
def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
if [:failed, :pending, :undefined].index(status)
#rerun = true
step_name = step_match.format_args(lambda { |param| "*#{ param }*" })
::Guard::Notifier.notify(step_name,
:title => #feature_name,
:image => icon_for(status),
:priority => priority(icon_for(status)))
end
end
# Just stolen from the guard/rspec/formatter code
def priority(image)
{ :failed => 2,
:pending => -1,
:success => -2
}[image]
end
end
end
end
The original guard/cucumber/notification_formatter.rb file is as follows:
require 'guard'
require 'guard/notifier'
require 'cucumber/formatter/console'
require 'cucumber/formatter/io'
module Guard
class Cucumber
# The notification formatter is a Cucumber formatter that Guard::Cucumber
# passes to the Cucumber binary. It writes the `rerun.txt` file with the failed features
# an creates system notifications.
#
# #see https://github.com/cucumber/cucumber/wiki/Custom-Formatters
#
class NotificationFormatter
include ::Cucumber::Formatter::Console
attr_reader :step_mother
# Initialize the formatter.
#
# #param [Cucumber::Runtime] step_mother the step mother
# #param [String, IO] path_or_io the path or IO to the feature file
# #param [Hash] options the options
#
def initialize(step_mother, path_or_io, options)
#options = options
#file_names = []
#step_mother = step_mother
end
# Notification after all features have completed.
#
# #param [Array[Cucumber::Ast::Feature]] features the ran features
#
def after_features(features)
notify_summary
write_rerun_features if !#file_names.empty?
end
# Before a feature gets run.
#
# #param [Cucumber::Ast::FeatureElement] feature_element
#
def before_feature_element(feature_element)
#rerun = false
#feature_name = feature_element.name
end
# After a feature gets run.
#
# #param [Cucumber::Ast::FeatureElement] feature_element
#
def after_feature_element(feature_element)
if #rerun
#file_names << feature_element.file_colon_line
#rerun = false
end
end
# Gets called when a step is done.
#
# #param [String] keyword the keyword
# #param [Cucumber::StepMatch] step_match the step match
# #param [Symbol] status the status of the step
# #param [Integer] source_indent the source indentation
# #param [Cucumber::Ast::Background] background the feature background
# #param [String] file name and line number describing where the step is used
#
def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
if [:failed, :pending, :undefined].index(status)
#rerun = true
step_name = step_match.format_args(lambda { |param| "*#{ param }*" })
::Guard::Notifier.notify step_name, :title => #feature_name, :image => icon_for(status)
end
end
private
# Notify the user with a system notification about the
# result of the feature tests.
#
def notify_summary
icon, messages = nil, []
[:failed, :skipped, :undefined, :pending, :passed].reverse.each do |status|
if step_mother.steps(status).any?
step_icon = icon_for(status)
icon = step_icon if step_icon
messages << dump_count(step_mother.steps(status).length, 'step', status.to_s)
end
end
::Guard::Notifier.notify messages.reverse.join(', '), :title => 'Cucumber Results', :image => icon
end
# Writes the `rerun.txt` file containing all failed features.
#
def write_rerun_features
File.open('rerun.txt', 'w') do |f|
f.puts #file_names.join(' ')
end
end
# Gives the icon name to use for the status.
#
# #param [Symbol] status the cucumber status
# #return [Symbol] the Guard notification symbol
#
def icon_for(status)
case status
when :passed
:success
when :pending, :undefined, :skipped
:pending
when :failed
:failed
else
nil
end
end
end
end
end
EDIT: I am using jruby-head if that makes a difference to monkey business.
Guard::Cucumber makes a system call to start Cucumber in a subshell and your monkey patch is not loaded in that environment. You need to tell Cucumber to require your patch on start like the notification formatter is required.
I'm not actively developing Guard::Cucumber at the moment because I have no project with Cucumber, but I still do maintain it and I'll happily merge that improvement if you make a pull request. I think your improvement would be useful for other users as well.
try to put this file in spec/support dir (create if not exists)
this dir contents are usually included in spec/spec_helper.rb just before running any tests

Resources