SlideShare uma empresa Scribd logo
1 de 45
Rails Summer of Code
                                     Week 6




Richard Schneeman - @ThinkBohemian
Rails - Week 6
           • Email in Rails
           • Background Tasks
           • modules
           • Observers & Callbacks
           • Internationalization & Localization
            • I18n & L10n

Richard Schneeman - @ThinkBohemian
Email in Rails
    • What is Email?
    • Why Use Email?
    • How does Rails use email?




Richard Schneeman - @ThinkBohemian
What is Email
           • Communications medium defined by RFC standards
              ✦   RFC = Request for Comments
              ✦   Comprised of Header & Body
                  ✦
                      Header (to, from, reply-to, content-type, subject, cc, etc.)
                  ✦
                      Body (message and attachments)




Richard Schneeman - @ThinkBohemian
Email- Content Types
  • Defines How the Mail User Agent (MUA) Interprets Body
    ✦       Text/HTML
    ✦       Text/Plain
    ✦       Multipart/Related (Example: Inline Pictures)
    ✦       Multipart/Alternative
        ✦
            Send Text/HTML with Text/Plain as backup
        ✦
            Add Attachments


Richard Schneeman - @ThinkBohemian
Why use Email with
                    Rails?
   •   Status Updates ( Twitter, Facebook, Etc. )

   •   Direct to Consumer Marketing source

   •   Added functionality (lost passwords, etc.)

   •   Send documents

   •   Everyone has it, and

       •   Everyone can use it



Richard Schneeman - @ThinkBohemian
How Does RoR Send
                 Email?
  • Low Volume                                  Mail Server   Operating System


    • use Gmail
                                       Your
                                     Computer                       MTA



  • High Volume
                                                                    Postfix
                                                                    SMTP
                                                                (Sends Emails)
                                                                       d


    • use a re-mailer                             Their
                                                Computer          Courier
                                                                 IMAP/POP

    • Build your own


Richard Schneeman - @ThinkBohemian
How Does RoR Send
                 Email?
   •   ActionMailer

       •   Mail Gem

                         rails generate mailer Notifier



                              /app/mailers/notifier.rb



Richard Schneeman - @ThinkBohemian
Email With Rails




    Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
Email With Rails

                                     • Default Mailer Settings

                                     • In-Line Attachments

                                     • mail( ) method

    Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
Email With Rails

                                     • Default Mailer Settings

                                     • In-Line Attachments

                                     • mail( ) method

    Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
Email With Rails

                                     • Default Mailer Settings

                                     • In-Line Attachments

                                     • mail( ) method

    Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
Email With Rails
  ✦ Using   Gmail
                                     config/environments/development.rb
    ✦   Use Port 587
    ✦   Gmail will throttle large number
        of email requests
    ✦   Close to real life conditions
    ✦   Relatively Easy
    ✦   Don’t use with automated


    Notifier.signup_notification(“foo@example.com”).deliver
Richard Schneeman - @ThinkBohemian
Email re-cap
    ✦   Receiving Email much harder
        ✦   Also less common
    ✦   Test your Mailer using an Interceptor
    ✦   use a re-mailer in production
    ✦   real life application: http://whyspam.me




Richard Schneeman - @ThinkBohemian
Background Tasks
    • What is a background task?
     • Why use one?
    • Where do i put my task in rails?
    • How do i keep my task alive?



Richard Schneeman - @ThinkBohemian
Background Task
  • What is a background task?
   • Any server process not initiated by http request
   • Commonly run for long periods of time
   • Do not block or stop your application
    • Clean up server, or application
    • Generate reports
    • Much more
Richard Schneeman - @ThinkBohemian
Background Task
  • rake tasks
   • organize code in “lib/tasks”
     • run with:
                      rake <command> RAILS_ENV=<environment>




Richard Schneeman - @ThinkBohemian
Background Task
  • rake tasks
   • organize code in “lib/tasks”
     • run with:
                      rake <command> RAILS_ENV=<environment>




Richard Schneeman - @ThinkBohemian
Background Task
  • Example
   • cleanup.rake
       namespace :cleanup do
         desc "clean out Tickets over 30 days old"
          task :old_tickets => :environment do
            tickets = Ticket.find(:all, :conditions => ["created_at < ?", 30.days.ago ], :limit => 5000)
            tickets.each do |ticket|
              ticket.delete
            end
       end


                                       rake cleanup:old_tickets



Richard Schneeman - @ThinkBohemian
Background Task
  • What if i don’t want to execute from command line?
   • run task with a automation program
    • Cron
    • Monit
    • God


Richard Schneeman - @ThinkBohemian
Cron
  • Very reliable unix time scheduler
  • Built into the OS
   • Executes command line calls
   • Smallest interval is 1 minute
   • Requires full paths


Richard Schneeman - @ThinkBohemian
Cron
    • Awesome, but verbose


    • Use Whenever gem instead



Richard Schneeman - @ThinkBohemian
Monit
    • Not installed on OS by default
     • Monitors and Executes (cron only executes)
    • Extra functionality - Sysadmin emails etc...




Richard Schneeman - @ThinkBohemian
God
    • Written in ruby
    • Very configurable
    • can be memory
        intensive in some
        applications



                sudo gem install god


Richard Schneeman - @ThinkBohemian
Background Processes
    • More Options
     • Workling/Starling
     • Backgroundrb




Richard Schneeman - @ThinkBohemian
Modules (ruby)
  • Add “Mixins” to your code
  • Keep code seperate with different namespaces
  • put them in your rails project under /lib




Richard Schneeman - @ThinkBohemian
Modules (ruby)
 • Example Mixin:
  • include adds instance methods
   class Dog                                module AntiCheating
     include AntiCheating                     def drug_test
   end                                          ...
                                              end
                                            end

                          puppy = Dog.new
                          puppy.drug_test
                          >> Passed
Richard Schneeman - @ThinkBohemian
Modules (ruby)
 • Example Mixin 2:
   class Dog                              module AntiCheating
     include AntiCheating                   def self.cleanup( level)
   end                                        ...
                                            end
                                          end

                           dirtyDog = Dog.new
                           dirtyDog.cleanup
                           >> No Method Error
Richard Schneeman - @ThinkBohemian
Modules (ruby)
 • Example Mixin 2:
                           module AntiCheating
                             def self.cleanup( level)
                               ...
                             end
                           end

                           AntiCheating.cleanup(10)
                           >>Very Clean



Richard Schneeman - @ThinkBohemian
Modules (ruby)
 • Example Mixin 2:
  • extend adds all module methods
   class Dog                               module AntiCheating
     extend AntiCheating                     def self.cleanup( level)
   end                                         ...
                                             end
                                           end

                          dirtyDog = Dog.new
                          dirtyDog.cleanup(2)
                          >> Kinda Clean
Richard Schneeman - @ThinkBohemian
Modules (ruby)
 • Extra Credit:
  • class << self
    class Dog                                    class Dog
      class << self                                  def self.sniff
        def sniff                                      ...
          ...                             =          end
        end                                      end
      end
    end

                                     Dog.sniff

Richard Schneeman - @ThinkBohemian
Modules (ruby)
 • Extra Credit:
  • class << self
   class Dog                               module AntiCheating
     class << self                           def self.cleanup( level)
       include AntiCheating                    ...
     end                                     end
   end                                     end


                           dirtyDog = Dog.new
                           dirtyDog.cleanup(2)
                           >> Kinda Clean
Richard Schneeman - @ThinkBohemian
Modules (ruby)
 • Example Namespace
   module FastDogs                   module SlowDogs
     class Dog                         class Dog
       ...                               ...
     end                               end
   end                               end

    lassie = FastDogs::Dog.new       droopy = SlowDogs::Dog.new



Richard Schneeman - @ThinkBohemian
Callbacks and
                         Observers
  • Callbacks
   • Non-polling event based method
   • hooks into lifecycle of Active Record object
  • Observers
   • Implement trigger behavior for class outside of the
        original class



Richard Schneeman - @ThinkBohemian
Callbacks
  • Polling
   • “Are We there Yet”
  • Callback
   • “I’ll tell you when we’re there”



Richard Schneeman - @ThinkBohemian
Callbacks in Rails
                                     save
    • Lifecycle Hooks =>             (-) valid
                                     (1) before_validation
                                     (2) before_validation_on_create
                                     (-) validate
                                     (-) validate_on_create
                                     (3) after_validation
                                     (4) after_validation_on_create
                                     (5) before_save
                                     (6) before_create
                                     (-) create
                                     (7) after_create
                                     (8) after_save

Richard Schneeman - @ThinkBohemian
Callbacks in Rails
    • Example
     • before_destroy
               class Topic < ActiveRecord::Base
                 before_destroy :delete_parents

                    def delete_parents
                      self.class.delete_all "parent_id = #{id}"
                    end
                 end
               end

Richard Schneeman - @ThinkBohemian
Observers
    • Add callback functionality without polluting the
        model




    • Will run after every new user instance is created
    • Keeps your code clean(er)
Richard Schneeman - @ThinkBohemian
i18n & L10n




                                Source: @mattt
Richard Schneeman - @ThinkBohemian
i18n & L10n




               Source: @mattt

Richard Schneeman - @ThinkBohemian
language configuration
                                     Source: @mattt




               Source: @mattt

Richard Schneeman - @ThinkBohemian
Multiple Language Files
                 fr.yml




                                     ja.yml




                                        Source: @mattt
Richard Schneeman - @ThinkBohemian
It Worked!




                                     Source: @mattt
Richard Schneeman - @ThinkBohemian
What about L10n




Richard Schneeman - @ThinkBohemian
Questions?
                       http://guides.rubyonrails.org
                        http://stackoverflow.com
                           http://peepcode.com


Richard Schneeman - @ThinkBohemian

Mais conteúdo relacionado

Semelhante a Rails Email Background Tasks Modules

Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010Yahoo Developer Network
 
Chirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterJohn Adams
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Reuven Lerner
 
Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Reuven Lerner
 
Scaling habits of ASP.NET
Scaling habits of ASP.NETScaling habits of ASP.NET
Scaling habits of ASP.NETDavid Giard
 
PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)
PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)
PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)Adam Charnock
 
Fixing twitter
Fixing twitterFixing twitter
Fixing twitterRoger Xia
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...smallerror
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...xlight
 
HadoopSummit_2010_big dataspamchallange_hadoopsummit2010
HadoopSummit_2010_big dataspamchallange_hadoopsummit2010HadoopSummit_2010_big dataspamchallange_hadoopsummit2010
HadoopSummit_2010_big dataspamchallange_hadoopsummit2010Yahoo Developer Network
 
Attack All the Layers - What's Working in Penetration Testing
Attack All the Layers - What's Working in Penetration TestingAttack All the Layers - What's Working in Penetration Testing
Attack All the Layers - What's Working in Penetration TestingNetSPI
 
Attack All The Layers - What's Working in Penetration Testing
Attack All The Layers - What's Working in Penetration TestingAttack All The Layers - What's Working in Penetration Testing
Attack All The Layers - What's Working in Penetration TestingNetSPI
 
Attack All the Layers: What's Working during Pentests (OWASP NYC)
Attack All the Layers: What's Working during Pentests (OWASP NYC)Attack All the Layers: What's Working during Pentests (OWASP NYC)
Attack All the Layers: What's Working during Pentests (OWASP NYC)Scott Sutherland
 
Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014
Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014
Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014Jon Milsom
 
John adams talk cloudy
John adams   talk cloudyJohn adams   talk cloudy
John adams talk cloudyJohn Adams
 
Large-Scale ETL Data Flows With Data Pipeline and Dataduct
Large-Scale ETL Data Flows With Data Pipeline and DataductLarge-Scale ETL Data Flows With Data Pipeline and Dataduct
Large-Scale ETL Data Flows With Data Pipeline and DataductSourabh Bajaj
 

Semelhante a Rails Email Background Tasks Modules (20)

UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4 UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4
 
UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1
 
Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010Winning the Big Data SPAM Challenge__HadoopSummit2010
Winning the Big Data SPAM Challenge__HadoopSummit2010
 
Chirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling Twitter
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
 
rspamd-fosdem
rspamd-fosdemrspamd-fosdem
rspamd-fosdem
 
Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011
 
Scaling habits of ASP.NET
Scaling habits of ASP.NETScaling habits of ASP.NET
Scaling habits of ASP.NET
 
PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)
PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)
PlayNice.ly: Using Redis to store all our data, hahaha (Redis London Meetup)
 
Fixing twitter
Fixing twitterFixing twitter
Fixing twitter
 
Fixing_Twitter
Fixing_TwitterFixing_Twitter
Fixing_Twitter
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
 
HadoopSummit_2010_big dataspamchallange_hadoopsummit2010
HadoopSummit_2010_big dataspamchallange_hadoopsummit2010HadoopSummit_2010_big dataspamchallange_hadoopsummit2010
HadoopSummit_2010_big dataspamchallange_hadoopsummit2010
 
Attack All the Layers - What's Working in Penetration Testing
Attack All the Layers - What's Working in Penetration TestingAttack All the Layers - What's Working in Penetration Testing
Attack All the Layers - What's Working in Penetration Testing
 
Attack All The Layers - What's Working in Penetration Testing
Attack All The Layers - What's Working in Penetration TestingAttack All The Layers - What's Working in Penetration Testing
Attack All The Layers - What's Working in Penetration Testing
 
Attack All the Layers: What's Working during Pentests (OWASP NYC)
Attack All the Layers: What's Working during Pentests (OWASP NYC)Attack All the Layers: What's Working during Pentests (OWASP NYC)
Attack All the Layers: What's Working during Pentests (OWASP NYC)
 
Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014
Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014
Startup DevOps - Jon Milsom Pitchero - Leeds DevOps - August 2014
 
John adams talk cloudy
John adams   talk cloudyJohn adams   talk cloudy
John adams talk cloudy
 
Large-Scale ETL Data Flows With Data Pipeline and Dataduct
Large-Scale ETL Data Flows With Data Pipeline and DataductLarge-Scale ETL Data Flows With Data Pipeline and Dataduct
Large-Scale ETL Data Flows With Data Pipeline and Dataduct
 

Último

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 

Último (20)

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 

Rails Email Background Tasks Modules

  • 1. Rails Summer of Code Week 6 Richard Schneeman - @ThinkBohemian
  • 2. Rails - Week 6 • Email in Rails • Background Tasks • modules • Observers & Callbacks • Internationalization & Localization • I18n & L10n Richard Schneeman - @ThinkBohemian
  • 3. Email in Rails • What is Email? • Why Use Email? • How does Rails use email? Richard Schneeman - @ThinkBohemian
  • 4. What is Email • Communications medium defined by RFC standards ✦ RFC = Request for Comments ✦ Comprised of Header & Body ✦ Header (to, from, reply-to, content-type, subject, cc, etc.) ✦ Body (message and attachments) Richard Schneeman - @ThinkBohemian
  • 5. Email- Content Types • Defines How the Mail User Agent (MUA) Interprets Body ✦ Text/HTML ✦ Text/Plain ✦ Multipart/Related (Example: Inline Pictures) ✦ Multipart/Alternative ✦ Send Text/HTML with Text/Plain as backup ✦ Add Attachments Richard Schneeman - @ThinkBohemian
  • 6. Why use Email with Rails? • Status Updates ( Twitter, Facebook, Etc. ) • Direct to Consumer Marketing source • Added functionality (lost passwords, etc.) • Send documents • Everyone has it, and • Everyone can use it Richard Schneeman - @ThinkBohemian
  • 7. How Does RoR Send Email? • Low Volume Mail Server Operating System • use Gmail Your Computer MTA • High Volume Postfix SMTP (Sends Emails) d • use a re-mailer Their Computer Courier IMAP/POP • Build your own Richard Schneeman - @ThinkBohemian
  • 8. How Does RoR Send Email? • ActionMailer • Mail Gem rails generate mailer Notifier /app/mailers/notifier.rb Richard Schneeman - @ThinkBohemian
  • 9. Email With Rails Notifier.signup_notification(“foo@example.com”).deliver Richard Schneeman - @ThinkBohemian
  • 10. Email With Rails • Default Mailer Settings • In-Line Attachments • mail( ) method Notifier.signup_notification(“foo@example.com”).deliver Richard Schneeman - @ThinkBohemian
  • 11. Email With Rails • Default Mailer Settings • In-Line Attachments • mail( ) method Notifier.signup_notification(“foo@example.com”).deliver Richard Schneeman - @ThinkBohemian
  • 12. Email With Rails • Default Mailer Settings • In-Line Attachments • mail( ) method Notifier.signup_notification(“foo@example.com”).deliver Richard Schneeman - @ThinkBohemian
  • 13. Email With Rails ✦ Using Gmail config/environments/development.rb ✦ Use Port 587 ✦ Gmail will throttle large number of email requests ✦ Close to real life conditions ✦ Relatively Easy ✦ Don’t use with automated Notifier.signup_notification(“foo@example.com”).deliver Richard Schneeman - @ThinkBohemian
  • 14. Email re-cap ✦ Receiving Email much harder ✦ Also less common ✦ Test your Mailer using an Interceptor ✦ use a re-mailer in production ✦ real life application: http://whyspam.me Richard Schneeman - @ThinkBohemian
  • 15. Background Tasks • What is a background task? • Why use one? • Where do i put my task in rails? • How do i keep my task alive? Richard Schneeman - @ThinkBohemian
  • 16. Background Task • What is a background task? • Any server process not initiated by http request • Commonly run for long periods of time • Do not block or stop your application • Clean up server, or application • Generate reports • Much more Richard Schneeman - @ThinkBohemian
  • 17. Background Task • rake tasks • organize code in “lib/tasks” • run with: rake <command> RAILS_ENV=<environment> Richard Schneeman - @ThinkBohemian
  • 18. Background Task • rake tasks • organize code in “lib/tasks” • run with: rake <command> RAILS_ENV=<environment> Richard Schneeman - @ThinkBohemian
  • 19. Background Task • Example • cleanup.rake namespace :cleanup do desc "clean out Tickets over 30 days old" task :old_tickets => :environment do tickets = Ticket.find(:all, :conditions => ["created_at < ?", 30.days.ago ], :limit => 5000) tickets.each do |ticket| ticket.delete end end rake cleanup:old_tickets Richard Schneeman - @ThinkBohemian
  • 20. Background Task • What if i don’t want to execute from command line? • run task with a automation program • Cron • Monit • God Richard Schneeman - @ThinkBohemian
  • 21. Cron • Very reliable unix time scheduler • Built into the OS • Executes command line calls • Smallest interval is 1 minute • Requires full paths Richard Schneeman - @ThinkBohemian
  • 22. Cron • Awesome, but verbose • Use Whenever gem instead Richard Schneeman - @ThinkBohemian
  • 23. Monit • Not installed on OS by default • Monitors and Executes (cron only executes) • Extra functionality - Sysadmin emails etc... Richard Schneeman - @ThinkBohemian
  • 24. God • Written in ruby • Very configurable • can be memory intensive in some applications sudo gem install god Richard Schneeman - @ThinkBohemian
  • 25. Background Processes • More Options • Workling/Starling • Backgroundrb Richard Schneeman - @ThinkBohemian
  • 26. Modules (ruby) • Add “Mixins” to your code • Keep code seperate with different namespaces • put them in your rails project under /lib Richard Schneeman - @ThinkBohemian
  • 27. Modules (ruby) • Example Mixin: • include adds instance methods class Dog module AntiCheating include AntiCheating def drug_test end ... end end puppy = Dog.new puppy.drug_test >> Passed Richard Schneeman - @ThinkBohemian
  • 28. Modules (ruby) • Example Mixin 2: class Dog module AntiCheating include AntiCheating def self.cleanup( level) end ... end end dirtyDog = Dog.new dirtyDog.cleanup >> No Method Error Richard Schneeman - @ThinkBohemian
  • 29. Modules (ruby) • Example Mixin 2: module AntiCheating def self.cleanup( level) ... end end AntiCheating.cleanup(10) >>Very Clean Richard Schneeman - @ThinkBohemian
  • 30. Modules (ruby) • Example Mixin 2: • extend adds all module methods class Dog module AntiCheating extend AntiCheating def self.cleanup( level) end ... end end dirtyDog = Dog.new dirtyDog.cleanup(2) >> Kinda Clean Richard Schneeman - @ThinkBohemian
  • 31. Modules (ruby) • Extra Credit: • class << self class Dog class Dog class << self def self.sniff def sniff ... ... = end end end end end Dog.sniff Richard Schneeman - @ThinkBohemian
  • 32. Modules (ruby) • Extra Credit: • class << self class Dog module AntiCheating class << self def self.cleanup( level) include AntiCheating ... end end end end dirtyDog = Dog.new dirtyDog.cleanup(2) >> Kinda Clean Richard Schneeman - @ThinkBohemian
  • 33. Modules (ruby) • Example Namespace module FastDogs module SlowDogs class Dog class Dog ... ... end end end end lassie = FastDogs::Dog.new droopy = SlowDogs::Dog.new Richard Schneeman - @ThinkBohemian
  • 34. Callbacks and Observers • Callbacks • Non-polling event based method • hooks into lifecycle of Active Record object • Observers • Implement trigger behavior for class outside of the original class Richard Schneeman - @ThinkBohemian
  • 35. Callbacks • Polling • “Are We there Yet” • Callback • “I’ll tell you when we’re there” Richard Schneeman - @ThinkBohemian
  • 36. Callbacks in Rails save • Lifecycle Hooks => (-) valid (1) before_validation (2) before_validation_on_create (-) validate (-) validate_on_create (3) after_validation (4) after_validation_on_create (5) before_save (6) before_create (-) create (7) after_create (8) after_save Richard Schneeman - @ThinkBohemian
  • 37. Callbacks in Rails • Example • before_destroy class Topic < ActiveRecord::Base before_destroy :delete_parents def delete_parents self.class.delete_all "parent_id = #{id}" end end end Richard Schneeman - @ThinkBohemian
  • 38. Observers • Add callback functionality without polluting the model • Will run after every new user instance is created • Keeps your code clean(er) Richard Schneeman - @ThinkBohemian
  • 39. i18n & L10n Source: @mattt Richard Schneeman - @ThinkBohemian
  • 40. i18n & L10n Source: @mattt Richard Schneeman - @ThinkBohemian
  • 41. language configuration Source: @mattt Source: @mattt Richard Schneeman - @ThinkBohemian
  • 42. Multiple Language Files fr.yml ja.yml Source: @mattt Richard Schneeman - @ThinkBohemian
  • 43. It Worked! Source: @mattt Richard Schneeman - @ThinkBohemian
  • 44. What about L10n Richard Schneeman - @ThinkBohemian
  • 45. Questions? http://guides.rubyonrails.org http://stackoverflow.com http://peepcode.com Richard Schneeman - @ThinkBohemian

Notas do Editor