super_p && web

my little piece of "work memory"

1 note &

Ruby wrapper for startmobile sms service

This gem provide the easy way to send and check a single sms with startmobile.com.ua service.

Install

gem 'startmobile_sms'

Configure

StartmobileSms.setup do |config|
  config.login = 'login'
  config.password = 'password'
  config.out_number = 'out_number'
end

Usage

# Send single sms (out_number is optional)
sms_id = StartmobileSms.send(:phone => '30971234567', :text => 'Hello!', :out_number => 'service_name')

# Check sms delivery status
StartmobileSms.check(sms_id)

# Or use instance object
sms = StartmobileSms::Message.new(:phone => '30971234567', :text => 'Hello!')

sms.phone # 30971234567
sms.text # Hello!
sms.send # send sms to server
sms.id # response id

sms.status # check sms status (Accepted, Enroute, Delivered, Expired, Deleted, Undeliverable, Rejected

Filed under ruby on rails ruby sms startmobile git

0 notes &

Friendly_id. Don’t change slug on update

If you don’t want to regenerate you slug when model change friendly_id field, use this hook:

# config/initializers/add_static_to_friendly_id.rb
module FriendlyId
  module Static
    def should_generate_new_friendly_id?
      new_record?
    end
  end
end

# your model
class MyModel < ActiveRecord::Base
  extend FriendlyId
  friendly_id :something, :use => [:slugged, :static]
end

0 notes &

Select album photos likes from Facebook by FQL query

I have Facebook contest album with many photos. And I need to display rating of this photos by user likes.

This FQL query get a list of all likes (user_ids) by all photos:

SELECT user_id, object_id FROM like WHERE object_id IN (SELECT object_id FROM photo WHERE aid=’139307129471375_51887’ limit 5000) limit 100000

Then you can order results by object_id and sort by object_id count. 

NOTE! Album ID (‘139307129471375_51887’) - is not facebook graph object id. 139307129471375 - is page id, 51887 - element id for this page.

For my example I use Ruby and fb_graph gem:

query = FbGraph::Query.new(“SELECT user_id, object_id FROM like WHERE object_id IN (SELECT object_id FROM photo WHERE aid=’139307129471375_51887’ limit 5000) limit 100000 “).fetch(SiteConfig.fb_token)

SiteConfig.fb_token - is my facebook app access_token with permissions user_photos (get it easy by using Graph API Explorer)

Then I order and sort my array:

photos = query.inject(Hash.new(0)){ |h, e| h[e[“object_id”]] += 1 ; h }.sort{|a,b| b[1]<=>a[1]}

photos.each {|p| puts “Object id - #{p[0]}, likes count - #{p[1]}”}

Filed under facebook ruby ruby on rails fb_graph fql

0 notes &

Cyrillic length problem

Ruby 1.8.7 (Ruby Enterprise Edition):

“string”.length # => 6

“строка”.length # => 12


Solution:

“string”.split(//).size # => 6

“строка”.split(//).size # => 6