46
Mashups with Ruby Monday, September 13, 2010

Ruby Course - Lesson 7 - Mashups With Ruby

Embed Size (px)

Citation preview

Page 1: Ruby Course - Lesson 7 - Mashups With Ruby

Mashups with Ruby

Monday, September 13, 2010

Page 2: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 3: Ruby Course - Lesson 7 - Mashups With Ruby

What’s a mashup?

Monday, September 13, 2010

Page 4: Ruby Course - Lesson 7 - Mashups With Ruby

A method of programming that uses data and/or

service from one or more sources external from the

system

Monday, September 13, 2010

Page 5: Ruby Course - Lesson 7 - Mashups With Ruby

Basically, getting someone else to do the

‘dirty’ job

Monday, September 13, 2010

Page 6: Ruby Course - Lesson 7 - Mashups With Ruby

Web as a platform

Monday, September 13, 2010

Page 7: Ruby Course - Lesson 7 - Mashups With Ruby

Types of mashups

Monday, September 13, 2010

Page 8: Ruby Course - Lesson 7 - Mashups With Ruby

Types of mashups

DataService

Monday, September 13, 2010

Page 9: Ruby Course - Lesson 7 - Mashups With Ruby

Types of mashups

DataService

API

SDKRedirect

Monday, September 13, 2010

Page 10: Ruby Course - Lesson 7 - Mashups With Ruby

Types of mashups

DataService

API

SDKRedirect

Mashup app

Mashup plug-in

Monday, September 13, 2010

Page 11: Ruby Course - Lesson 7 - Mashups With Ruby

Some examples

Monday, September 13, 2010

Page 12: Ruby Course - Lesson 7 - Mashups With Ruby

Monday, September 13, 2010

Page 13: Ruby Course - Lesson 7 - Mashups With Ruby

Monday, September 13, 2010

Page 14: Ruby Course - Lesson 7 - Mashups With Ruby

Monday, September 13, 2010

Page 15: Ruby Course - Lesson 7 - Mashups With Ruby

Common mashup methods

Monday, September 13, 2010

Page 16: Ruby Course - Lesson 7 - Mashups With Ruby

Common mashup methods

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

Monday, September 13, 2010

Page 17: Ruby Course - Lesson 7 - Mashups With Ruby

Common mashup methods

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

Monday, September 13, 2010

Page 18: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 19: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 20: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 21: Ruby Course - Lesson 7 - Mashups With Ruby

RESTful web services

Monday, September 13, 2010

Page 22: Ruby Course - Lesson 7 - Mashups With Ruby

Uses HTTP methods explicitly

Monday, September 13, 2010

Page 23: Ruby Course - Lesson 7 - Mashups With Ruby

GET /adduser?name=Robert HTTP/1.1

Creating a new user (non-REST)

Monday, September 13, 2010

Page 24: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 25: Ruby Course - Lesson 7 - Mashups With Ruby

Deal with resources identified by URIs

Monday, September 13, 2010

Page 26: Ruby Course - Lesson 7 - Mashups With Ruby

Stateless

Monday, September 13, 2010

Page 27: Ruby Course - Lesson 7 - Mashups With Ruby

Transfer XML, JSON, YAML etc

Monday, September 13, 2010

Page 28: Ruby Course - Lesson 7 - Mashups With Ruby

JSONJavascript Object Notation

Monday, September 13, 2010

Page 29: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 30: Ruby Course - Lesson 7 - Mashups With Ruby

Accessing REST APIs with Ruby

Monday, September 13, 2010

Page 31: Ruby Course - Lesson 7 - Mashups With Ruby

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

Monday, September 13, 2010

Page 32: Ruby Course - Lesson 7 - Mashups With Ruby

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

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

RestClient.get 'https://user:[email protected]/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

Page 33: Ruby Course - Lesson 7 - Mashups With Ruby

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

Monday, September 13, 2010

Page 34: Ruby Course - Lesson 7 - Mashups With Ruby

Hands-on #1Authenticating with Google ClientLogin

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

Monday, September 13, 2010

Page 35: Ruby Course - Lesson 7 - Mashups With Ruby

Preferred external authentication is through

OAuth or Open ID

NOTE!

Monday, September 13, 2010

Page 36: Ruby Course - Lesson 7 - Mashups With Ruby

Monday, September 13, 2010

Page 37: Ruby Course - Lesson 7 - Mashups With Ruby

Monday, September 13, 2010

Page 38: Ruby Course - Lesson 7 - Mashups With Ruby

Monday, September 13, 2010

Page 39: Ruby Course - Lesson 7 - Mashups With Ruby

%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

Page 40: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 41: Ruby Course - Lesson 7 - Mashups With Ruby

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

models.rb

Change this in your own code when trying out this

hands-on (should be GMail account)

Monday, September 13, 2010

Page 42: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 43: Ruby Course - Lesson 7 - Mashups With Ruby

Monday, September 13, 2010

Page 44: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 45: Ruby Course - Lesson 7 - Mashups With Ruby

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

Page 46: Ruby Course - Lesson 7 - Mashups With Ruby

Questions?

Monday, September 13, 2010