Ruby Course - Lesson 7 - Mashups With Ruby

Preview:

Citation preview

Mashups with Ruby

Monday, September 13, 2010

Today’s lesson

Introduction to mashups

Basic introduction to common technologies - REST, JSON

Mashups

Using Google ClientLogin for authentication

Using HostIP to geocode your IP and Geonames to find nearby street names

Monday, September 13, 2010

What’s a mashup?

Monday, September 13, 2010

A method of programming that uses data and/or

service from one or more sources external from the

system

Monday, September 13, 2010

Basically, getting someone else to do the

‘dirty’ job

Monday, September 13, 2010

Web as a platform

Monday, September 13, 2010

Types of mashups

Monday, September 13, 2010

Types of mashups

DataService

Monday, September 13, 2010

Types of mashups

DataService

API

SDKRedirect

Monday, September 13, 2010

Types of mashups

DataService

API

SDKRedirect

Mashup app

Mashup plug-in

Monday, September 13, 2010

Some examples

Monday, September 13, 2010

Monday, September 13, 2010

Monday, September 13, 2010

Monday, September 13, 2010

Common mashup methods

Monday, September 13, 2010

Common mashup methods

SDKs• Language specific packages• Use language specific APIs• Tight integration with application

Monday, September 13, 2010

Common mashup methods

Web APIsSDKs• Language specific packages• Use language specific APIs• Tight integration with application

Monday, September 13, 2010

Common mashup methods

Web APIsSDKs• Language specific packages• Use language specific APIs• Tight integration with application

XML-RPC• Remote procedure call

(RPC)• Use XML to package RPC• Transport over HTTP

Monday, September 13, 2010

Common mashup methods

Web APIsSDKs• Language specific packages• Use language specific APIs• Tight integration with application

XML-RPC• Remote procedure call

(RPC)• Use XML to package RPC• Transport over HTTP

SOAP• Use XML to package

data and calls• Transport over HTTP

and others• More used in

enterprise settings

Monday, September 13, 2010

Common mashup methods

Web APIsSDKs• Language specific packages• Use language specific APIs• Tight integration with application

XML-RPC• Remote procedure call

(RPC)• Use XML to package RPC• Transport over HTTP

SOAP• Use XML to package

data and calls• Transport over HTTP

and others• More used in

enterprise settings

REST• Describe the transfer

and action on resources that are identified by URI

• closely tied to HTTP• Most popular in Web

2.0 apps

Monday, September 13, 2010

RESTful web services

Monday, September 13, 2010

Uses HTTP methods explicitly

Monday, September 13, 2010

GET /adduser?name=Robert HTTP/1.1

Creating a new user (non-REST)

Monday, September 13, 2010

GET /adduser?name=Robert HTTP/1.1

Creating a new user (non-REST)

Creating a new user (REST)POST /users HTTP/1.1Host: myserverContent-Type: application/xml<?xml version="1.0"?><user> <name>Robert</name></user>

Monday, September 13, 2010

Deal with resources identified by URIs

Monday, September 13, 2010

Stateless

Monday, September 13, 2010

Transfer XML, JSON, YAML etc

Monday, September 13, 2010

JSONJavascript Object Notation

Monday, September 13, 2010

Lightweight text-based exchange standard designed for human-readability

Often used as an alternative to XML{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }

Monday, September 13, 2010

Accessing REST APIs with Ruby

Monday, September 13, 2010

RestClienthttp://github.com/archiloque/rest-client

Monday, September 13, 2010

RestClient.get 'http://example.com/resource'

RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}

RestClient.get 'https://user:password@example.com/private/resource', {:accept => :json}

RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }

RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json

RestClient.delete 'http://example.com/resource'

response = RestClient.get 'http://example.com/resource'response.code➔ 200response.cookies➔ {"Foo"=>"BAR", "QUUX"=>"QUUUUX"}response.headers➔ {:content_type=>"text/html; charset=utf-8", :cache_control=>"private" ...

Monday, September 13, 2010

Most of the time, you will be using some gem to access the web APIs

Monday, September 13, 2010

Hands-on #1Authenticating with Google ClientLogin

http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html

Monday, September 13, 2010

Preferred external authentication is through

OAuth or Open ID

NOTE!

Monday, September 13, 2010

Monday, September 13, 2010

Monday, September 13, 2010

Monday, September 13, 2010

%w(rubygems haml sinatra rack-flash json rest_client).each { |gem| require gem}%w(models helpers).each {|feature| require feature}

set :sessions, trueset :show_exceptions, falseuse Rack::Flash

get '/' do redirect '/dashboard' if session[:id] redirect '/login'end

get '/login' do haml :login, :layout => false end

post '/login' do if authenticate(params[:email], params[:password]) redirect '/' else redirect_with_message '/login', 'Email or password wrong. Please try again' endend

get '/logout' do session.clear redirect '/'end

get '/dashboard' do require_login haml :dashboardend

error do redirect '/' end

auth.rb

Monday, September 13, 2010

helpers do def require_login redirect_with_message('/login', 'Please login first') unless session[:id] end def authenticate(email, password) response = RestClient.post('https://www.google.com/accounts/ClientLogin', 'accountType' => 'HOSTED_OR_GOOGLE', 'Email' => email, 'Passwd' => password, :service => 'xapi', :source => 'Goog-Auth-1.0') do |response, request, result, &block| user = User.find :email => email if response.code == 200 and not user.nil? session[:id] = response.to_s session[:user] = user.id.to_s return true end return false end end

def redirect_with_message(to_location, message) flash[:message] = message redirect to_location endend

helpers.rb

Monday, September 13, 2010

class User attr :email def self.find(options) return true if options[:email] = 'sausheong@gmail.com' endend

models.rb

Change this in your own code when trying out this

hands-on (should be GMail account)

Monday, September 13, 2010

Hands-on #2Geocode your IP with HostIP and find nearest

street with Geonames http://www.hostip.info/

http://www.geonames.org/export/ws-overview.html

Monday, September 13, 2010

Monday, September 13, 2010

get '/geo' do require_login location = location_from_ip @loc = location['Country'] @ip = location['IP'] @long = location['Longitude'] @lat = location['Latitude'] nearby_streets = find_nearby_with @lat, @long @street_names = nearby_streets['streetSegment'].collect { |segment| segment['name'] }.uniq haml :geoend

auth.rb

Monday, September 13, 2010

def location_from_ip response = RestClient.get "http://api.hostip.info/get_html.php?position=true" YAML.load response.bodyend

def find_nearby_with(lat,long) response = RestClient.get "http://ws.geonames.org/findNearbyStreetsOSMJSON?lat=#{lat}&lng=#{long}" JSON.parse response.bodyend

helpers.rb

Should normally use ip=#{request.ip} to get the user’s incoming IP

Monday, September 13, 2010

Questions?

Monday, September 13, 2010