I am trying to setup grape api. While I'm including defaults module:
module API
module V1
module Defaults
extend ActiveSupport::Concern
included do
version 'v1'
format :json
end
end
end
end
the error undefined method ` ' for API::V1::Projects:Class occurs. Also, when I paste
version 'v1'
format :json
to classes without doing mixin, it works. My operating system is Mac OS
The format :json line is indented by both, normal spaces and en spaces.
Inspecting the line's code points reveals them:
line = " format :json"
line.codepoints
#=> [8194, 32, 8194, 32, 8194, 32, 8194, 32, 102, 111, 114, 109, 97, 116, 32, 58, 106, 115, 111, 110]
# ^ ^ ^ ^
Replacing the en spaces with normal spaces should fix the problem.
Related
I'm trying to replace big % numbers to shorters versions (10000% -> 10k%). Generally code works, but if number_to_percentage used it stop working (with TOTALY SAME string).
Loading development environment (Rails 5.1.2)
2.3.1 :001 > "10000.000%".bytes
=> [49, 48, 48, 48, 48, 46, 48, 48, 48, 37]
2.3.1 :002 > helper.number_to_percentage(10000).bytes
=> [49, 48, 48, 48, 48, 46, 48, 48, 48, 37] # totally same
2.3.1 :003 > helper.number_to_percentage(10000).sub(/(\d\d)\d\d\d(?:[.,]\d+)?\%$/){ "#{$1}k%" }
=> "k%" # doesn't work
2.3.1 :004 > "10000.000%".sub(/(\d\d)\d\d\d(?:[.,]\d+)?\%$/){ "#{$1}k%" }
=> "10k%" # works
What can cause this? Any ideas?
Because number_to_percentage returns an ActiveSupport::SafeBuffer and not a String.
helper.number_to_percentage(10000).class # => ActiveSupport::SafeBuffer
ActiveSupport::SafeBuffer (which is a subclass of String) does some magic around unsafe methods like sub. That's why you can have some surprises.
The key difference is:
"10000.000%".class #=> String
number_to_percentage(10000).class # => ActiveSupport::SafeBuffer
ActiveSupport::SafeBuffer is a subclass of String, and contains the concept of UNSAFE_STRING_METHODS (including sub and gsub). This concept is useful for rails views (which is where number_to_percentage is normally used!), in relation to security; preventing XSS vulnerabilities.
A workaround would be to explicitly convert the variable to a String:
number_to_percentage(10000).to_str.sub(/(\d\d)\d\d\d(?:[.,]\d+)?\%$/){ "#{$1}k%" }
=> "10k%"
(Note that it's to_str, not to_s! to_s just returns self, i.e. an instance of ActiveSupport::SafeBuffer; whereas to_str returns a regular String.)
This article, and this rails issue go into more detail on the issue.
Alternatively, you could write your code like this, and it works as expected:
number_to_percentage(10000).sub(/(\d\d)\d\d\d(?:[.,]\d+)?%$/, '\1k%')
#=> "10k%"
I would actually prefer this approach, since you are no longer relying on modification to the (non-threadsafe) global variable.
I am trying to scrape player information from MLS sites to create a map of where the players come from, as well as other information. I am as new to this as it gets.
So far I have used this code:
require 'HTTParty'
require 'Nokogiri'
require 'JSON'
require 'Pry'
require 'csv'
page = HTTParty.get('https://www.atlutd.com/players')
parse_page = Nokogiri::HTML(page)
players_array = []
parse_page.css('.player_list.list-reset').css('.row').css('.player_info').map do |a|
player_info = a.text
players_array.push(player_info)
end
#CSV.open('atlantaplayers.csv', 'w') do |csv|
# csv << players_array
#end
pry.start(binding)
The output of the pry function is:
"Miguel Almirón10\nMidfielder\n-\nAsunción, ParaguayAge:\n23\nHT:\n5' 9\"\nWT:\n140\n"
Which when put into the csv creates this in a single cell:
"Miguel Almirón10
Midfielder
-
Asunción, ParaguayAge:
23
HT:
5' 9""
WT:
140
"
I've looked into things and have determined that it is possible nodes (\n)? that is throwing off the formatting.
My desired outcome here is to figure out how to get the pry output into the array as follows:
Miguel, Almiron, 10, Midfielder, Asuncion, Paraguay, 23, 5'9", 140
Bonus points if you can help with the accent marks on names. Also if there is going to be an issue with height, is there a way to convert it to metric?
Thank you in advance!
I've looked into things and have determined that it is possible nodes (\n)? that is throwing off the formatting.
Yes that's why it's showing in this odd format, you can strip the rendered text to remove extra spaces/lines then your text will show without the \ns
player_info = a.text.strip
[1] pry(main)> "Miguel Almirón10\n".strip
=> "Miguel Almirón10"
This will only remove the \n if you wish to store them in a CSV in this order
Miguel, Almiron, 10, Midfielder, Asuncion, Paraguay, 23, 5'9", 140
then you might want to split by spaces and then create an array for each row so when pushing the line to the CSV file it will look like this:
csv << ["Miguel", "Almiron", 10, "Midfielder", "Asuncion", "Paraguay", 23, "5'9\"", 140]
with the accent marks on names
you can use the transliterate method which will remove accents
[8] pry(main)> ActiveSupport::Inflector.transliterate("Miguel Almirón10")
=> "Miguel Almiron10"
See http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate and you might want to require 'rails' for this
Here's what I would use, i18n and people gems:
require 'people'
require "i18n"
I18n.available_locales = [:en]
#np = People::NameParser.new
players_array = []
parse_page.css('.player_info').each do |div|
name = #np.parse I18n.transliterate(div.at('.name a').text)
players_array << [
name[:first],
name[:last],
div.at('.jersey').text,
div.at('.position').text,
]
end
# => [["Miguel", "Almiron", "10", "Midfielder"],
# ["Mikey", "Ambrose", "22", "Defender"],
# ["Yamil", "Asad", "11", "Forward"],
# ...
That should get you started.
My workbook always named like my template "invoices_generate.xlsx".
How can i rename this File ?
Template "invoices_generate.xlsx.axlsx" :
wb = xlsx_package.workbook
wb.add_worksheet(:name => "Beleg") do |sheet|
.
.
.
sheet.column_widths 2 , 11, 11, 11, 11, 23, 3
end
Do you have the axlsx_rails gem added as well? If so:
wb = xlsx_package.workbook
wb.add_worksheet(name: "Beleg") do |sheet|
sheet.column_widths 2 , 11, 11, 11, 11, 23, 3
end
See https://github.com/straydogstudio/axlsx_rails#template for more details and if that doesn't work try setting the filename from the controller action rendering the xlsx template as described here: https://github.com/straydogstudio/axlsx_rails#file-name
For future references: In the controller, on the function where generate the workbook
def index
#users = User.all
render xlsx: 'export', filename: 'my_new_filename.xlsx' //Render with export and naming the file
respond_to do |format|
format.html
format.xlsx
end
end
I have correct code, but Prawn complains to it:
class MyClass < Prawn::Document
# ....
def def123
table main_table , width: bounds.width
end
def main_table
[[
"0","1", "2", "3", "4"
]] +
[
[{content: "data1", colspan: 4}, "111"],
[{content: "data2", colspan: 4}, "222"],
[{content:"data3", colspan: 4}, "333"]
]
end
end
by saying:
undefined method colspan= for Prawn::Table::Cell::Text:0x007fb86c3e7020
Note that I need to use width: bounds.width to be able to make the table fill a whole page.
prawn (0.12.0)
I'd recommend updating your Gem file to pull prawn directly from the git repository. Most undefined method errors are the result of using an out of date version:
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git"
I'm having an encoding problems with Portuguese characters. It didn't render correctly.
This is replacing the Portuguese characters by ���
In my email method I have something like:
#html_content = html_content
#text_content = text_content
mail(
:from => "#{from_name} <notifications#email.com>",
:to => options[:recipients],
:subject => options[:subject]
)
and in my views I have something like that:
<%= #html_content %>
I already checked the variable content and everything looks good. So probably the problem happens inside the method mail(...)
I tried to follow this instructions
actionmailer encoding - rendering garbage in email client
and didn't work. Any idea?
I'm using Rails 3.2.2 and Ruby 1.9.3
[UPDATE]
The bytes values are:
[208, 146, 209, 150, 209, 130, 208, 176, 208, 187, 209, 150, 208, 185]
[80, 111, 114, 116, 117, 103, 117, 195, 170, 115]
The message content is:
Віталій
Português