Leveraging Web Services

Leveraging Web Services

  • There are many many different kinds of services that deliver their info over a web service.
  • Use google and Programmable Web has thousands of APIs you can use
    • You might have to call a service directly (using HTTP)
    • You might find a gem to provide a nicer, limited, ruby apii to the service
    • You might elect to build your own gem
  • Good design practice to isolate details of the web service from the rest of the code
Security, Authentication, Money
  • Several reasons why a service might need authentication
    • They want to charge for it
    • They want to force you to register with them so they know who you are
    • They simply want to put in a speedbump
  • Most typicall technique
    • You register on their site (manually) and they give you api token of some kind (a random string)
    • Every call to the service includes a mandatory paramter
Case study
  • Problem: Need to get an assortment of information about stocks: price quotes, historical info, and so on.
  • Steps to solve the problem
    1. Locate an appropriate web service, with google and progammable web site
    2. Investigate whether there is an API, is it REST, is it free or does it cost, do they have the data I want
    3. A bunch of googling led me to two candidates: Yahoo Stocks Api and Markit On Demand
    4. Experiment to see if they do what I need. See Github Web Service Demo
    5. Build a class or set of classes to encapsulate the API calls. Consider isolation from web service , error handling, caching of common results.
  • Key Gems to make your life good: typhoeusXmlSimple

Delivering Web Services

  • Providing a Web API from your server
  • Often nice to provide sample code and a ‘binding’ to a language
  • Need to decide on the url (REST!)
  • Latest thinking is that you should not base it on your underlying relational database structure.
  • In other words, to get title of course 3’s lecture 4
    • Don’t specify: http://teachback/api/course/3/lecture/4/title
    • Do specify: http://teachback/api/course_info?code=COSI-234 -> xml structure with info about that course.
Case Study
  • Goal: Your server needs to respond to HTTP GET for this url: http://teachback/api/course_info?code=COSI-234
    • Controller: api
    • Action: course_info
    • Parameter: code=COSI-234
  • Let’s take a look at the code in Github Web Service Demo
  • Try this url: http://0.0.0.0:3000/api/course_info.xml?course=aaa