Class: Shin::Data::Dfi
- Inherits:
-
Object
- Object
- Shin::Data::Dfi
- Defined in:
- lib/shin/data/dfi.rb
Defined Under Namespace
Classes: HTTPError, MissingArgument, NotValid
Instance Method Summary collapse
-
#info(params = {}) ⇒ Object
Info.
- #new ⇒ Object
-
#search(params = {}) ⇒ Object
Search (on title).
Instance Method Details
#info(params = {}) ⇒ Object
Info
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/shin/data/dfi.rb', line 53 def info(params={}) raise MissingArgument, "You are missing the argument 'id' which is required to use this source." unless params[:id] != "" # Response response = Base.get('http://nationalfilmografien.service.dfi.dk/movie.svc/' + params[:id].to_s) raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200 # Nokogiri parse data = response.parsed_response['MovieItem'] rescue nil if data != nil {id: data['ID'].to_i, title: data['Title'], original_title: data['OriginalTitle'].strip, age_limit: data['Censorship'], url: data['Url'], description: data['Description']}.to_hashugar else raise NotValid, "Nokogiri failed to parse the XML." end end |
#new ⇒ Object
8 9 10 |
# File 'lib/shin/data/dfi.rb', line 8 def new self end |
#search(params = {}) ⇒ Object
Search (on title)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/shin/data/dfi.rb', line 13 def search(params={}) raise MissingArgument, "You are missing the argument 'title' which is required to use this source." unless params[:title] != "" params[:year] ||= nil # Response if params[:year] != nil response = Base.get('http://nationalfilmografien.service.dfi.dk/movie.svc/list?titlecontains=' + URI.encode(params[:title]) + '&startyear='+(params[:year] - 1).to_s+'&endyear='+(params[:year] + 1).to_s) else response = Base.get('http://nationalfilmografien.service.dfi.dk/movie.svc/list?titlecontains=' + URI.encode(params[:title])) end raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200 # Nokogiri parse doc = Nokogiri::XML(response.body) rescue nil # Can't be nil if doc != nil doc.remove_namespaces! @array = [] doc.xpath("//MovieListItems/MovieListItem").each do |item| # If year is nil then return all if params[:year] != nil return {id: item.xpath('./ID').text.to_i, name: item.xpath('./Name').text, url: item.xpath('./Url').text}.to_hashugar else @array << {id: item.xpath('./ID').text.to_i, name: item.xpath('./Name').text, url: item.xpath('./Url').text} end end @array.to_hashugar else raise NotValid, "Nokogiri failed to parse the XML." end end |