tuxwebror/app/helpers/users_helper.rb
2018-12-21 19:06:39 +01:00

54 lines
1 KiB
Ruby

module UsersHelper
def markdown(text)
if !text.nil? && !text.blank?
text = put_emote(
Rack::Utils.escape_html(
text
)
.gsub(
/\n/,
'<br>'
)
.gsub(
/```/,
''
)
)
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
md.render(text)
else
'n/a'
end
end
def get_location
result = ActiveSupport::JSON.decode(Timeout::timeout(5) {Net::HTTP.get_response(URI.parse('http://ip-api.com/json/' + request.remote_ip)).body})
if result['status'] != "fail"
result['regionName']
end
end
private
def put_emote(text)
splitted_text = text.split
clean_text = []
regex = "&lt;:([a-zA-Z0-9]*):[0-9]{18}&gt;"
splitted_text.each do |word|
if word.match(/#{regex}/)
emote_name = word.match(/#{regex}/).captures[0]
begin
formated_word = image_tag("icones/#{emote_name}.png", size: 16, alt: emote_name)
rescue
formated_word = ":#{emote_name}:"
end
else
formated_word = word
end
clean_text.push(formated_word)
end
clean_text.join(' ')
end
end