Smartphone Native Application using Ruby in 30 min

Building a Smartphone (Android, iPhone, BlackBerry etc) app using ruby

Google Android
iPhone Logo
BlackBerry Logo


RhoMobile Framework


Rhodes

Rhodes is an open source framework to rapidly build NATIVE apps for all major smartphone operating systems (iPhone, Windows Mobile, RIM, Symbian and Android). These are true native device applications (NOT mobile web apps) which work with synchronized local data and take advantage of device capabilities such as GPS, PIM contacts and camera.

Yeah! Its in Ruby ūüôā

  • support for ALL smartphones
  • synchronized offline data
  • a Model View Controller (MVC) framework
  • an Object Relational Manager (ORM) to avoid direct SQL coding
  • the mobile Ruby
  • a hosted development service
  • scaffolding to quickly build the app

Setup (Android)

  • gem install rhodes
  • Java JDK latest version is needed for Android. Make sure its installed. Test by typing $javac -version in your terminal
  • Download¬†Android SDK (for¬†linux,¬†Mac OS,¬†Windows) and extract at a¬†convenient location.
  • Change in your ~/.bash_profile(Mac) and ~/.bashrc(Linux) to add
    export PATH=/path-to-android/tools:$PATH
    export ANDROID_HOME=/path-to-android
  • typing $android in the terminal shall start Android SDK and AVD Manager
  • once ACD manager started install the latest packages under “Available packages”, it will take a while to install
  • Once installed they will list under “Installed Packages”
  • else installation follow these¬†instructions to install Android SDK starter packages, and change the PATH environment variable for sure
  • Download and install Android NDK (Win, Mac OS, Linux) and export your path same as above

Setting up rhodes
Now we need to link our rhodes installation to the SDK and eclipse.
$rhodes-setup
You’ll be prompted with questions – my answers are below:
  • JDK path: /Library/Java/Home
  • Android SDK path: /opt/androidSDK/
  • Android NDK path: /opt/androidNDK/
  • the remainder of the questions related to Windows and Blackberry: (left blank)

HelloWorld Program

rhogen app HelloWorld

cd HelloWorld

rake -T  (will list all the rake tasks for all supported smartphones)

rake run:android (it will build, install and launch the HelloWorld Program)

Of course it will be blank and its just the start. Go to launcher you will be able to see your HelloWorld application with rhodes default icon.

My Contacts

This is fine, but what about ruby and MVC framework? Open your latest HelloWorld in your¬†favorite¬†editor(textmate etc). You will find an “app”, “app=>helpers” folder much like rails “rhoconfig.txt” => configuration settings

$ rhogen model Contact name, email, phone

Generating with model generator:

[ADDED]  app/Contact/index.erb

[ADDED]  app/Contact/edit.erb

[ADDED]  app/Contact/new.erb

[ADDED]  app/Contact/show.erb

[ADDED]  app/Contact/index.bb.erb

[ADDED]  app/Contact/edit.bb.erb

[ADDED]  app/Contact/new.bb.erb

[ADDED]  app/Contact/show.bb.erb

[ADDED]  app/Contact/contact_controller.rb

[ADDED]  app/Contact/contact.rb

[ADDED]  app/test/contact_spec.rb

You can see the controller, model and view files similar to Rails frame work.

Change the

start_path = ‘/app/Contact’ (in rhoconfig.txt)

$ rake run:android

Your application will relaunch and you can click “new” button to add new contacts and edit and delete them. So basic CRUD functionality is available much like rails and ready to go.

I hope you will find this ready to go tutorial very helpful to start of your application on any smartphone using Rhodes framework.

Stay tuned for more!

Railscasts crawler (Download all screencasts easily)

I wrote this script, as I felt really annoying to download each screencast everytime I needed them. So I ended up writing script to automatically download all screencasts without hassle i.e crawler.

Its in Ruby of course ūüôā and requires simple Hpricot gem.
If you not having it. Just type this command in your terminal —

$gem install hpricot

–Rest include this script in your /lib folder
–Change the path in the script where you want to download all the screencasts
–Go to your projects development environment (script/console) and run the script by these commands–
video = Railscasts.new #new Railscasts object
video.start #will start downloading all screencasts from Railscasts
Note:
  1. If you stop the script in between manually, it will not download those screencasts that are already in your hard disk.
  2. All logs are maintained in Railsproject/log/railscasts.log.
  3. Handling all exceptions raised

Improvements/Suggestions  are appreciated.

Thanks
And yes script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
</pre>
<pre># Author : Akshay Gupta</pre>
<pre>#file: railscasts.rb
# First check you have all gems installed. Place the script in /lib folder and run the script.
# I don't have expertize in ruby, please tell how it can be improved.
# change the path accordingly, where you want to save path
# My working env is on MacOS, one need to make some changes if running on Windows
<code>
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'logger'
$log = Logger.new('log/railscasts.log')
$path = "/Users/akshaygupta/railsvideo/railscasts/"
$stop = false

class Railscasts
attr_accessor :url

def initialize
  @@page = 1
  @@url  = "http://railscasts.com/episodes?page="
  start
end

def url
  @url = @@url+@@page.to_s
end

def start
  url
  build_doc
  screencasts_links
  download_screencasts
  next_page
  if !$stop
    start
  else
    puts "Successfully done :) Enjy all the screencasts"
  end
end

def build_doc
  begin
    $log.info("*********Fetching #{@url}***********")
    @doc = Hpricot(open(@url))
  rescue Exception =&gt; e
    $log.debug("Problem fetching #{e}")
  end
end

def screencasts_links
  begin
    @download_links =
      (@doc/".download/a[1]").collect {|a| (a.search("[@href]").first[:href])}
    $log.info(" All Download links on this page :\n #{@download_links}")
  rescue
    $log.info("Problem in download links")
  end
end

def download_screencasts
  @download_links.each do |mov|
    begin
      file = mov.split('/').last
      res = `cd #{$path}; ls | grep "#{file}"`
      if !res
        $log.info("Now downloading file #{file}")
        result = `cd #{$path}; wget "#{mov}"`
        if result
          $log.info("Successfully Downloaded #{file}")
        end
      else
        $log.info("Already downloaded #{file}")
      end
    rescue Exception =&gt; e
      $log.info("problem downloding file #{e}")
    end
  end
end

  def next_page
    if @@page &lt; 17
      @@page += 1
    else
      $log.info("All screencasts downloaded :-), Mission accomplished!!")
      $stop = true
    end
  end
end</code></pre>
<pre><span style="font-family: monospace;">