MMS2R and Multiple Images Rails - email

Here's my code:
require 'mms2r'
class IncomingMailHandler < ActionMailer::Base
##
# Receives email(s) from MMS-Email or regular email and
# uploads that content the user's photos.
# TODO: Use beanstalkd for background queueing and processing.
def receive(email)
begin
mms = MMS2R::Media.new(email)
##
# Ok to find user by email as long as activate upon registration.
# Remember to make UI option that users can opt out of registration
# and either not send emails or send them to a username+32523#example.com
# type address.
##
# Remember to get SpamAssasin
if (#user = User.find_by_email(email.from) && email.has_attachments?)
mms.media.each do |key, value|
if key.include?('image')
value.each do |file|
#user.photos.push Photo.create!(:uploaded_data => File.open(file), :title => email.subject.empty? ? "Untitled" : email.subject)
end
end
end
end
ensure
mms.purge
end
end
end
and here's my error:
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48: undefined method `photos' for true:TrueClass (NoMethodError)
from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `each'
from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `receive'
from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `each'
from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `receive'
from /usr/local/lib/ruby/gems/1.8/gems/actionmailer-2.3.4/lib/action_mailer/base.rb:419:in `receive'
from (eval):1
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /home/xxx/script/runner:3
I sent an email to the server with two image attachments. Upon receiving the email the server runs
"| ruby /xxx/script/runner 'IncomingMailHandler.receive STDIN.read'"
What is going on? What am I doing wrong?
(MMS2R docs)

Please replace
if (#user = User.find_by_email(email.from) && email.has_attachments?)
with
if ((#user = User.find_by_email(email.from)) && email.has_attachments?)

Related

How create a Login with Kivy and Python

I would like to create a GUI with Kivy to login to a website, Is it possible?
Here is a part of the code.
Instead of logging in from the terminal I would like to create an android application that does some web scraping for me once I log in..
def login():
driver.get("https://sgv.ivu-cloud.com/mbweb/j_security_check")
# find username/email field and send the username itself to the input field
driver.find_element(By.ID, 'j_username').send_keys(username)
# find password input field and insert password as well
driver.find_element(By.ID, 'j_password').send_keys(password)
# click login button
driver.find_element(By.ID, 'login-button').click()
# wait the ready state to be complete
WebDriverWait(driver=driver, timeout=5).until(
lambda x: x.execute_script("return document.readyState === 'complete'")
)
error_message = "Login fehlgeschlagen."
# get the errors (if there are)
errors = driver.find_elements(By.CLASS_NAME, "highlight_login")# print the errors optionally
#for e in errors:
# print(e)
# if we find that error message within errors, then login is failed
if any(errors):
print("Anmeldung fehlgeschlagen!")
print('Falscher Benutzername oder Passwort!\nPrüfe die Exceldatei "LoginDaten.xlsx", ob die Felder korrekt ausgefüllt sind!')
exit()
else:
print("Anmeldung erfolgreich!")
loggedin=True
login()

Elixir issue with Bamboo.SentEmailViewerPlug

I do have an issue with mentioned function. This is the error from browser:
function Bamboo.SentEmailViewerPlug.init/1 is undefined (module Bamboo.SentEmailViewerPlug is not available)
And this is from console :
[error] #PID<0.868.0> running RewardappWeb.Endpoint (connection #PID<0.829.0>, stream id 4) terminated
Server: localhost:4000 (http)
Request: GET /mailbox
** (exit) an exception was raised:
** (UndefinedFunctionError) function Bamboo.SentEmailViewerPlug.init/1 is undefined (module Bamboo.SentEmailViewerPlug is not available)
Bamboo.SentEmailViewerPlug.init([])
(phoenix 1.6.6) lib/phoenix/router/route.ex:41: Phoenix.Router.Route.call/2
(phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.__call__/2
(rewardapp 0.1.0) lib/rewardapp_web/endpoint.ex:1: RewardappWeb.Endpoint.plug_builder_call/2
(rewardapp 0.1.0) lib/plug/debugger.ex:136: RewardappWeb.Endpoint."call (overridable 3)"/2
(rewardapp 0.1.0) lib/rewardapp_web/endpoint.ex:1: RewardappWeb.Endpoint.call/2
(phoenix 1.6.6) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Here is my router.ex file :
defmodule RewardappWeb.Router do
use RewardappWeb, :router
use Phoenix.Router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, {RewardappWeb.LayoutView, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", RewardappWeb do
pipe_through :browser
#get "/", PageController, :index
#NEW ROUTES
#get "/users", GrantController, :login
get "/admin", GrantController, :admin
get "/main", GrantController, :main
get "/", GrantController, :index
post "/", GrantController, :login
get "/add", GrantController, :add
post "/add/:id", GrantController, :update
get "/admin/delete/:id", GrantController, :delete
end
if Mix.env == :dev do
forward "/mailbox", Bamboo.SentEmailViewerPlug
end
# Other scopes may use custom stacks.
# scope "/api", RewardappWeb do
# pipe_through :api
# end
# Enables LiveDashboard only for development
#
# If you want to use the LiveDashboard in production, you should put
# it behind authentication and allow only admins to access it.
# If your application does not have an admins-only section yet,
# you can use Plug.BasicAuth to set up some basic authentication
# as long as you are also using SSL (which you should anyway).
if Mix.env() in [:dev, :test] do
import Phoenix.LiveDashboard.Router
scope "/" do
pipe_through :browser
live_dashboard "/dashboard", metrics: RewardappWeb.Telemetry
end
end
# Enables the Swoosh mailbox preview in development.
#
# Note that preview only shows emails that were sent by the same
# node running the Phoenix server.
if Mix.env() == :dev do
scope "/dev" do
pipe_through :browser
#forward "/mailbox", Plug.Swoosh.MailboxPreview
end
end
end
Mails are being sent totally fine - I have inspected that with IO.inspect, and right now, they are sent fine. However, I can not attempt to see localhost:4000/mailbox. Where is my mistake? Thank you in advance!!
The issue was with wrong name Bamboo module name.

Return Sinatra response before stopping EventMachine

I'm using Sinatra within event-machine, and I want to shut down the server and exit upon receiving a DELETE request, and return a 200 OK. However, right now I can't get to that point, and always end u returning an empty response before exiting. How would I achieve this? Here's the relevant code:
EM.run do
class Server < Sinatra::Base
delete '*' do
EM.defer proc{ halt 200 }, proc{ EM.stop }
end
end
Server.run!
end
What happens is that I get an empty reply, and get the following stacktrace:
/Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/sinatra-1.3.3/lib/sinatra/base.rb:803:in `throw': uncaught throw `halt' in thread 0x7fa4225f2020 (ThreadError)
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/sinatra-1.3.3/lib/sinatra/base.rb:803:in `halt'
from instant-markdown-d.rb:39:in `DELETE *'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `call'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `spawn_threadpool'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `initialize'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `new'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `spawn_threadpool'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1023:in `defer'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/thin-1.5.0/lib/thin/connection.rb:51:in `process'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/thin-1.5.0/lib/thin/connection.rb:39:in `receive_data'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /Users/syeo/.rvm/gems/ruby-1.8.7-p352#instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from instant-markdown-d.rb:10
I've also tried many similar ways but couldn't find a way to send a 200, then shut down the server.
Ended up doing this:
EM.run do
class Server < Sinatra::Base
delete '*' do
EM.add_timer(0.2) do
EM.stop
exit
end
status 200
end
end
Server.run!
end
Which is a hack, but at least works.

RailsTutorial: NoMethodError 'permanent' Rake::Test::CookieJar

app/helpers/sessions_helper.rb
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
#current_user = user
end
def current_user
#current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
end
Tests defined in section 9.2.1 Requiring signed-in users are failing:-
At first I was getting sign_in method not found then I added
include SessionsHelper
in spec/utilities.rb file after that I started getting below error, saying no method with name permanent exists in Rake::Test::CookieJar.
Is it due to some Gem version issue.
1) User Pages edit page
Failure/Error: before { sign_in user}
NoMethodError:
undefined method `permanent' for #<Rack::Test::CookieJar:0x007ff12c661e88>
# ./app/helpers/sessions_helper.rb:3:in `sign_in'
# ./spec/requests/user_pages_spec.rb:55:in `block (3 levels) in <top (required)>'
Just ran into the same problem and got it fixed.
It seems they are not referring to the sign_in method in sessions_helper.rb but the the sign_in method in spec/support/utilities.rb
In my case this helper method in utilities.rb had a different name, after renaming it everything worked fine :-)

500 error when calling webservice through rhosync/rhodes

I am trying to call a web service in rhosync application.rb, I see a 500 error response in rhosync console .. and 'server returned an error' in BB simulator .. :(
Some info about my setup -
I have created a rhodes app that connects to a rhosync app when user enters user name and password and clicks on "login". I am calling this webservice through "authenticate" method of application.rb of the rhosync application ..
def authenticate(username,password,session)
Rho::AsyncHttp.get(:url => 'http://mywebserviceURL',:callback => (url_for :action => :httpget_callback),:callback_param => "" )
end
UPDATE
Instead of http:async, I tried consuming a soap based webservice and it worked just fine .. here is code if anyone cones here in search of a sample.. in application.rb of rhosync app
require "soap/rpc/driver"
class Application < Rhosync::Base
class << self
def authenticate(username,password,session)
driver = SOAP::RPC::Driver.new('http://webserviceurl')
driver.add_method('authenticate', 'username', 'password')
ret=driver.authenticate(username,password)
if ret=="Success" then
true
else
false
end
end
end
Application.initializer(ROOT_PATH)
You can typically find the problem if you crank up your log. Edit rhoconfig.txt in your app
set these properties -
# Rhodes runtime properties
MinSeverity = 1
LogToOutput = 1
LogCategories = *
ExcludeLogCategories =
then try again and watch the terminal output. Feel free to post the log back and I'll take a look.
You also might want to echo out puts the mywebserviceURL if you're using that as a variable, I trust you just changed that for the post here. Can you access the webservice if you hit it with a browser?
require "soap/rpc/driver"
class Application < Rhosync::Base
class << self
def authenticate(username,password,session)
driver = SOAP::RPC::Driver.new('http://webserviceurl')
driver.add_method('authenticate', 'username', 'password')
ret=driver.authenticate(username,password)
if ret=="Success" then
true
else
false
end
end
end
Application.initializer(ROOT_PATH)
in this what is done in add_method and authenticate method and where it to be written.