Guest User

Untitled

a guest
Mar 1st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. require 'xmlrpc/server'
  2. require 'rubygems'
  3. require_gem 'activerecord'
  4.  
  5. #set up active record
  6. @db_settings = YAML::load(IO.read("../config/database.yml"))
  7. curr_db = @db_settings["development"]
  8. ActiveRecord::Base.establish_connection(:adapter => curr_db["adapter"], :database => curr_db["database"],
  9. :host => curr_db["host"], :username => curr_db["username"], :password => curr_db["password"])
  10.  
  11. #this should come from a config file
  12. MOVIE_BASE_DIR = 'C:\Dev\projects\training\code tutorials\movies'
  13.  
  14. server = XMLRPC::Server.new(8080)
  15.  
  16. server.add_handler("movie.getLessonMovies") do |name|
  17. puts "movie.getLessonMovies called with parameters #{name}"
  18. if(name.empty?)
  19. raise XMLRPC::FaultException.new(-99, "No lesson name given")
  20. else
  21. lesson = Lesson.find(:first,
  22. :conditions => ["name=?", name])
  23. puts "returning movie list: #{lesson.movies.split(",").collect{|movie| movie.strip}.join(',')}"
  24. [lesson.description, lesson.movies.split(",").collect{|movie| movie.strip}]
  25. end
  26. end
  27.  
  28. server.add_handler("movie.getMovieInfo") do |name|
  29. puts "movie.getMovieInfo called with parameters '#{name.strip}'"
  30. if(name.empty?)
  31. raise XMLRPC::FaultException.new(-99, "No movie name given")
  32. else
  33. movieInfo = Movie.find( :first,
  34. :conditions => ["name=?", name.strip])
  35. if(movieInfo == nil)
  36. raise XMLRPC::FaultException.new(-99, "No movie was found with the name '#{name}'")
  37. else
  38. retVal = [movieInfo.name, movieInfo.description, movieInfo.frames, MOVIE_BASE_DIR + movieInfo.file]
  39. puts "returning #{retVal.join(',')}"
  40. retVal
  41. end
  42. end
  43. end
  44.  
  45. server.set_default_handler do |name, *args|
  46. puts "Unknown XMLRPC method called, method: '#{name}', arguments: #{args}"
  47. raise XMLRPC::FaultException.new(-99, "Method #{name} missing or wrong number of parameters")
  48. end
  49.  
  50. server.serve
Add Comment
Please, Sign In to add comment