Convert 1200 to 1.2K in ruby/rails - ruby-on-rails

I think there is a method within ruby or rails that does this but I can't remember where to find it or how to search for it, so I was hoping stackoverflow's collective wisdom might help.
I don't mind writing a method to do this, but I'm sure someone has a better solution.

number_to_human(1200, :format => '%n%u', :units => { :thousand => 'K' })
# 1200 => 1.2K

If your number happens to be 1223 the accepted answer output would be 1.22K, include the precision parameter to reduce this to 1.2K. Also, if your number could be a wide range of numbers in the millions and billions, then best to cater for these also:
number_to_human(1200, :format => '%n%u', :precision => 2, :units => { :thousand => 'K', :million => 'M', :billion => 'B' })
# => "1.2K"
number_to_human(1223, :format => '%n%u', :precision => 2, :units => { :thousand => 'K', :million => 'M', :billion => 'B' })
# => "1.2K"
number_to_human(1223456789, :format => '%n%u', :precision => 2, :units => { :thousand => 'K', :million => 'M', :billion => 'B' })
# => "1.2B"

Take a look at Rails Number Helper,
The method number_to_human_size might be what you need.

Related

Print "signature_required" option in Fedex label - Rails

I'm using the fedex gem version 3.9 and cannot get the "signature required" option printed on my fedex label.
This is how it looks:
label = fedex.label(
:filename => "public/labels/#{label_name}",
:shipper => shipper,
:recipient => recipient,
:packages => packages,
:service_type => service_type,
:signature_required => true,
:shipping_options => shipping_options)
I remember this used to work a couple of months ago but now it doesn't.
Let me know if you have any suggestions.
Best,
Turns out that the signature options should be sent as part of the packages. Then it prints the label with the right signature initials
packages = [{
:weight => {:units => "LB", :value => 3},
:dimensions => {:length => 10, :width => 5, :height => 4, :units => "IN" },
:special_services_requested => {:special_service_types => "SIGNATURE_OPTION", :signature_option_detail => {:signature_option_type => "DIRECT"} }
}]
label = fedex.label(
:filename => "public/labels/#{label_name}",
:shipper => shipper,
:recipient => recipient,
:packages => packages
:shipping_options => shipping_options)

Inconsistent display of checkboxes on PDF form filled with PDFtk

I am filling PDF forms in my rails app with the pdf-forms (https://github.com/jkraemer/pdf-forms) gem, based on PDFtk. Text fields work as I would expect, but my checkbox fields do not. The boxes display well in Chrome, but in Preview and Mail the checkbox fields appear empty.
class FormsController < ApplicationController
require 'pdf_forms'
def acord25
#policy = Policy.find(params[:id])
pdftk = PdfForms.new('/usr/local/bin/pdftk')
# find out the field names that are present in form.pdf
pdftk.get_field_names 'lib/pdfs/acord25.pdf'
# take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
pdftk.fill_form '/lib/pdfs/acord25.pdf', "acord25.pdf",
"F[0].P1[0].Form_CompletionDate_A[0]" => #policy.dateIssued,
"F[0].P1[0].Producer_FullName_A[0]" => #policy.client.broker.name,
"F[0].P1[0].Producer_MailingAddress_LineOne_A[0]" => #policy.client.broker.company,
"F[0].P1[0].Producer_MailingAddress_LineTwo_A[0]" => #policy.client.broker.address,
"F[0].P1[0].Producer_ContactPerson_FullName_A[0]" => #policy.legalVesting,
"F[0].P1[0].Producer_ContactPerson_PhoneNumber_A[0]" => #policy.client.broker.phone,
"F[0].P1[0].Producer_FaxNumber_A[0]" => #policy.client.broker.fax,
"F[0].P1[0].Producer_ContactPerson_EmailAddress_A[0]" => #policy.client.broker.email,
"F[0].P1[0].NamedInsured_FullName_A[0]" => #policy.client.name,
"F[0].P1[0].NamedInsured_MailingAddress_LineOne_A[0]" => #policy.client.address.titlecase,
"F[0].P1[0].GeneralLiability_CoverageIndicator_A[0]" => 1,
"F[0].P1[0].GeneralLiability_OccurrenceIndicator_A[0]" => 1,
"F[0].P1[0].GeneralLiability_GeneralAggregate_LimitAppliesPerLocationIndicator_A[0]" => 1,
"F[0].P1[0].Policy_PolicyNumberIdentifier_A[0]" => #policy.policyNumber,
"F[0].P1[0].Policy_EffectiveDate_A[0]" => #policy.dateEffective,
"F[0].P1[0].PolicyExpirationGeneral[0]" => #policy.term.dayEnd,
"F[0].P1[0].Insurer_FullName_A[0]" => "Lexington Insurance Company",
"F[0].P1[0].Insurer_NAICCode_A[0]" => 19437,
"F[0].P1[0].Insurer_FullName_B[0]" => "Commerce & Industry Insurance Company",
"F[0].P1[0].Insurer_NAICCode_B[0]" => 19410,
"F[0].P1[0].Insurer_FullName_C[0]" => "Great American Insurance Company",
"F[0].P1[0].Insurer_NAICCode_C[0]" => 37532,
"F[0].P1[0].Insurer_FullName_D[0]" => "Admiral Insurance Company",
"F[0].P1[0].Insurer_NAICCode_D[0]" => 24856,
"F[0].P1[0].GeneralLiability_InsurerLetterCode_A[0]" => "A",
"F[0].P1[0].GeneralLiability_EachOccurrence_LimitAmount_A[0]" => 1000000,
"F[0].P1[0].GeneralLiability_FireDamageRentedPremises_EachOccurrenceLimitAmount_A[0]" => 50000,
"F[0].P1[0].GeneralLiability_MedicalExpense_EachPersonLimitAmount_A[0]" => "Excluded",
"F[0].P1[0].GeneralLiability_PersonalAndAdvertisingInjury_LimitAmount_A[0]" => 1000000,
"F[0].P1[0].GeneralLiability_GeneralAggregate_LimitAmount_A[0]" => 2000000,
"F[0].P1[0].GeneralLiability_ProductsAndCompletedOperations_AggregateLimitAmount_A[0]" => 2000000,
"F[0].P1[0].Vehicle_InsurerLetterCode_A[0]" => "A",
"F[0].P1[0].Vehicle_HiredAutosIndicator_A[0]" => 1,
"F[0].P1[0].Vehicle_NonOwnedAutosIndicator_A[0]" => 1,
"F[0].P1[0].Policy_PolicyNumberIdentifier_B[0]" => #policy.policyNumber,
"F[0].P1[0].Policy_EffectiveDate_B[0]" => #policy.dateEffective,
"F[0].P1[0].Policy_ExpirationDate_B[0]" => #policy.term.dayEnd,
"F[0].P1[0].Vehicle_CombinedSingleLimit_EachAccidentAmount_A[0]" => 1000000,
"F[0].P1[0].ExcessUmbrella_InsurerLetterCode_A[0]" => "B",
"F[0].P1[0].ExcessUmbrella_OccurrenceIndicator_A[0]" => 1,
"F[0].P1[0].ExcessUmbrella_DeductibleIndicator_A[0]" => 1,
"F[0].P1[0].ExcessUmbrella_Umbrella_DeductibleOrRetentionAmount_A[0]" => #policy.coverages.first.deductibleOcc,
"F[0].P1[0].Policy_PolicyNumberIdentifier_D[0]" => #policy.policyNumber,
"F[0].P1[0].Policy_EffectiveDate_D[0]" => #policy.dateEffective,
"F[0].P1[0].Policy_ExpirationDate_D[0]" => #policy.term.dayEnd,
"F[0].P1[0].ExcessUmbrella_Umbrella_EachOccurrenceAmount_A[0]" => 10000000,
"F[0].P1[0].ExcessUmbrella_Umbrella_AggregateAmount_A[0]" => 10000000
send_file("#{Rails.root}/acord25.pdf", filename: "#{#policy.client.name} - #{#policy.carrier.name} - #{#policy.policyNumber} (#{Time.now}).pdf", type: "application/vnd.ms-excel")
end
end
TL;DR: Checked boxes display in Chrome, but not in Preview or Mail.
I appreciate any help or leads on what the problem might be. Thank you!
Turns out the PDF form was corrupted. Had to recreate form, not a PDFTK issue.

Fedex Plugin Rails

I working on an existing code base for a client. The app is built in rails 2.3 and uses the fedex plugin. Now i have develop a functionality for shipping the goods using international priority service type, the problem i'm facing is when ever i make a call to the fedex api for a shipping label it returns an error
"Fedex::FedexError: Unable to get label from Fedex: Customs Value is required"
Can some one help me how to mention the customs value for the label.
This is how my code is:
price, labels, tracking_number = fedex.label(
:shipper => { :contact => shipper, :address => origin },
:recipient => { :contact => recipient, :address => destination.merge(:residential => false) },
:service_type => service_type,
:packages => [{:length => 3,
:height => 4,
:width => 5,
:weight => 5,
}],
:weight => 5,
:commodities => commodities,
:customs_value =>{:currency => "USD", :amount => "100" },
:residential => is_residential_destination
)
Thanks in advance.

Rails 3.1.3 custom routing by date

How to make path helpers for this route?
resources :news
match 'news/:year/:month/:day' => 'news#show',
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
:as => 'newsdate'
I tried it many ways, but it not works:
link_to news.created_at.strftime '%d.%m.%Y ', newsdate_path(:year => '2011', :month => '11', :day => '11')
I get app error for this line by GET http://localhost:3000/news:
ArgumentError in News#index
Showing /home/foxweb/work/dev/app/views/news/index.html.slim where line #6 raised:
wrong number of arguments (2 for 1)
How to make it right way?
P.S. http://localhost:3000/news/2011/11/11 works fine.
Oh, that's common mistake. You need to take strftime arguments in braces.
link_to news.created_at.strftime('%d.%m.%Y'), newsdate_path(:year => '2011', :month => '11', :day => '11')
That's all!

ruby gem flickr-fu license problem

I've trying to get the flickr-fu gem to search correctly only for commercial friendly photos but with no luck. It seems to ignore the parameter altogether.
photos = flickr.photos.search(:text => "anything",
:tags => 'architecture',
#:license_id => '4,6,5',
:license_id => [0,4,5,6],
#:license_id => 0,
:content_type => '1',
:safe_search => '1',
:sort => 'date-posted-asc, relevance',
:per_page => 5)
The license IDs 1,2,3 are NonCommercial, so I'm trying to avoid those and take all others, but it doesn't seem to matter, i'm still getting a lot of 3s in my query.
http://www.flickr.com/services/api/flickr.photos.licenses.getInfo.html
http://github.com/commonthread/flickr_fu/blob/master/lib/flickr/photos.rb
Nevermind, this seems to work.
photos = flickr.photos.search(:text => "anything",
:tags => 'architecture',
:license => '0,4,5,6',
:content_type => '1',
:safe_search => '1',
:sort => 'date-posted-asc, relevance',
:per_page => 5)

Resources