super_p && web

my little piece of "work memory"

0 notes &

Get facebook user registration date

If you need to get facebook registration date, you can get the first post created date.

But, Facebook api graph don’t return timeline posts with sorted by asc or desc. And it is very sadly.

We can return registration year by simple algorithm:

In cycle of years array check if exists any post before current year by fql query.

SELECT created_time FROM stream WHERE source_id = me() AND created_time < {date} limit 1

If you use ruby, you can use my lib for this - https://github.com/galetahub/social_profile

Filed under fql facebook ruby facebook api

0 notes &

Rails file upload implementation with jQuery-File-Upload

HTML5 File uploader for rails

This gem use https://github.com/blueimp/jQuery-File-Upload for upload files.

Preview:

Uploader in use

Install

In Gemfile:

gem “rails-uploader”

In routes:

mount Uploader::Engine => '/uploader'

Usage

Architecture to store uploaded files (cancan integration):

class Asset < ActiveRecord::Base
  include Uploader::Asset

  def uploader_create(params, request = nil)
    ability = Ability.new(request.env['warden'].user)

    if ability.can? :create, self
      self.user = request.env['warden'].user
      super
    else
      errors.add(:id, :access_denied)
    end
  end

  def uploader_destroy(params, request = nil)
    ability = Ability.new(request.env['warden'].user)

    if ability.can? :delete, self
      super
    else
      errors.add(:id, :access_denied)
    end
  end
end

class Picture < Asset
  mount_uploader :data, PictureUploader

  validates_integrity_of :data
  validates_filesize_of :data, :maximum => 2.megabytes.to_i
end

For example user has one picture:

class User < ActiveRecord::Base
  has_one :picture, :as => :assetable, :dependent => :destroy

  fileuploads :picture
end

Find asset by foreign key or guid:

@user.fileupload_asset(:picture)

Include assets

Javascripts:

//= require uploader/application

Stylesheets:

*= require uploader/application  

Views

<%= uploader_field_tag :article, :photo %>

or FormBuilder:

<%= form.uploader_field :photo %>

Formtastic

<%= f.input :picture, :as => :uploader %>

SimpleForm

<%= f.input :picture, :as => :uploader %>

View on github

Filed under rails ruby uploader jquery file-upload rails-uploader

16 notes &

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

4 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