Connect direct to web service with RhoMobile 4.0 - rhomobile

I am working on application using RhoMobile 4.0. I want to know, how to connect to web service in rhomobile 4.0 without rhoconnect. Need to do http post and get.

Using RhoMobile, you can using the usual AJAX methods to reach a webservice to get or post data.
Additionally you can use the RhoMobile Network API from Ruby or from JavaScript to call a webservice, registering a callback.
For example in Ruby you can have this code in a controller:
def getData
#Perform an HTTP GET request.
getProps = Hash.new
getProps['url'] = "http://<my_url>/Json/Server/GetDada?username=admin&password=pass"
getProps['headers'] = {"Content-Type" => "application/json"}
Rho::Network.get(getProps, url_for(:action => :get_callback))
render :action => :transferring
end
def get_callback
if #params['status'] == "ok"
get_result = Rho::JSON.parse(#params['body'])
puts "**** Parsed it" #{#params['body']}"
# Do something with the data
end
end

Related

Twilio can't find xml on rails

I am integrating twilio click to call into my rails project.
Everything works fine however the url: in my twilio controller cannot be found on heroku. However, it can be found if you navigate to it in a browser. The phone dials but the voice says "Sorry a problem has occurred, good bye." If I change the url to an external xml file it works fine, just doesn't recognize this particular one. So I'm lead to believe that the controller etc works fine.
twillio_controller.rb
def call
#full_phone = current_user.phone
#partial_phone = #full_phone.last(-1)
#connected_number = "+61" + #partial_phone
#client = Twilio::REST::Client.new ##twilio_sid, ##twilio_token
# Connect an outbound call to the number submitted
#call = #client.calls.create(
:from => ##twilio_number,
:to => #connected_number,
:url => 'http://besttradies.herokuapp.com/mytradies/connect.xml', # Fetch instructions from this URL when the call connects
)
#msg = { :message => 'Phone call incoming!', :status => 'ok' }
end
def connect
# Our response to this request will be an XML document in the "TwiML"
# format. Our Ruby library provides a helper for generating one
# of these documents
response = Twilio::TwiML::Response.new do |r|
r.Say 'If this were a real click to call implementation, you would be connected to an agent at this point.', :voice => 'alice'
end
render text: response.text
end
The OP solved in the comments above:
Figured it out. Routes for connect needed to be POST and I also had to
add skip_before_action :verify_authenticity_token to the twilio
controller as it was behind membership doors.

How to post to external url from rails app without using form

I am able to post to my payment gateway using a html form. But I got a new requirement. Now, On click of "payment" button, i have to validate whether item is still available for purchase and if available then automatically post the payment form to gateway.
So to say, I want to execute some rails controller code on button click and then want to redirect to an external url ( payment gateway ) with params if validation succeed.
I read, it is not possible to post using redirect_to in rails. It is also not possible to post using ajax to external url. How should i achieve it ?
You can use Ruby’s Net::HTTP like this:
require "net/http"
require "uri"
uri = URI.parse("http://example.com/search")
params = {"foo" => "value one", "bar" => "value two"}
# Shortcut way
response = Net::HTTP.post_form(uri, params)
# With more control
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(params)
response = http.request(request)
puts response.code # => 200
puts response.body # => The body (HTML, JSON, etc)

can't post from app to another app in server side

I have 2 apps running on my localhost on port 3000 and 9000 (rails 2 and sinatra app).
I have set up a controller in the rails app (without any specific model or view) in app/controllers/finance_service.rb
class FinanceServiceController < ApplicationController
def after_token_create
p "after token create function: #{params.inspect}"
end
end
and had set up a route like so :
map.finance_service '/finance_service' , :controller => "finance_service", :action => "after_token_create"
and When I access it on the url http://localhost:3000/finance_service
I get error on template missing, but that's fine because it means the route is working (I am using it as a service api to the another app).
when trying to access the method from the other app using httparty gem like so :
HTTParty.post("http://localhost:3000/FinanceServiceController/after_token_create", :body => post_params)
I get an error on the rails app-
myapp/public/404.html (method_not_allowed)
Also tried from advanced Rest client app, and I get the same error.
Shouldn't the request be like this? since the route matcher is /finance_service
HTTParty.post("http://localhost:3000/finance_service", :body => post_params)

parsing # in rails uris

i am getting the following url information and need to parse it within rails. i already checked request and params but it is not there.
the "#" character seems to f*ck up things.
here's the url:
http://foo.bar.me/whoo#access_token=131268096888809%7C2.5BRBl_qt4xJ08n88ycbpZg__.3600.1276880400-100001151606930%7C0kJ1K-qoGBbDoGbLx6s4z5UEaxM.
thanks for any pointers.
You won't be able to access the part after the '#' character as the browser doesn't send it to the server. You can use it on the client side with javascript though.
It seems that you're trying to use the javascript based authentication which is not what you really want.
I didn't have any problems using this oauth2 library. Then you only need to check for params[:code] within your callback action.
UPDATE:
This is a simplified version of the code I used in my experiments with the new facebook graph API:
# Accessible as facebook_url:
# routes.rb: map.facebook '/facebook', :controller => 'facebook', :action => 'index'
def index
oauth2 = OAuth2::Client.new(FB_API_KEY, FB_API_SECRET, :site => 'https://graph.facebook.com')
if current_user.facebook_token
# The user is already authenticated
fb = OAuth2::AccessToken.new(oauth2, current_user.facebook_sid)
result = JSON.parse(fb.get('/me'))
elsif params[:code]
# Here we get the access token from facebook
fb = oauth2.web_server.get_access_token(params[:code], :redirect_uri => facebook_url)
result = JSON.parse(fb.get('/me'))
current_user.facebook_id = result["id"]
current_user.facebook_token = fb.token.to_s
current_user.save
else
# The user is visiting this page for the first time. We redirect him to facebook
redirect_to oauth2.web_server.authorize_url(:redirect_uri => facebook_url, :scope => 'read_stream,publish_stream,offline_access')
end
end
You don't really need anything else for it to work.

Rhomobile rhodes Rho AsyncHttp post

I am having problems with Rhomobile rhodes, plaese can someone tell me how to make http post, get, put, and delete using Rho::AsyncHttp?
I've been trying it to no success for hours.
Here's some sample code to place in your controller.rb file
Here's the initial call
def index
Rho::AsyncHttp.get(
:url => 'http://the.page.you.want.to.get',
:callback => (url_for :action => :httpget_callback),
:callback_param => "" )
render :action => :wait
end
the code above will initiate the httpget_callback method (below)
while that goes off and loads the url it'll change the screen and load the wait.erb file
def httpget_callback
if #params['status'] != 'ok'
##error_params = #params
WebView.navigate(url_for :action => :show_error )
else
#html = #params['body']
end
WebView.navigate ( url_for :action => :show_result )
end
Without getting too far into it - the body of the returned page is placed into #html variable
Hope that helps, if you need more help, let me know.
I have a sample of get an post
res = Rho::AsyncHttp.post(:url => 'http://192.168.1.64/WebServiceTest/Service.asmx/Sumar')
#msg= "Sync http call: #{res}"
http://wiki.rhomobile.com/index.php/RhodesConnectToWebServices
I'm often struggling with the nuances of AsyncHttp in Rhodes as well, so I can't claim mastery yet, but I really felt the need to chime in with a suggestion:
I find using the Firebug plugin of Firefox to be VERY helpful when debugging my Rhodes app. You can hook it up very easily! You can load your app with any browser by configuring the web server to run on a specific port. This setting is in rhoconfig.txt and it is called local_server_port.
This is specifically helpful because you can easily survey the HTML and raw data of requests/responses and use the console to run javascript commands and play with the DOM and web page in realtime.

Resources