Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Oliver Haag www.ohcon.de

« Newer Snippets
Older Snippets »
6 total  XML / RSS feed 

create object fom underscore_syntax

// camelize - from underscore_syntax to Uppercas/ClassSyntax
// constantize - create instance
// (params[:property]) -- read data from web-form


    @property = property_type.camelize.constantize.new(params[:property])

get url / uri in *.rhtml file

// find out the current url / uri in *.rhtml file
// is quite simple with the request object
<% page = request.request_uri %>
page: <%= page %>


// or when different urls mean the same page
<% page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
page: <%= page %>

pluralize, depending on count

// from Agile WebDevelopment with rails
// slightly extendet

def pluralize(count, noun, text)
  case count
  when 0: "There are no #{noun.pluralize} #{text}"
  when 1: "There is one #{noun} #{text}"
  else: "There are #{count} #{noun.pluralize} #{text}"
  end
end

count characters

// description of your code here

class Counter
  
def initialize()
  @characters = Hash.new(0)
end

def read()
  @text = IO.read("text.txt")
end

def count_chars
  @text.each_byte do |ch|
  @characters[ch] +=1
  end
end

def report  
  @characters.each do |key, value|
    puts "#{key.chr} (#{key}) occurs #{value} times"
  end  
end

end

send mail with :login

// with ":login" you authenticate with user and password
// more info: Library Net:SMTP [Dave Thomas, The Pragmatc Programmer]
  # send email with :login
  def send_email(subject, message)
   
    from='sender@sender_address.de'
    from_alias='the sender'
    to='recipient@recip_address.de'
    to_alias='the recipient'
    smtp_host='smtp.1und1.com'
    port=25   # default port is 25
    user='username'
    password='its_a_secret'

    myMessage = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}

#{message}
END_OF_MESSAGE

    Net::SMTP.start(smtphost, port, from, user, password, :login) do |smtp|
      smtp.send_message myMessage, from, to
    end
  end

Simple Test if Site is online

// test if sites are online by title validation
// if the title can change, test can validate occurance of a phrase instead

require 'rwebunit'  # ruby web test based on watir

# test if sites are online by title validation
# usage: to run this test without visible ie use the -b option
# C:\ruby\workspace\ruject1>ruby rwu_site_checker.rb -b

class RwuSiteChecker < RWebUnit::WebTestCase

  # hash with url and title
  @@sites = {
    "http://www.domain_number_one.de" => "title number one",
    "http://www.seccond_domain.org" => "seccond title",
    "http://www.yet_another_domain.com" => "yet another title"
  }
  
  # test for titles
  def test_titles()
    log = "testing title \n"
    @@sites.each { |url, title|
      getTestContext().base_url=url
      beginAt("/")
      assertTitleEquals(title)
      # to check for phrase: assertTextPresent(phrase) 
   
      log += url + " ok \n"
    }
    puts log
  end
  
end
« Newer Snippets
Older Snippets »
6 total  XML / RSS feed