Module: BBC::Programmes

Defined in:
lib/bbc/programmes.rb,
lib/bbc/programmes/base.rb,
lib/bbc/programmes/brand.rb,
lib/bbc/programmes/series.rb,
lib/bbc/programmes/episode.rb,
lib/bbc/programmes/segment.rb,
lib/bbc/programmes/service.rb,
lib/bbc/programmes/version.rb,
lib/bbc/programmes/broadcast.rb,
lib/bbc/programmes/programme.rb,
lib/bbc/programmes/time_line.rb,
lib/bbc/programmes/time_interval.rb

Defined Under Namespace

Classes: Base, Brand, Broadcast, Episode, Programme, Segment, Series, Service, TimeInterval, TimeLine, Version

Constant Summary collapse

BASE_URI =
'http://www.bbc.co.uk/programmes'
USER_AGENT =
"bbc-programmes-ruby/0.1"

Class Method Summary collapse

Class Method Details

.fetch(identifier) ⇒ Object

Load a subject into the local repository



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bbc/programmes.rb', line 75

def self.fetch(identifier)
  unless identifier.is_a?(RDF::URI)
    identifier = RDF::URI("#{BASE_URI}/#{identifier}")
  end

  graph = self.http_get_graph(identifier)

  type = graph.first_object([identifier, RDF.type, nil])
  raise "No type found for #{identifier}" if type.nil?

  klass = BBC::Programmes::Base.subclasses.find { |k| k.type == type }
  raise "No class found for #{identifier} of type #{type}" if klass.nil?

  # Add the graph to the repository of the type
  graph.each_statement do |s|
    klass.repository << s
  end

  klass.for(identifier)
end

.http_get_graph(uri) ⇒ Object

Fetches and returns a Graph for an identifier



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bbc/programmes.rb', line 46

def self.http_get_graph(uri)
  uri = RDF::URI(uri) unless uri.is_a?(RDF::URI)

  # FIXME: support other HTTP libraries too
  res = Net::HTTP.start(uri.host, uri.port) do |http|
    http.get(uri.request_uri, {
      'User-Agent' => USER_AGENT,
      'Accept' => 'application/rdf+xml'
    })
  end

  # Throw an exception if it failed
  res.value

  # Find a reader for the content type returned
  reader_class = RDF::Reader.for(:content_type => res.content_type)
  raise "No reader found for parsing: #{res.content_type}" if reader_class.nil?

  # Parse the RDF
  RDF::Graph.new(uri) do |graph|
    reader_class.new(res.body, :base_uri => uri) do |reader|
      reader.each_statement do |statement|
        graph << statement
      end
    end
  end
end

.search(keywords) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bbc/programmes.rb', line 30

def self.search(keywords)
  encoded = Addressable::URI.encode(keywords)
  graph = http_get_graph("#{BASE_URI}/a-z/by/#{encoded}/all.rdf")

  # FIXME: push graph into the repository?

  programmes = []
  graph.query([nil, RDF.type, RDF::PO.Programme]) do |s|
    programme = Programme.for(s.subject)
    programme.title = graph.first_literal([s.subject, RDF::DC11.title, nil])
    programmes << programme
  end
  programmes
end