private method issues - ios

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.

Related

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.

Pubnub and Rails 4

I'm following a twitter tutorial for a class project and I'm stuck at the part where the tutorial is using PUBNUB. I'm getting the following error:
Showing C:/RubyProjects/twitter/app/views/layouts/application.html.erb where line #93 raised:
**wrong number of arguments(1 for 0)**
Extracted source (around line #93):
PUBNUB.subscribe({
channel : "<%= Digest::SHA1.hexdigest(current_user.username, current_user.created_at) %>",
callback : function(message) { updateTimeline(message) }
I found on stackoverflow and found that EventMachine helped some folks and I tried that but still nada :(
I checked the PUBNUB page on Github and saw that it has changed the way channel and callback was written so I tried doing that but it did not help either. Im still getting the same error about wrong number of arguments(1 for 0).
Notify.rb
class Notify
def self.deliver_message_to_user(params)
post = Post.find(params[:post_id])
user = User.find(params[:user_id])
user.channel ||= Channel.new(
:channel_ident =>
Digest::SHA1.hexdigest(user.username, user.created_at.to_s))
Pubnub.publish({
:channel => user.channel.channel_ident,
:message => post.to_json(:include => :user)
})
end
end
application.html.erb
<script>
function updateTimeline(message) {
var html = JST['post'](jQuery.parseJSON(message));
$('#timeline').prepend(html);
}
Pubnub.subscribe({
:channel => "<%= Digest::SHA1.hexdigest(current_user.username, current_user.created_at) %>",
:callback => function(message) { updateTimeline(message) }
})
</script>
I put require 'digest' in my application.rb file and it still din't help. Could it be the syntax? If it is, i'm not sure what the correct syntax would be.
Publishing PubNub Messages in Ruby on Rails 4.0+
I'm pulling the following details from the PubNub Ruby README.md file - https://github.com/pubnub/ruby/blob/master/README.md
First you need to require PubNub lib from the PubNub Gem in Ruby.
## Require PubNub Gem
require 'pubnub'
## Instantiate a new PubNub instance.
pubnub = Pubnub.new(
:publish_key => 'demo', # publish_key only required if publishing.
:subscribe_key => 'demo', # required
:secret_key => nil, # optional, if used, message signing is enabled
:cipher_key => nil, # optional, if used, encryption is enabled
:ssl => nil # true or default is false
)
## Create a callback for checking response of Publish
#my_callback = lambda { |message| puts(message) }
## Execute Publish
pubnub.publish(
:channel => :hello_world,
:message => "hi",
:callback => #my_callback
)
## Sometimes you need a sleep depending on your server type
sleep(1)

ActionMailer::Base::NullMail when trying exception_notification in development

I'd like to add the exception_notification gem to our app, however, this happens when I try to manually trigger a mail:
exception
# => #<ZeroDivisionError: divided by 0>
ExceptionNotifier::Notifier.exception_notification(request.env, exception)
# => #<ActionMailer::Base::NullMail:0x007fa81bc7c610>
ExceptionNotifier::Notifier.background_exception_notification(exception)
# => #<ActionMailer::Base::NullMail:0x007fa81bf58190>
In the above example, the console is at a breakpoint inside rescue_from Exception in the ApplicationController after a deliberate 1/0 in some controller.
I'm using delayed_job as well, but - no surprise - ExceptionNotifier::Notifier.background_exception_notification(exception).deliver does not spool anything.
I've already set config.consider_all_requests_local = false in development, but still exception_notification instantiates NullMail. In other parts of the app, mailers work just fine and use sendmail.
Any ideas what I'm doing wrong here? Thanks for your help!
Likely you are using an old version of the ExceptionNotifier and a newer version of ActiveMailer::Base. Not calling the mail command within the email functionality will result in the ActionMailer::Base::NullMail instance returned rather than a Mail instance.
From documentation:
class Notifier < ActionMailer::Base
default :from => 'no-reply#example.com',
:return_path => 'system#example.com'
def welcome(recipient)
#account = recipient
mail(:to => recipient.email_address_with_name,
:bcc => ["bcc#example.com", "Order Watcher <watcher#example.com>"])
end
end
I had my tests / rspec returning NullMail objects. the solution was simple, my code was:
def my_mail(foo)
mail(
to: to,
from: from,
subject: #sample_item.campaign_market.campaign.translation_for(language_id, 'sample_item_mailer.request_review.subject'),
content_type: "text/html"
)
#sample_item.update_attributes!({feedback_requested: true, review_requested_at: Time.now})
TrackingService::Event.new(#user, #user.market, 'sample_items', "request_review_email #{#sample_item.id}").call()
end
what's not immediately clear from the ruby docs is that you need to return the mail function,not just execute it. If you need to do something after building the mail object make sure you return the mail at the end. like so:
def my_mail(foo)
m = mail(
to: to,
from: from,
subject: #sample_item.campaign_market.campaign.translation_for(language_id, 'sample_item_mailer.request_review.subject'),
content_type: "text/html"
)
#sample_item.update_attributes!({feedback_requested: true, review_requested_at: Time.now})
TrackingService::Event.new(#user, #user.market, 'sample_items', "request_review_email #{#sample_item.id}").call()
return m
end

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

How to generate code_128 barcode for string with Barby

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 %>

Resources