emporio armani,fake rolex for sale,rolex sea dweller,panerai,iwc,richard mille,replica watches,bell ross,a lange sohne,cheap replica watches,jaeger lecoultre,rolex explorer,corum,rolex milgauss,breguet,piaget,franck muller,chopard
Archives for category: Ruby

I have a collection of users who store their favorite stuff id’s in an array, like this:

2.3-head :212 > User.each{ |u| puts u.inspect }
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35c'), favorite_ids: [1, 3, 5]>
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35d'), favorite_ids: [7, 3, 5, 2, 8]>
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35e'), favorite_ids: [3, 2, 7]>
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35f'), favorite_ids: [1, 10]>

I want to find those users who have favorited items 2, 3 and 8, and get them sorted by number of matchings.

class User
  def self.find_matches(array)
      { "$match" => { favorite_ids: { "$in" => array } } },
      { "$unwind" => "$favorite_ids" },
      { "$match" => { favorite_ids: { "$in" => array } } },
      { "$group" => { _id: "$_id", match_count: { "$sum" =>1 } } },
      { "$sort" => { match_count: -1 } }


2.3-head :228 >   User.find_matches([2,3,8]).entries
 => [{"_id"=>BSON::ObjectId('587356c45973747b77f1a35d'), "match_count"=>3}, {"_id"=>BSON::ObjectId('587356c45973747b77f1a35e'), "match_count"=>2}, {"_id"=>BSON::ObjectId('587356c45973747b77f1a35c'), "match_count"=>1}]

Are you usually deploying your Ruby applications with Capistrano and want to run remote commands depending on the server IP?

A not very well documented trick is to interpolate the special string $CAPISTRANO:HOST$ on your command. For example:

server_ip = capture("echo $CAPISTRANO:HOST$").strip
run "I'm running on server #{server_ip}"

The problem comes when you’re deploying to multiple servers, like this:

role :app, '', ''

task :some_task, :role => :app do
  server_ip = capture("echo $CAPISTRANO:HOST$").strip
  run "I'm running on server #{server_ip}"

This will always print your app’s role first ip,

The solution is even less documented:

role :app, '', ''

task :some_task, :role => :app do
  find_servers_for_task(current_task).each do |current_server|
    run "I'm running on server #{current_server.host}"

You know that ruby hype which claims “no need to document things, simply read the code”… well, that’s the reason for blog posts like this one. A mess.

I’ve been trying to set up a Motion-JPEG streaming with ruby, for a webcam which uploads a picture per second to a server. I want new images to be served via streaming instead of making the browser call them every second via a javascript call.

I found many different solutions, some of them including EventMachine, which now is not necessary thanks to the new Sinatra 1.3 streaming feature.
The tricky part was related to the headers, the boundary, and the need to send the content type before each image.

For the test I first created a directory and stored some pictures inside

mkdir /tmp/images
# place some jpg pictures here

And the code. My little app looks like this:

# motion_stream.rb
require 'sinatra'
set :server, :thin

get '/' do |dir|
  boundary      = 'some_shit'
  source_dir    = '/tmp/images'

  headers \
    "Cache-Control" => "no-cache, private",
    "Pragma"        => "no-cache",
    "Content-type"  => "multipart/x-mixed-replace; boundary=#{boundary}"

  stream(:keep_open) do |out|
    while true
      file        = random_file(source_dir) # see also latest_file() below
      content     = File.open("#{source_dir}/#{file}", 'rb') { |f| f.read }

      out << "Content-type: image/jpeg\n\n"
      out << content
      out << "\n\n--#{boundary}\n\n"

      sleep 1

## get a random image from a directory
def random_file(dir)
  files = Dir.entries(dir).collect { |file| file }
  files -= ['.', '..']

## ... or get the newest image
## In this case I'm not taking the latest file
## uploaded by the camera, but the previous one.
## This is to avoid grabbing a currently uploading
## file, which may be shown as corrupt or incomplete.
def latest_file(dir)
  files = Dir.entries(dir).collect { |file| file }.sort { |file2,file1| File.mtime(dir+file1) <=> File.mtime(dir+file2) }
  files -= ['.', '..']

Then simply create a Gemfile including Sinatra and Thin, as WebRick is not evented and does not support this kind of stream.

# Gemfile
source :rubygems

gem 'sinatra'
gem 'thin'

And that’s all. Run the app and you’re done.

ruby motion_stream.rb

Just visit http://localhost:4567/ with your browser :-)

I’m translating my usual sysadmining shellscripts into ruby for fun and practice. One thing I hate from Ruby (maybe the only or the most important one) is its documentation. It is zero browseable and friendly, and makes you waste half of your time trying to find on google which module/class does what you need.

One thing I’ve been trying to find is how to copy a file (OMG!).
Can it be so difficult?? … please, try to find it on File class reference.

You have to figure out that exists a module called FileUtils or its old bro (ruby 1.8.6) ftools. Using them you can easily copy, move, rename and make basic file management.
What I find more surprising is that the File class reference does not mention any of them (Uh!?).

Once I found it the job was easy (using ftools as I’m still on Ruby 1.8.7):

require 'ftools'
File.copy source_file, target_file
Theme: Wu Wei by Jeff Ngan