What I want to do:
grab the base64 readme.md contents of the github API. F.e. https://api.github.com/repos/airbnb/lottie-ios/contents/README.md
In my ruby application decode the base64, this should returns markdown. This markdown I can parse to HTML to be used on my site.
require 'commonmarker'
require "base64"
require 'logger'
class ToolsController < ApplicationController
impressionist :actions=>[:show]
def show
log = Logger.new('log.txt')
#tool = Tool.friendly.where.not("status ~* ?", "(Scheduled|Draft)").find(params[:id])
impressionist(#tool)
#related_tools = #tool.categories.first.tools.published.where.not(id: #tool.id).sample(3)
encoded_string = File.read("test-tool-readme.txt")
decoded_string = Base64.decode64(encoded_string)
log.debug(decoded_string) # EMPTY
#tool_test_markdown_body = CommonMarker.render_doc(decoded_string, :DEFAULT).to_plaintext
render "show-with-github"
end
end
For some reason it will not decode the base64... online tools or local tools seem to do it perfectly. It returns null. In strict mode it fails.
Does anyone know why I can't decode this base64?
Related
My RoR app is on Heroku and active storage is configured correctly. However, when I'm in the app and fetch the uploaded document, the url is something like https://cremers.s3.eu-west-1.amazonaws.com/cx9xy0pmbieagvuw8a0vzcnfhvcc?response-content-disposition=inline%3B filename%3D"Digeste_9.1.pdf"%3B....
How to change this to a "normal" url, like https://www.cremers.fr/documents/digest_9.1.pdf ?
You can use a route/controller to act as a sort of proxy. I've done exactly this, below is roughly the code I used, edited for your specifics.
I did not test this with your settings, obviously, and I wasn't using ActiveStorage in my case, so you might need/want to adjust, but this should get you started:
# config/routes.rb
Rails.application.routes.draw do
get '/documents/:filename.:format.:compression', to: 'documents#show'
end
# app/controllers/documents_controller.rb
require 'open-uri'
class DocumentsController < ApplicationController
def show
bucket_name = 'cremers'
aws_region = 'eu-west-1'
filename = params[:filename]
s3_url = "https://s3-#{aws_region}.amazonaws.com/#{bucket_name}/#{filename}"
data = open(s3_url)
send_data data.read, type: data.content_type
end
end
Using Rails 4.2.10
I would like to open image from URL thanks to mongoid papaerclip and open_uri
It perfectly works in 95% of use cases but some website send me a 404 when they see the user-agent of the request is Ruby.
The problem is with the lib paperclip=>
paperclip/io_adapters/uri_adapter.rb in download_content at line 48
def download_content
options = { read_timeout: Paperclip.options[:read_timeout] }.compact
open(#target, **options)
end
If I could add here an option it would be great but I don't think it's possible so I would like to add a default header with my user-agentto all request done by open_uri
Luckily for your use case there is no such thing as a class being closed against modification in ruby.
Add a patch to your rails app in an initializer. The structure is roughly as follows:
In config/initializers/some_arbitrary_name.rb
module UriAdapterPatch
def open(url, options)
# alter the objects however you want
super(altered_or_original_url, altered_or_original_options)
end
end
Paperclip::UriAdapter.prepend(UriAdapterPatch)
Solution for paperclip-3.5.4
module Paperclip
class UriAdapter < AbstractAdapter
def download_content
open(#target,"User-Agent" => "Your Custom User Agent")
end
end
end
# for example put it in config/initializers/paperclip_user_agent.rb
For other versions just write in project folder
gem which paperclip
and find in path from output file paperclip/io_adapters/uri_adapter.rb
There is function def download_content, it is your aim to rewrite
I'm a bit confused on how to handle this. Essentially I'm trying to learn more about Angular on Rails, so I'm using Rails in API Mode - However, currently, I'm trying to build an API into the non-API version of Rails I initiated (Not quite relevant, but I'm curious how to get it to work for both).
Very basic test off the bat, I'd like to his
/steam/:id endpoint (in this example) and get the return for
GetPlayedSummaries. HTTParty is installed, and working in terminal.
class SteamController < ApplicationController
#steamKey = "{REDACTED}"
#baseUrl = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='+#steamKey+'&steamids='+#steamId+'"
def get_player_summaries
#steamId = params[:id]
puts #baseUrl
#games = HTTParty.get(#baseUrl).to_json
puts response.body, response.code, response.message, response.headers.inspect
end
end
This is my very basic controller. Realistically I have no reason to have steamKey separated but it helps encourage where I need help.
Originally I had #baseUrl as
#baseUrl = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=#{#steamKey}+'&steamids=#{#steamId}"
With the full version I get a bad argument (expected URI object or URI string) pointing at the #baseUrl so I feel like I'm not including it properly. I'm working on that but even once it works I'm not quite sure how to make it so it just throws it out into the UI as pure JSON? I have this done with a lot of my other elements using JSON Builders etc, but that's DB Data. This is just an API call.
Insight/documents I can check out?
EDIT: I believe my issue regarding using #steamKey is related to This SO Article. # is even being passed down to get_player_summaries
As mentioned in my edit, I was able to find the article to explain how to properly use URLs.
My working code is:
class SteamController < ApplicationController
def initialize
#steamKey = "{REDACTED}"
#baseUrl = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key="+#steamKey+"&steamids="
end
def get_player_summaries
#playerUrl = #baseUrl + params[:id]
#games = HTTParty.get(#playerUrl).to_json
puts response.body, response.code, response.message, response.headers.inspect
render json: #games
end
end
It's a bit slow on the response but could be an issue with C9.
Relatively new to Rails. I am trying to call an API and it's supposed to return a unique URL to me. I have HTTParty bundled on my app. I have created a UniqueNumber controller and I have read through several HTTParty guides as far as what I want but maybe I'm just a bit lost and really have no idea what to do.
Basically, all I need to do is call the API, get the URL it returns, then insert that URL into the database for a user. Can anyone point me in the right direction or share some code with me?
Let's assume the API is in a JSON format and returns the data like so:
{"url": "http://example.com/unique-url"}
To keep things tidy and well structured, the API logic should belong in it's own class:
# lib/url_api.rb
require 'httparty'
class UrlApi
API_URL = 'http://example.com/create'
def unique_url
response = HTTParty.get(API_URL)
# TODO more error checking (500 error, etc)
json = JSON.parse(response.body)
json['url']
end
end
Then call that class in the controller:
require 'url_api'
class UniqueNumberController < ApplicationController
def create
api = UrlApi.new()
url = api.unique_url
#user = # Code to retrieve User
#user.update_attribute :url, url
# etc
end
end
Basically HTTParty returns a response object that contains the HTTP response data which includes both the headers and the actual content (.body). The body contains a string of data that you can process as you like. In this case, we're parsing the string as JSON into a Ruby hash. If you need to customise the HTTP request to the API you can see all the options in the HTTParty documentation.
I want to be able to upload an Excel file that contains contact information. I then went to be able to parse it and create records for my Contact model.
My application is a Rails application.
I am using the paperclip gem on heroku, I've been able to parse vim cards into the Contact model, and am looking for something similar, but will go through all lines of the Excel file.
Gems that simplify the task and sample code to parse would be helpful!
Spreadsheet is the best Excel parser that I have found so far. It offers quite a lot of functionality.
You say you use Paperclip for attachments which is good. However, if you store the attachments in S3 (which I assume since you use Heroku) the syntax for passing the file to spreadsheet is a little different but not difficult.
Here is an example of the pure syntax that can be used and not placed in any classes or modules since I don't know how you intend to start the parsing of contacts.
# load the gem
require 'spreadsheet'
# In this example the model MyFile has_attached_file :attachment
#workbook = Spreadsheet.open(MyFile.first.attachment.to_file)
# Get the first worksheet in the Excel file
#worksheet = #workbook.worksheet(0)
# It can be a little tricky looping through the rows since the variable
# #worksheet.rows often seem to be empty, but this will work:
0.upto #worksheet.last_row_index do |index|
# .row(index) will return the row which is a subclass of Array
row = #worksheet.row(index)
#contact = Contact.new
#row[0] is the first cell in the current row, row[1] is the second cell, etc...
#contact.first_name = row[0]
#contact.last_name = row[1]
#contact.save
end
I had a similar requirement in one of my Rails 2.1.0 application. I solved it in the following manner:
In the 'lib' folder I wrote a module like this:
require 'spreadsheet'
module DataReader
def read_bata(path_to_file)
begin
sheet = book.worksheet 0
sheet.each 2 do |row|
unless row[0].blank?
# Create model and save it to DB
...
end
end
rescue Exception => e
puts e
end
end
end
Had a model Upload:
class Upload < AR::Base
has_attached_file :doc,
:url => "datafiles/:id",
:path => ":rails_root/uploads/:id/:style/:basename.:extension"
# validations, if any
end
Generated an UploadsController which would handle the file upload and save it to appropriate location. I used Paperclip for file upload.
class UploadsController < AC
include DataReader
def new
#upload = Upload.new
end
def create
#upload = Upload.new(params[:upload])
#upload.save
file_path = "uploads/#{#upload.id}/original/#{#upload.doc_file_name}"
#upload.read = DataReader.read_data(file_path)
# respond_to block
end
end
Read about 'spreadsheet' library here and here. You can make appropriate improvements and make the technique work in Rails 3. Hope this helps.
I made a gem to achieve this easily. I called it Parxer and...
It's built on to of roo gem.
It allows you to parse xls, xlsx and csv files.
Has a DSL to handle:
Column mapping.
File, row, and column/cell validation.
Column/cell formatting.