Archive for the ‘Ruby on Rails’ Category

5 Popular Websites Built by Ruby on Rails

Advertisements

Cara Menggunakan Appschef API dengan w_platform Gem.

Tutorial

Appschef.com adalah SAAS dari Indonesia yang dibuat menggunakan Ruby on Rails. Berikut ini langkah – langkah untuk menggunakan Appschef API dari Aplikasi Ruby on Rails 3.2 dengan gem w_platform:

  • Signup dan Daftarkan produk / aplikasi Anda di http://appschef.com
  • Login ke Apsschef.com dan dapatkan API_KEY untuk produk / aplikasi yang anda daftarkan
  • Edit Gemfile aplikasi Rails anda dan tambahkan code berikut:
gem 'w_platform', :git => 'git@github.com:febyartandi/w_platform.git'
  • Di terminal run:
$ bundle install
$ rails generate w_initializer install
  • Kemudian Edit file config/w_platform.yml sesuai dengan konfigurasi aplikasi Anda, berikut contohnya:
common: &default_settings
w_api_url_users: /api/users/
w_api_url_get_active_session: /api/login
#nama aplikasi / produk Anda
app_name: w_factory_operations
development:
<< *default_settings
# link ke website appschef
appschef_url: https://appschef.com
# ganti dengan Api key produk / aplikasi Anda
api_key: xxxxxxxxxxxxxxxxxxxxxxxx
# link ke website Anda
app_url: http://localhost:3000
# protokol yang digunakan ( pilihan bisa http atau https )
protocol: https
#nama controller dan action yang dijadikan root (“/”) di Aplikasi Anda
root_controller: home
root_action: index
  • Definisikan “key” untuk setiap fitur yang ada di aplikasi Anda.

Untuk menentukan fitur apa saja yang ada di Aplikasi Anda, Appschef mengharuskan anda untuk membuat daftar fitur dan “key” sebagai representatifnya. List itu harus anda buat dalam excell dan upload ke Appschef.com.

Kemdian setelah itu anda definisikan fitur tersebut di aplikasi anda dengan edit file config/w_features.yml, berikut contohnya:

home:
index: wfo_home_index
projects:
new: wfo_project_new
show: wfo_project_show

Keterangan:

home, Projects => Nama Controller

index, new, show => Nama Action

wfo_home_index  => Contoh  ‘key’ untuk fitur “home/index”

  • Mount Routes WPlatform dengan edit config/routes.rb dan tambahkan code berikut:
mount WPlatform::Engine => “/”, :as => “w_platform”
  • Include WPlatformHelper dan tambahkan before filter untuk check access user ke fitur yang ada di Aplikasi Anda. Sebagai contoh Anda dapat Edit application_controller.rb dan tambahkan 2 line berikut:
include WPlatformHelper
before_filter :check_user_access
  • Langkah terakhir, Run server aplikasi Anda, kemdian login dari server appschef dan pilih untuk membuka Aplikasi Anda.

Tips

  • Anda dapat mendefinisikan controllers dan actions yang tidak memerlukan check authentikasi dengan cara berikut ini:
before_filter :check_user_access_but_skip_some_actions
protected
def check_user_access_but_skip_some_actions
check_user_access([{:controller_name => “about”, :action_name => “index”}])
end
  • Anda dapat menggunakan helper yang tersedia, yaitu:

current_user_platform =>return has semua result data yang diberikan oleh Appschef server

current_company => return hash data company

current_products => return has data products

current_features => return hash fitur – fitur yang boleh diakases user

  • Menambahkan link logout
<%= link_to “Logout”, w_platform.new_authentication_session_cleaner_path, :confirm => “Anda yakin untuk logout ? ?”%>

Bug Report

Jika Anda menemukan bug dalam tutorial ini atau gem w_platform, harap hubungi saya melalui email ke: feby.a@wgs.co.id

Terima kasih.

ROR Application for solved Mars Rover Problem

Download the app here:
https://github.com/febyartandi/mars-rover-ruby-with-webview

MARS HOVER PROBLEM
===================================

A squad of robotic rovers are to be landed by NASA on a plateau on Mars. This plateau, which is curiously rectangular, must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to send back to Earth.

A rover’s position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North.

In order to control a rover, NASA sends a simple string of letters. The possible letters are ‘L’, ‘R’ and ‘M’. ‘L’ and ‘R’ makes the rover spin 90 degrees left or right respectively, without moving from its current spot. ‘M’ means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1).

INPUT:
The first line of input is the upper-right coordinates of the plateau, the lower-left coordinates are assumed to be 0,0.

The rest of the input is information pertaining to the rovers that have been deployed. Each rover has two lines of input. The first line gives the rover’s position, and the second line is a series of instructions telling the rover how to explore the plateau.

The position is made up of two integers and a letter separated by spaces, corresponding to the x and y co-ordinates and the rover’s orientation.

Each rover will be finished sequentially, which means that the second rover won’t start to move until the first one has finished moving.

OUTPUT
The output for each rover should be its final co-ordinates and heading.

INPUT AND OUTPUT

Test Input:
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM

Expected Output:
1 3 N
5 1 E

Error Twitter Api search – Sorry, your query is too complex. Please reduce complexity and try again.

Hi,

I am found interesting case in twitter api search using geocode params.
When I call:

http://search.twitter.com/search.atom?geocode=40.757929%2C-73.985506%2C200km
it’s returned
….
Sorry, your query is too complex. Please reduce complexity and try again.
….

Strange,.because This call working some days ago.
And I spend some minutes and found that we can’t request more than 160km
This the right call:

Sample of rails application – basic user system ( signup, login, edit profile etc )

Hi,

I want to share rails 2 app sample.
It includes
– signup
– login
– activation ( via email )
– forgot password ( send to email )
– edit profile
– create member area
– simple layout for front end and member ( include basic css )
– script to feeds table countries

To run application, Please Follow this steps:

Checkout app

git clone git@github.com:febyartandi/rails_2_scratch_app.git

Change configuration

– change configuration for database myql in config/database.yml with yours

– change google account for sending email in config/application.yml
smtp_user_name: yourgmailaccount
smtp_password: yourgmailpassword

– rake db:create && rake db:migrate
– rake migration:populate
– ruby script/server

Hope this help people !

How to use linkedin api – invite people by email in ruby on rails

Hi guys,

This is quick tutorial about linkedin api 1 – invitation by people email to join your linkedin network.

We will use linkedinapi rails plugin ( Created by Dimas Priyanto and Me base on oauth rails plugin )

  • initialize your linkedin api key and secret key
>> linkedin_api_key = ” YOUR LINKEDIN API KEY”

>> secret_key = “YOUR LINKEDIN API SECRET”

  • setting your callback
  • create oauth object
>> oauth = LinkedIn::Oauth.new(linkedin_api_key, linkedin_secret)
  • request authorize url

Open the link in your browser  as redirection url for user to do authorization process,
if user authorize it we will get params[:oauth_verifier] in our “callback_url”,
use it to authorize from request

for example:

  • authorize using oauth verifier and token
>> oauth.authorize_from_request(oauth.request_token.token, oauth.request_token.secret, oauth_verifier)
  • create linkedin api object
>> linkedin = LinkedIn::Base.new(oauth)
  • show your linkedin profile
>> linkedin.profile
“<?xml version=\”1.0\” encoding=\”UTF-8\” standalone=\”yes\”?>\n<person>\n  <first-name>Feby</first-name>\n  <last-name>Artandi</last-name>\n  <headline>Ruby on rails programmer</headline>\n  <site-standard-profile-request>\n    <url>http://www.linkedin.com/profile?viewProfile=&amp;key=30011332&amp;authToken=nM0a&amp;authType=name&amp;trk=api*a101208*</url&gt;\n  </site-standard-profile-request>\n</person>\n”
“AAAAAA”
=> “<?xml version=\”1.0\” encoding=\”UTF-8\” standalone=\”yes\”?>\n<person>\n  <first-name>Feby</first-name>\n  <last-name>Artandi</last-name>\n  <headline>Ruby on rails programmer</headline>\n  <site-standard-profile-request>\n    <url>http://www.linkedin.com/profile?viewProfile=&amp;key=30011332&amp;authToken=nM0a&amp;authType=name&amp;trk=api*a101208*</url&gt;\n  </site-standard-profile-request>\n</person>\n”
  • invite people by email, first name and last name
linkedin.invite_by_email(“feby@kiranatama.com”, “Feby”, “Artandi”)

That’s it, enjoy ! 🙂

Simple code to test twitter oauth with ruby on rails via web browser

Hi,This is tutorial about implement twitter oauth in Ruby on Rails application using twitter_oauth gem.

You can see installation from http://github.com/moomerman/twitter_oauth/. The gem author show how to implement the oauth call in command prompt.

Now I’ll show you how to use the gem via web browser and save access key into database.

1. Create rails application ( if it’s your 1st rails project you should google about ruby on rails app )

2. Implement the restful authentication plugin ( see http://github.com/technoweenie/restful-authentication ) until you have user system.

3. Install the twitter_oauth gem http://github.com/moomerman/twitter_oauth/

4. Create migration

class CreateTwitterAccounts < ActiveRecord::Migration

  def self.up
    create_table :twitter_accounts do |t|
      t.integer :user_id
      t.string :token
      t.string :secret
      t.timestamps
    end
  end

  def self.down
    drop_table :twitter_accounts
  end
end

5. Edit user model, add this line

  has_one  :twitter_account, :dependent => :destroy

  def has_twitter_oauth?
    self.twitter_account and self.twitter_account.token and self.twitter_account.secret
  end

  def create_or_update_twitter_account_with_oauth_token(token, secret)
    twitter_account = self.twitter_account ? self.twitter_account : TwitterAccount.new(:user_id => self.id)
    twitter_account.token = token
    twitter_account.secret = secret
    twitter_account.save!
  end

6. Create and edit Model twitter_account ( ruby script/generate model twitter_account )
   Add this line: 

   belongs_to :user

7. Create try controller ( ruby script/generate controller try )
8 Edit try controller and copy-paste this:

   # require the gem twitter_oauth
   require 'twitter_oauth'
   # need user logged in
   before_filter :login_required

   #initialize your twitter key and secret ( you can get from here http://apiwiki.twitter.com/ )
   TWITTER_KEY = '83evR5lsb0E1gmvn0FXacQ'
   TWIITER_SECRET = 'REuY5OAS6EFXrC4KYCKYLX68qVBmx4lJshTZehIfI2M'

   # The url to back when user has authorize your app
   TWITTER_CALLBACK_URL = 'http://localhost:3000/try/callback'

    #index method
    # this method check if user has authorize your app
    # if no it'll show they link to authorize your app in their twitter account
    def index
      if !current_user.has_twitter_oauth?
        @T_OAUTH = TwitterOAuth::Client.new( :consumer_key => TWITTER_KEY, :consumer_secret => TWIITER_SECRET )
        @T_REQUEST_TOKEN = @T_OAUTH.request_token :oauth_callback => TWITTER_CALLBACK_URL
        @T_TOKEN = @T_REQUEST_TOKEN.token
        @T_SECRET = @T_REQUEST_TOKEN.secret
        @link = @T_REQUEST_TOKEN.authorize_url
        session['token' ] = @T_TOKEN
        session['secret'] = @T_SECRET
     end
  end

  # Method callback
  # this method will call after user authorize your app in twitter website
  # this method will authorize oauth token and verifier send from twitter and if success key and secret will stored in database
  def callback
    @T_OAUTH = TwitterOAuth::Client.new(:consumer_key => TWITTER_KEY, :consumer_secret => TWIITER_SECRET )
    @oauth_verifier = params[:oauth_verifier]
    access_token = @T_OAUTH.authorize(session['token'], session['secret'], :oauth_verifier => @oauth_verifier)
    if (@result = @T_OAUTH.authorized?)
      current_user.create_or_update_twitter_account_with_oauth_token(access_token.token, access_token.secret)
      session['token'] = nil
      session['secret'] = nil
      flash[:notice] = "Authorize complete"
    else
      flash[:notice] = "Authorize failed"
    end

    rescue
      flash[:notice] = "Authorize failed"
  end

  # method update status
  # this method is for sent tweet to your twitter
  def update_status
    if current_user.has_twitter_oauth?
      @T_OAUTH = TwitterOAuth::Client.new( :consumer_key => TWITTER_KEY,
      :consumer_secret => TWIITER_SECRET, :token => current_user.twitter_account.token,
      :secret => current_user.twitter_account.secret )

      if @T_OAUTH.authorized? and @T_OAUTH.update(params[:status])
        flash[:notice] = "Your tweet has been sent"
      else
        flash[:notice] = "Sorry ! Your tweet failed to sent, try later !"
      end
    else
      flash[:notice] = "You dont have twitter account with oauth token, please authorize myapp in your twitter !"
    end

   redirect_to :action => 'index' 

 end
9. Add file index.html.erb in app/views/try
    Copy - paste

    Test Twitter API

     <% if @link %>
        You have not authorize my app into your twitter 

       <%= link_to "Click here to authorize myapp.me", @link %>
     <% else %>
        Try to update your status 

        <% form_tag "/try/update_status", :method => "post" do %>
          <%= text_field_tag "status" %>
          <%= submit_tag "Update" %>
        <% end %>
      <% end %>

10. Add file callback.html.erb in app/views/try
   copy - paste

   <%= flash[:notice] %>

   <%= link_to "Click here", "/try" %> to back

Hope it can help you..
Please let me know if you find any errors or problems when implement the codes.

Tag Cloud