NoMethodError: undefined method `bytesize' for #<Array: - ruby-on-rails

I am trying to call my custom api which uploades file using paperclip , its working fine on local using https://github.com/jwagener/httmultiparty , but when I try to call the api and upload file for heroku deployed app, following errors occurs.
NoMethodError: undefined method `bytesize' for #<Array:0x00000006e71980>
from /home/umair/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/net/http.rb:1735:in `send_request_with_body'
from /home/umair/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/net/http.rb:1724:in `exec'
from /home/umair/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/net/http.rb:1189:in `transport_request'
from /home/umair/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/net/http.rb:1177:in `request'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
from /home/umair/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/net/http.rb:1170:in `block in request'
from /home/umair/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/net/http.rb:627:in `start'
from /home/umair/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/net/http.rb:1168:in `request'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/httparty-0.8.3/lib/httparty/request.rb:76:in `perform'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/httparty-0.8.3/lib/httparty/request.rb:208:in `handle_response'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/httparty-0.8.3/lib/httparty/request.rb:90:in `perform'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/httparty-0.8.3/lib/httparty.rb:400:in `perform_request'
from /home/umair/Projects/core/lib/httmultiparty.rb:81:in `post'
from (irb):5
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /home/umair/.rvm/gems/ruby-1.9.2-p320#gm/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
I am running this on ruby console to test the file upload api.
HttpClient.post('/storage.xml',:basic_auth=> {:username=>'badde88248834bec759d94563f102d52e19702fa',:password=>'b890b48e2769192372bf531fc46827cd86bea23f'} ,:query => {:auth_token=> '1045aa26cda23f5f3aec737a45e7ba0017c1a7cf',:user_file => File.new( '/home/umair/Login.png')})

I ran into the same problem and couldn't find a solution. Now I'm doing this, it only requires the multipart-post gem.
class Client
require 'net/http/post/multipart'
def self.perform inputurl, file
url = URI.parse(inputurl)
File.open(file) do |transfile|
Net::HTTP.start(url.host, url.port) do |http|
req = Net::HTTP::Post::Multipart.new(
url.path,
file1: UploadIO.new(transfile, "multipart/formdata", File.basename(file)),
)
response = http.request(req)
end
end
end
end

Related

API endpoint link is working in Postman but not in Ruby on Rails with ActiveResource

I am working on Ruby on Rails application which requires to access an External API via GET requests. The request needs to send some parameters with request. I have used Activeresource gem for accessing API. The API request is working fine in Postman and giving expected records but gives me a 400 error from Rails application.
Following is the code
class PayRunEmploymentHeroBase < ActiveResource::Base
self.site = "https://api.yourpayroll.com.au"
self.user = ENV['EMPLOYMENT_HERO_USERNAME']
self.element_name = ""
self.include_format_in_path = false
self.prefix = "/api/v2/business/:business_id/payrun?$filter=DateFinalised gt datetime':finalised_date'"
end
I am pasting here the details of 400 error while accessing the link from Rails app.
Reloading...
=> true
2.3.0 :006 > PayRunEmploymentHeroBase.find(:all, params: {:business_id => business_id, :finalised_date => finalised_date})
##############################################INFO##############################################
{
"method": "get",
"request_uri": "https://api.yourpayroll.com.au:443/api/v2/business/48058/payrun?$filter=DateFinalised gt datetime'2017-11-16'",
"http_status": "400",
"http_message": "Bad Request"
}
##############################################INFO##############################################
ActiveResource::BadRequest: Failed. Response code = 400. Response message = Bad Request.
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:141:in `handle_response'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:126:in `request'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:85:in `block in get'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:220:in `with_auth'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:85:in `get'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/base.rb:1073:in `find_every'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/base.rb:974:in `find'
from (irb):6
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/console.rb:65:in `start'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<main>'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:293:in `block in require'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:293:in `require'
from /Users/bebble1/work/awe/bin/rails:9:in `<main>'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `load'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `load'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:287:in `block in load'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:287:in `load'
from /Users/bebble1/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/bebble1/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in `require'
I have also attached a picture here of the outcome from Rails app
It seems like you are not authenticating with Rails app. And I am sure you have set authentication username/password in Postman.
Set self.password = "password" (use your own password) to PayRunEmploymentHeroBase and it will work.
Update:
It's actually your url is not encoded, do this and it will solve the issue:
self.prefix = "/api/v2/business/:business_id/payrun?$filter=" + URI::encode("DateFinalised gt datetime':finalised_date'")

How do I change my IP address using Ruby/Tor?

I”m using Rails 4.2.7 and I have several Tor gems installed.
gem 'tor'
gem 'tor_requests'
gem 'tor-privoxy'
gem 'net-telnet'
I started my Tor browser (running on Mac El Capitan) and I want to periodically (every 20th request) change the IP address of where my TOR web requests originate. So I tried this
agent = TorPrivoxy::Agent.new '127.0.0.1', '', {8118 => 9151} do |agent|
sleep 1
puts "New IP is #{agent.ip}"
end
However, this results in the below error. I’m confused about how else I need to configure things so that I can make the above work.
Error during processing: Failed to open TCP connection to 127.0.0.1:8118 (general SOCKS server failure)
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:882:in `rescue in block in connect'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:879:in `block in connect'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/timeout.rb:91:in `block in timeout'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/timeout.rb:101:in `timeout'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:878:in `connect'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:863:in `do_start'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:858:in `start'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:700:in `start'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:631:in `connection_for'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/mechanize-2.7.5/lib/mechanize/http/agent.rb:274:in `fetch'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/mechanize-2.7.5/lib/mechanize.rb:464:in `get'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-privoxy-0.1.1/lib/tor-privoxy/agent.rb:38:in `ip'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:57:in `block in get_content'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-privoxy-0.1.1/lib/tor-privoxy/agent.rb:11:in `initialize'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:55:in `new'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:55:in `rescue in get_content'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:50:in `get_content'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:14:in `get_url'
/Users/davea/Documents/workspace/myproject/app/services/onlinerr_race_finder_service.rb:41:in `get_race_list'
/Users/davea/Documents/workspace/myproject/app/services/abstract_race_finder_service.rb:26:in `process_data'
/Users/davea/Documents/workspace/myproject/app/services/run_crawlers_service.rb:18:in `block in run_all_crawlers'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
/Users/davea/Documents/workspace/myproject/app/services/run_crawlers_service.rb:5:in `run_all_crawlers'
(irb):2:in `irb_binding'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/workspace.rb:87:in `eval'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/workspace.rb:87:in `evaluate'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/context.rb:380:in `evaluate'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:489:in `block (2 levels) in eval_input'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:623:in `signal_status'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:486:in `block in eval_input'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:246:in `block (2 levels) in each_top_level_statement'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:232:in `loop'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:232:in `block in each_top_level_statement'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:231:in `catch'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:231:in `each_top_level_statement'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:485:in `eval_input'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:395:in `block in start'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:394:in `catch'
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:394:in `start'
How do I programmatically force an IP address change using Ruby/Tor?
Edit:Here is what I included in my helper file and below is the error that results
require 'rubygems'
$:.unshift "./tor/lib"
require 'tor'
…
cookie_file = '/Users/davea/Library/Application Support/TorBrowser-Data/Tor/control_auth_cookie'
file = File.open(cookie_file, 'rb')
cookie = file.read # read contents of auth cookie to string
file.close
Tor::Controller.connect(:port => 9150, :cookie => cookie) do |tor|
tor.signal('NEWNYM') # send NEWNYM signal (gets new IP)
end
The error that results in the rails console …
Error during processing: undefined method `signal' for #<Tor::Controller:0x007fe044b1e550>
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:87:in `block in get_content'
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-0.1.2/lib/tor/control.rb:38:in `connect'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:86:in `rescue in get_content'
Edit 2:
After adding this to my Gemfile file
gem 'tor', :git => 'https://github.com/dryruby/tor.rb.git'
I ran the code above, and now got the error …
Error during processing: end of file reached
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:301:in `readline'
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:301:in `read_reply'
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:194:in `authenticate'
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:282:in `send_command'
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:269:in `signal'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:90:in `block in get_content'
To change your IP in Tor, you need to connect directly to the controller (it uses a telnet-like line based command/response protocol.
It looks like you're trying to connect through Privoxy which isn't necessary. The controller only allows local connections by default.
Tor Browser's Tor config enforces authentication uses the cookie method, without changing any options in torrc you need to read the contents of the auth cookie to connect.
I was able to change the IP with the following code:
require 'rubygems'
$:.unshift "./tor/lib"
require 'tor'
# the cookie file contains a "password" for authentication, 32 random bytes
cookie_file = '/home/me/tor-browser_en-US/Browser/TorBrowser/Data/Tor/control_auth_cookie'
file = File.open(cookie_file, 'rb')
cookie = file.read # read contents of auth cookie to string
file.close
Tor::Controller.connect(:port => 9151, :cookie => cookie) do |tor|
p tor.signal('NEWNYM') # send NEWNYM signal (gets new IP)
end
tor.signal returns a string, which should read "250 OK" if the NEWNYM command was successful. There is some internal rate limiting which prevents this from being run too often (like every 10 seconds I think) which shouldn't be a problem for you.
EDIT: The 0.1.2 (current release) of this Gem doesn't include the signal method from the master branch. You'll need to use the latest code or replicate their code instead of calling signal.

Download file from S3 with link in Ruby and 403 Forbidden error

I'm now trying to implement function that reads the uri of S3 file ( mp4 ) and puts to the rails server.
def self.download
uri = 'https://s3.amazonaws.com/tokbox.com.archive2/45550892%2F324cad13-b447-4d3c-9477-c829665c8180%2Farchive.mp4?forprivacytherestofaddressisnotshownhere'
contents = URI.parse(uri)
File.open('testtest.mp3', 'wb') do |f|
f.write contents.read
end
end
But unfortunately, I'm now seeing these following errors.
OpenURI::HTTPError: 403 Forbidden
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:353:in `open_http'
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:724:in `buffer_open'
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:210:in `block in open_loop'
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:208:in `catch'
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:208:in `open_loop'
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:704:in `open'
from /Users/sungpah/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:712:in `read'
from (irb):9:in `block in irb_binding'
from (irb):8:in `open'
from (irb):8
I've tried to parse uri, or make it string, but it hasn't been working with me.
I'd love to hear any good idea or hunch to move forward!!!
Best

How to render rails mailer views in Mailgun HTTP API

I'm using Mailgun HTTP API to send emails from rails application.But I'm not able to render the mailer views. This is my mailer class.
class MailgunMails < ActionMailer::Base
def send_complex_message
data = Multimap.new
data[:from] = "Excited User <postmasters##{##domain}>"
data[:to] = "alice#example.com"
data[:subject] = "Hello"
data[:html] = File.open("#{Rails.root}/app/views/mailgun_mails/send_complex_message.html.erb", 'r').to_s.html_safe
## also tried
data[:html] = render(template: "send_complex_message.html.erb")
response = RestClient.post "https://api:#{##private_key}"\
"#api.mailgun.net/v2/#{##domain}/messages", data
response = JSON.parse(response)
end
end
But that throws:
NoMethodError: undefined method `each_byte' for nil:NilClass
How do I use mailer views with Mailgun API.
PS: I know there are many gems available to use mailgun with rails but none of them gives full functionality as the API does.
IF there is any alternative way to send emails from the api using the rails views, I'm waiting for that answer. I just don't know how to do that (even if it doesn't use actionmailer)
FULL error trace
NoMethodError: undefined method `each_byte' for nil:NilClass
from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:307:in `block in escape'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/core_ext/string/output_safety.rb:177:in `gsub'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/core_ext/string/output_safety.rb:177:in `gsub'
from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:304:in `escape'
from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:623:in `escape'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:150:in `handle_key'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:143:in `block in build_stream'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:142:in `collect'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:142:in `build_stream'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:53:in `initialize'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:18:in `new'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:18:in `generate'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:45:in `initialize'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `new'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
... 2 levels...
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:373:in `_run__1281843520849433682__process_action__callbacks'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/callbacks.rb:17:in `process_action'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/base.rb:136:in `process'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/rendering.rb:44:in `process'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:503:in `process'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:497:in `initialize'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:480:in `new'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:480:in `method_missing'
from (irb):1
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
How about using the render_to_string method? I believe you want to dynamically generate the view content. Reading up the view template by opening up the file would not run any Ruby code in it.
class MailgunMails < ActionMailer::Base
def send_complex_message
data = Multimap.new
data[:from] = "Excited User <postmasters##{##domain}>"
data[:to] = "alice#example.com"
data[:subject] = "Hello"
html_output = render_to_string template: "mailgun_mails/send_complex_message"
data[:html] = html_output.to_str
response = RestClient.post "https://api:#{##private_key}"\
"#api.mailgun.net/v2/#{##domain}/messages", data
response = JSON.parse(response)
end
end

Twitter gem & Rails 3 - TypeError: can't convert Pathname into String

I'm trying to use the twitter gem in a rails 3 app but keep getting the following error:
TypeError: can't convert Pathname into String
Here's the code I'm trying to run:
class Tweet < ActiveRecord::Base
def self.test_tweet
Twitter.user_timeline("sferik").first.text
end
end
Here's the full error message:
TypeError: can't convert Pathname into String
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:658:in `initialize'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:658:in `new'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:658:in `connect'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:626:in `start'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:1168:in `request'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:888:in `get'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/adapter/net_http.rb:59:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/response.rb:8:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/response.rb:8:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/response.rb:8:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/request/url_encoded.rb:14:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/request/multipart.rb:13:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request/multipart_with_file.rb:17:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request/phoenix.rb:13:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/connection.rb:210:in `run_request'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request.rb:23:in `request'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request.rb:11:in `get'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/client/timelines.rb:208:in `user_timeline'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter.rb:17:in `method_missing'
from /home/shane/projects/bv_data/app/models/tweet.rb:5:in `test_tweet'
from (irb):1
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/railties-3.1.2/lib/rails/commands/console.rb:45:in `start'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/railties-3.1.2/lib/rails/commands/console.rb:8:in `start'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/railties-3.1.2/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I get this error even when running the same code from the console:
Twitter.user_timeline("sferik").first.text
I created a blank test app with the same version of rails and the twitter gem and it runs no problem so I'm not sure what the problem is.
As I had this working before in the app I went back through my commits until I could find a point were it was working and what I did to break it.
So in case anyone happens to be in the same position what I had added was a fix_ssl.rb file in config/initializers which was to address a previous SSL connection issue. Removing this file allowed the Twitter gem to work correctly again.
config/initializers/fix_ssl.rb
require 'open-uri'
require 'net/https'
module Net
class HTTP
alias_method :original_use_ssl=, :use_ssl=
def use_ssl=(flag)
self.ca_file = Rails.root.join('lib/ca-bundle.crt')
self.verify_mode = OpenSSL::SSL::VERIFY_PEER
self.original_use_ssl = flag
end
end
end

Resources