35
Rhomobile – Ruby no Rhomobile – Ruby no Mobile Mobile

Desenvolvimento Mobile com Ruby

Embed Size (px)

DESCRIPTION

Palestra de Desenvolvimento Mobile com Ruby, no evento Rock And Rails, www.rockandrails.com.br

Citation preview

Page 1: Desenvolvimento Mobile com Ruby

Rhomobile – Ruby no MobileRhomobile – Ruby no Mobile

Page 2: Desenvolvimento Mobile com Ruby

Quem sou?

Rodrigo Martins

[email protected]

@rr_martins

www.rrmartins.com

Page 3: Desenvolvimento Mobile com Ruby

Quem sou?

Rodrigo Martins

[email protected]

@rr_martins

www.rrmartins.com

AntesAntes→ → PHPPHP→ → JavaJava→ → ClipperClipper→ → ZimZim

Page 4: Desenvolvimento Mobile com Ruby

Quem sou?

Rodrigo Martins

[email protected]

@rr_martins

www.rrmartins.com

AntesAntes→ → PHPPHP→ → JavaJava→ → ClipperClipper→ → ZimZim

Hoje, e Feliz!Hoje, e Feliz!→ → RubyRuby→ → PythonPython

Page 5: Desenvolvimento Mobile com Ruby

Qual o futuro?

Page 6: Desenvolvimento Mobile com Ruby

O que forma o Rhomobile?

Page 7: Desenvolvimento Mobile com Ruby

Estrutura

Page 8: Desenvolvimento Mobile com Ruby

Rhodes O que é? É um framework open-source, permite aos

desenvolvedores criar aplicações nativas para telefones móveis com a portabilidade de tecnologias web tais como XHTML, CSS, JavaScript e do poder do Ruby

Instalação:

$ gem install rhodes

$ rhodes-setup

Page 9: Desenvolvimento Mobile com Ruby

Rhodes Generando aplicação:

$ rhodes app ror http://localhost:3000/application

rhodes app <name_application> <path_server> rhoconfig.txt ->

syncserver = 'http://localhost:3000/application'

Page 10: Desenvolvimento Mobile com Ruby

Rhodes Generando aplicação:

$ rhodes app ror http://localhost:3000/application

rhodes app <name_application> <path_server> rhoconfig.txt ->

syncserver = 'http://localhost:3000/application'

Page 11: Desenvolvimento Mobile com Ruby

Rhodes index.erb.htmlEste é o arquivo de início, desta forma

<nome_aplicacao>/app/. A partir de agora todo o código é um pouco semelhante a escrever um RubyOnRails aplicação.

Page 12: Desenvolvimento Mobile com Ruby

Running the Application Rhodes Executando:

$ cd applicatonTest

$ rake run:iphone Rodando para outros Devises:

$ rake run:iphone

$ rake run:android

$ rake run:bb

Page 13: Desenvolvimento Mobile com Ruby

rake tasks

$ rake -T

Page 14: Desenvolvimento Mobile com Ruby

Rhodes Generando Models$ cd application$ rhodes model pessoa nome, idade, sobrenome, sexo, cpf Generating with model generator: [ADDED] app/Pessoa/index.erb [ADDED] app/Pessoa/edit.erb [ADDED] app/Pessoa/new.erb [ADDED] app/Pessoa/show.erb [ADDED] app/Pessoa/index.bb.erb [ADDED] app/Pessoa/edit.bb.erb [ADDED] app/Pessoa/new.bb.erb [ADDED] app/Pessoa/show.bb.erb [ADDED] app/Pessoa/pessoa_controller.rb [ADDED] app/Pessoa/pessoa.rb [ADDED] app/test/pessoa_spec.rb

Page 15: Desenvolvimento Mobile com Ruby

Views Rhodes edition Exemplo rapido (app/index.erb.html):

<div id="pageTitle"><h1>ApplicationTest</h1></div>

<div id="toolbar"> <div id="leftItem" class="blueButton"> <%= link_to "Sync", :controller => :Settings, :action => :do_sync %> </div> <% if SyncEngine::logged_in > 0 %> <div id="rightItem" class="regularButton"> <%= link_to "Logout", :controller => :Settings, :action => :logout %> </div> <% else %> <div id="rightItem" class="regularButton"> <%= link_to "Login", :controller => :Settings, :action => :login %> </div> <% end %></div>

Page 16: Desenvolvimento Mobile com Ruby

Views Rhodes edition Exemplo rapido (app/Pessoa/index.erb.html):

<div data-role="page"> <div data-role="header" data-position="inline"> <h1>Pessoas</h1> <a href="<%= Rho::RhoConfig.start_path %>" class="ui-btn-left" data-icon="home" data-

direction="reverse">Home</a> <a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">New</a> </div> <div data-role="content"> <ul data-role="listview"> <% @pessoas.each do |pessoa| %> <li> <a href="<%= url_for :action => :show, :id => pessoa.object %>"> <%= pessoa.nome %> </a> </li> <% end %> </ul> </div></div>

Page 17: Desenvolvimento Mobile com Ruby

Views Rhodes edition Exemplo rapido (app/Pessoa/index.erb.html):

<div data-role="page"> <div data-role="header" data-position="inline"> <h1>Pessoas</h1> <a href="<%= Rho::RhoConfig.start_path %>" class="ui-btn-left" data-icon="home"

data-direction="reverse">Home</a> <a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">New</a> </div> <div data-role="content"> <ul data-role="listview"> <% @pessoas.each do |pessoa| %> <li> <a href="<%= url_for :action => :show, :id => pessoa.object %>"> <%= pessoa.nome %> </a> </li> <% end %> </ul> </div></div>

He came out of where?He came out of where?

Page 18: Desenvolvimento Mobile com Ruby

Views Rhodes edition Exemplo rapido (app/Pessoa/index.erb.html):

<div data-role="page"> <div data-role="header" data-position="inline"> <h1>Pessoas</h1> <a href="<%= Rho::RhoConfig.start_path %>" class="ui-btn-left" data-icon="home"

data-direction="reverse">Home</a> <a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">New</a> </div> <div data-role="content"> <ul data-role="listview"> <% @pessoas.each do |pessoa| %> <li> <a href="<%= url_for :action => :show, :id => pessoa.object %>"> <%= pessoa.nome %> </a> </li> <% end %> </ul> </div></div>

He came out of where?He came out of where?

start_path = '/app' # rhoconfig.txtstart_path = '/app' # rhoconfig.txt

Page 19: Desenvolvimento Mobile com Ruby

RhoSync O que é?

Um framework de sincronização é uma estrutura composta por um componente de cliente no dispositivo e um componente de servidor que roda em qualquer servidor que podem executar o Ruby.

Page 20: Desenvolvimento Mobile com Ruby

Instalar dependencias RhoSync$ sudo gem install rhosync

$ wget http://redis.googlecode.com/files/redis-2.2.7.tar.gz

$ tar xzf redis-2.2.7.tar.gz

$ cd redis-2.2.7

$ cd src; make

$ make install

$ cp ../redis.conf /usr/local/etc

More: www.redis.io

Page 21: Desenvolvimento Mobile com Ruby

Instalar RhoSync$ rhosync app applicationTest-server

$ cd applicationTest-server/ If you are running first on the Mac or Linux,

you must install dtach:

$ sudo rake dtach:install

Page 22: Desenvolvimento Mobile com Ruby

Rodando a aplicação RhoSync

$ rake redis:start

$ rake rhosync:start

If all went well you should see:

[12:30:15 PM 2011-07-22] Rhosync Server v2.1.0 started...

Page 23: Desenvolvimento Mobile com Ruby

Definindo Adapters RhoSync

Ligar a um serviço de back-end com RhoSync requer que você escreva uma pequena quantidade de código Ruby para a consulta, criar, atualizar e excluir as operações de seu backend empresa particular.

Page 24: Desenvolvimento Mobile com Ruby

$ cd /applicationTest

$ rhosync source pessoa

---

Generating with source generator:

[ADDED] sources/pessoa.rb

[ADDED] spec/sources/pessoa_spec.rb

Definindo Adapters RhoSync

Page 25: Desenvolvimento Mobile com Ruby

pessoa.rbclass Pessoa < SourceAdapter def initialize(source) super(source) end

def login # TODO: Login to your data source here if necessary end

def query # TODO: Query your backend data source and assign the records # to a nested hash structure called @result. For example: # @result = { # "1"=>{"name"=>"Acme","industry"=>"Electronics"}, # "2"=>{"name"=>"Best", "industry"=>"Software"} # } raise SourceAdapterException.new("Please provide some code to read records from the backend data source") end

def sync # Manipulate @result before it is saved, or save it # yourself using the Rhosync::Store interface. # By default, super is called below which simply saves @result super end

def create(create_hash,blob=nil) # TODO: Create a new record in your backend data source # If your rhodes rhom object contains image/binary data # (has the image_uri attribute), then a blob will be provided raise "Please provide some code to create a single record in the backend data source using the create_hash" End

def update(update_hash) raise "Please provide some code to update a single record in the backend data source using the update_hash" end

Page 26: Desenvolvimento Mobile com Ruby

pessoa.rbdef delete(object_id) # TODO: write some code here if applicable # be sure to have a hash key and value for "object" # for now, we'll say that its OK to not have a delete operation # raise "Please provide some code to delete a single object in the backend #application using the hash values in name_value_list" end

def logoff # TODO: Logout from the data source if necessary endend

Page 27: Desenvolvimento Mobile com Ruby

settings/settings.yml The generator will modify this file:

#Sources

:sources:

Pessoa:

:poll_interval: 300

Page 28: Desenvolvimento Mobile com Ruby

Testando synchronization$ rake rhosync:start

$ rake rhosync:stop

$ rake rhosync:restart

Page 29: Desenvolvimento Mobile com Ruby

Sincronizando o model

class Pessoa

include Rhom::PropertyBag

enable :sync

end

Page 30: Desenvolvimento Mobile com Ruby

Creando Objetos com RhoSyncdef create(create_hash, blob=nil)

result = RestClient.post(@base, :pessoa => create_hash)

location = "#{result.headers[:location]}.json"

new_record = RestClient.get(location).body

JSON.parse(new_record)["pessoa"]["id"].to_s

end

--------create_hash :

{

“name” => “Lucas”,

“idade” => “21”

}

Page 31: Desenvolvimento Mobile com Ruby

Atualizar o adaptador de fonte

$ rake rhosync:restart

O objeto será criado imediatamente no cliente e enviado para o servidor na próxima sincronização.

Page 32: Desenvolvimento Mobile com Ruby

Autenticação O arquivo application.rb, editando o metodo de authenticate:

def authenticate(username, password, session) # ... connect to backend using API and authenticate ... if success # save the data for later use in the source adapter Store.put_value("username:#{username}:token",username) end return successend

Page 33: Desenvolvimento Mobile com Ruby

Source

http://docs.rhomobile.com/

http://rhomobile.com/

http://groups.google.com/group/rhomobile

Page 34: Desenvolvimento Mobile com Ruby

Doubt? :D

Page 35: Desenvolvimento Mobile com Ruby

Contact

Rodrigo Martins

[email protected]

@rr_martins

www.rrmartins.com