Short-form for validates_exclusion_of - validation

If
validates_presence_of :login,
:message => 'How do you expect to login?'
has a short-form variant
validates :login, :presence => { :message => 'How do you expect to login?' },
what is the short-form version of
validates_exclusion_of :login, :in => ['admin'],
:message => "\"{{value}}\" is reserved."
so I can also have a custom message?

validates :login, :exclusion => { :in => ['admin'], :message => "%{value} is reserved" }

Related

Fedex Ruby Gem: Customs Value is required - how to add?

I'm working with the Ruby gem 'FedEx', https://github.com/jazminschroeder/fedex.
I've set up my code for a development mode and I'm testing making a shipment.
However, I get stuck with the following error:
C:/Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:134:in 'failure_response': Customs Value is required. (Fedex:: RateError) from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:32:in 'process_request' from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-3.10.1/lib/fedex/shipment.rb:57:in 'ship' from C: /Ruby22/bin/css_fedex_v1.rb:92:in ''
It seems that I need to parse a 'Customs Value', probably as part of my 'packages' hash. However, I'm unable to find the relevant field for me to enter this in. Anyone who's experienced this and found a solution?
My code is as below:
require 'fedex'
fedex = Fedex::Shipment.new(:key => '***',
:password => '***',
:account_number => '***',
:meter => '***',
:mode => 'development')
shipper = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "DK" }
recipient = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "GB",
:residential => "false" }
packages = []
packages << {:weight => {:units => "LB", :value => 1}}
shipping_options = {:packaging_type => "YOUR_PACKAGING",
:drop_off_type => "REGULAR_PICKUP"}
rate = fedex.rate(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:shipping_options => shipping_options)
ship = fedex.ship(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:service_type => "INTERNATIONAL_PRIORITY",
:shipping_options => shipping_options)
puts ship[:completed_shipment_detail][:operational_detail][:transit_time]
Customs value is declared in their docs:
https://github.com/jazminschroeder/fedex/commit/9f1d4c67b829aaa4eeba9090c1a45d3bd507aab3#diff-4f122efb7c0d98120d8b7f0cd00998e4R106
customs_value = { :currency => "USD",
:amount => "200" }
As I understand you can pass it into the commodities hash or keep it separate.

How do I Work Around " Encoding::UndefinedConversionError: ""\x9D"" from Windows-1252 to UTF-8"?

Here's the code that generates the error:
issues.each
{
|issue|
response = RestClient::Request.new
(
:method => :get,
:url => "http://wls-eng1:8080/rest/api/2/issue/{issue['key']}/comment",
:user => username,
:password => password,
:headers => { :accept => :json, :content_type => :json },
:ssl_ca_file => :temp
).execute
}

Sinatra, Pony gem: NoMethodError

I am trying to send an e-mail using Pony and get the
NoMethodError at /
undefined method `address' for #Mail::Message:. error. This is my code so far:
post '/' do
Pony.options = { :from => '___#yandex.ru',
:via => :smtp,
:address => 'smtp.yandex.ru',
:port => '465',
:user_name => '___',
:password => '___',
:authentication => :plain,
:domain => "http://127.0.0.1:9393/"
}
Pony.mail(subject: 'Hello', to: "___#yandex.ru", body: 'hi')
redirect '/'
end
when running bundle list it does show pony (1.10). What could go wrong?
:address, :port, etc. go inside a :via_options hash.
Per the documentation:
:via_options => {
:address => 'smtp.yourserver.com',
:port => '25',
:user_name => 'user',
:password => 'password',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
}
Therefore, you'll want:
post '/' do
Pony.options = {
:from => '___#yandex.ru',
:via => :smtp,
:via_options => {
:address => 'smtp.yandex.ru',
:port => '465',
:user_name => '___',
:password => '___',
:authentication => :plain,
:domain => "http://127.0.0.1:9393/"
}
}
Pony.mail(subject: 'Hello', to: "___#yandex.ru", body: 'hi')
redirect '/'
end

How to make multiple file uploading via CarrierWave and Sinatra?

I'm not first who is asking and maybe not last. How to implement multiple uploading with CarrierWave in Sinatra? I'm using this code in action:
post '/create' do
params.delete 'submit'
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:created_at => Time.now
)
d.attachment = params[:attachments]
d.save
redirect '/success'
end
With this model:
class Dcmnt
include Mongoid::Document
store_in collection: 'dcmnts'
field :published, type: Boolean
field :name, type: String
field :description, type: String
field :created_at, type: Date
mount_uploader :attachment, Uploader, type: String
end
And this form:
%form{:method => "post", :action => "/create", :enctype => "multipart/form-data"}
%input{:type => "checkbox", :name => "published", :value => "true"} Published?
%input{:name => "name"}
%textarea{:name => "description", :rows => "5"}
%div.form-group
%label Attachments
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
Also CW's configuration:
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'attachments/' + model.id
end
end
Looks correct, but doesn't work. When I'm trying to upload couple of files either single file, Pow returns me no implicit conversion of nil into String on d.attachment = params[:attachments] line. And I can't figure out why. Can you help me with this?

username and password not accepted with pony gem in ruby

im trying to implement a simple form for my ruby website using pony and i keep getting the error '535-5.7.1 Username and Password not accepted.' I've typed in my own gmail username and password in the correct fields.
code in pony.rb
Pony.options = {
:to => 'myusername',
:via => :smtp,:
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'myusername',
:password => 'mypassword',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
}
}
See the WORKING code below. Just tested this with my gmail.
Pony.mail(:to => 'someone#acme.com', :via => :smtp, :via_options =>
{
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'yourgmail#gmail.com',
:password => 'yourpass',
:authentication => :plain,
:domain => "HELO",
},
:subject => 'Your Subject goes here', :body => "bla bla bla or #{$body}",
:attachments => {"yourfile.txt" => File.binread("c:/ruby193/bin/yourfile.txt")
}
)
Best of luck Arvid let me know if you need further help!

Resources