owb’s project

policy kit

Polkit (formerly PolicyKit) is a component for controlling system-wide
privileges in Unix-like operating systems. It provides an organized
way for non-privileged processes to communicate with privileged ones.
Polkit allows a level of control of centralized system policy. It is
developed and maintained by David Zeuthen from Red Hat and hosted by
the freedesktop.org project.

Can be used to give a certain user who runs the e.g. rails application required permissions

sinatra

Sinatra is a free and open source software web application library and
domain-specific language written in Ruby. It is an alternative to
other Ruby web application frameworks such as Ruby on Rails, Merb,
Nitro and Camping. It is dependent on the Rack web server interface.

Designed and developed by Blake Mizerany, Sinatra is small and
flexible. It does not follow the typical model–view–controller pattern
used in other frameworks, such as Ruby on Rails. Instead, Sinatra
focuses on “quickly creating web-applications in Ruby with minimal
effort.”

Can be used to build a light and easy to use webserver for client/server implementation

event machine

EventMachine is an event-driven I/O and lightweight concurrency
library for Ruby. It provides event-driven I/O using the Reactor
pattern, much like JBoss Netty, Apache MINA, Python’s Twisted,
Node.js, libevent and libev.

EventMachine is designed to simultaneously meet two key needs

Extremely high scalability, performance and stability for the most demanding production environments.
An API that eliminates the complexities of high-performance threaded network programming, allowing engineers to concentrate on
their application logic.

This unique combination makes EventMachine a premier choice for
designers of critical networked applications, including Web servers
and proxies, email and IM production systems,
authentication/authorization processors, and many more.

TODO

client/server

Implementing a Client/Server logic. Most difficult but fastest and ressource-saving option since the Client should be wirtten in plain C.

rails as root for syscalls

Easy but bad.

websocket

TODO:

padrino + puma == lightweight

Padrino is a ruby framework built upon the excellent Sinatra Web
Library. Sinatra is a DSL for creating simple web applications in Ruby
quickly and with minimal effort. This framework tries to make it as
fun and easy as possible to code more advanced web applications by
building upon the Sinatra philosophies and foundations.

Can be a good solution in combination with puma (smallest webserver)

chef for cluster provisioning

Chef is a configuration management tool written in Ruby and Erlang. It
uses a pure-Ruby, domain-specific language (DSL) for writing system
configuration “recipes”. Chef is used to streamline the task of
configuring & maintaining a company’s servers, and can integrate with
cloud-based platforms such as Rackspace, Amazon EC2, Google Cloud
Platform, OpenStack and Microsoft Azure to automatically provision and
configure new machines.

Use it with Vagrant to setup predefined settings in a big(or small) cluster of VMs
Could also be a good solution, huh?

Read More

mutlithreading in ruby

thread1 = Thread.new do
  sum=0
  1.upto(10) {|x| sum = sum + x}
  puts("The sum of the first 10 integers is #{sum}")
end

thread2 = Thread.new do
  product=1
  1.upto(10) {|x| product = product * x}
  puts("The product of the first 10 integers is #{product}")
end

thread1.join
thread2.join

While there is great power in multithreaded code, there is also a lot of danger. A
great way to introduce hard-to-find bugs into your programs is to allow two or more
threads to modify the same data structure at the same time. A good way to avoid this
and other race conditions and make your code thread safe is to use the Monitor class:

@monitor = Monitor.new
  @monitor.synchronize do
  # Only one thread at a time here...
end

Read More

jquery, morris, raphael => graphs

<link rel="stylesheet" href="http://cdn.oesmith.co.uk/morris-0.5.1.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.5.1.min.js"></script>


<div id="myfirstchart" style="height: 250px;"></div>

<script>
new Morris.Line({
// ID of the element in which to draw the chart.
element: 'myfirstchart',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: [
{ year: '2008', value: 20 },
{ year: '2009', value: 10 },
{ year: '2010', value: 5 },
{ year: '2011', value: 5 },
{ year: '2012', value: 20 }
],
// The name of the data record attribute that contains x-values.
xkey: 'year',
// A list of names of data record attributes that contain y-values.
ykeys: ['value'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value']
});
</script>

Read More

more lambda, more procs

p = Proc.new { |x| puts x*2 }
[1,2,3].each(&p)              

The ‘&’ tells ruby to turn the proc into a block

proc = Proc.new { puts "Hello World" }
proc.call                    

The body of the Proc object gets executed when called

lam = lambda { |x| puts x*2 }
[1,2,3].each(&lam)

lam = lambda { puts "Hello World" }
lam.call

blocks cant be called standalone while procs and lambdas can.

Also important to know is that both procs and lambdas are actually Procs

proc   # returns '#<Proc:0x007f96b1032d30@(irb):75>'
lam    # returns '<Proc:0x007f96b1b41938@(irb):76 (lambda)>'

Difference:

lam = lambda { |x| puts x }    # creates a lambda that takes 1 argument
lam.call(2)                    # prints out 2
lam.call                       # ArgumentError: wrong number of arguments (0 for 1)
lam.call(1,2,3)                # ArgumentError: wrong number of arguments (3 for 1)

whilst:

proc = Proc.new { |x| puts x } # creates a proc that takes 1 argument
proc.call(2)                   # prints out 2
proc.call                      # returns nil
proc.call(1,2,3)               # prints out 1 and forgets about the extra arguments

They also treat the return statement differently.. but i dont care ;/

one step further:

def calculation(a, b, operation)
  operation.call(a, b)
end

puts calculation(5, 6, lambda { |a, b| a + b }) # addition
puts calculation(5, 6, lambda { |a, b| a - b }) # subtraction

Read More

lambda procs and blocks

If you want to pass a code block to a existing method you can use Procs

Key here is the ampersand &.
If we have a Proc object in a
variable and we want to pass it to a method that is looking for a code block, we can
convert the Proc object back into a code block by sticking an ampersand in front of it:

class Report

  attr_reader :title, :text
  attr_accessor :formatter

  def initialize(&formatter)
    @title = 'Monthly Report'
    @text = [ 'Things are going', 'really, really well.' ]
    @formatter = formatter
  end

  def output_report
    @formatter.call( self )
  end
end

HTML_FORMATTER = lambda do |context|
  puts('<html>')
  puts(' <head>')
  puts("<title>#{context.title}</title>")
  puts(' </head>')
  puts(' <body>')
  context.text.each do |line|
  puts("<p>#{line}</p>")
end

puts(' </body>')
puts

With our new Proc -based formatters in hand, we are ready to create some
reports. Given that we have a Proc object and the Report constructor expects a code
block, we need to stick an ampersand in front of our Proc object when we create a
new Report instance:

report = Report.new &HTML_FORMATTER
report.output_report

Read More

yields and blocks

In Ruby, methods may receive a code block in order to perform arbitrary segments of code.

When a method expects a block, it invokes it by calling the yield function.

This comes in hand when you want to iterate over a list or provide an own algorithm

example

class Person 
  def initialize( name ) 
     @name = name
end

  def do_with_name 
     yield( @name ) 
  end 
end

When you now invoke the Person class and pass a block

person = Person.new("johnlol")
person.do_with_name do |name|
   puts "Hey, his name is #{name}"
end

This would results obviously in

Hey, his name is johnlol

another example:

palin = %w(Lagerregal Rotor Otto Rentner)
person = Person.new(palin)
person.do_with_name do |name|
   puts name.reverse
end

Palindromlol..

Read More

rspec receive counts

expect(...).to receive(...).once
expect(...).to receive(...).twice
expect(...).to receive(...).exactly(n).times
expect(...).to receive(...).at_least(:once)
expect(...).to receive(...).at_least(:twice)
expect(...).to receive(...).at_least(n).times
expect(...).to receive(...).at_most(:once)
expect(...).to receive(...).at_most(:twice)
expect(...).to receive(...).at_most(n).times

Read More