Module: Poms

Extended by:
Poms
Included in:
Poms
Defined in:
lib/poms.rb,
lib/poms/base.rb,
lib/poms/season.rb,
lib/poms/series.rb,
lib/poms/builder.rb,
lib/poms/version.rb,
lib/poms/broadcast.rb,
lib/poms/has_ancestors.rb,
lib/poms/schedule_event.rb,
lib/poms/has_base_attributes.rb

Defined Under Namespace

Modules: Base, Exceptions, HasAncestors, HasBaseAttributes Classes: Broadcast, Builder, ScheduleEvent, Season, Series, Strand

Constant Summary collapse

URL =
'http://docs.poms.omroep.nl'
MEDIA_PATH =
'/media/'
BROADCASTS_VIEW_PATH =
'/media/_design/media/_view/broadcasts-by-broadcaster-and-start'
ANCESTOR_AND_TYPE_PATH =
'/media/_design/media/_view/by-ancestor-and-type'
ANCESTOR_AND_SORTDATE_PATH =
'/media/_design/media/_view/by-ancestor-and-sortdate'
CHANNEL_AND_START_PATH =
'/media/_design/media/_view/broadcasts-by-channel-and-start'
VALID_CHANNELS =
/^NED(1|2|3)$/
VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#ancestor_sortdate_params(mid, start_time = 1.month.ago) ⇒ Object



90
91
92
93
94
# File 'lib/poms.rb', line 90

def ancestor_sortdate_params(mid, start_time=1.month.ago)
  end_time_i    = 1.week.from_now.to_i * 1000
  start_time_i  = start_time.to_i * 1000
  "?reduce=false&startkey=[\"#{mid}\",#{start_time_i}]&endkey=[\"#{mid}\",#{end_time_i}]&include_docs=true"
end

#ancestor_type_params(mid, type) ⇒ Object



86
87
88
# File 'lib/poms.rb', line 86

def ancestor_type_params(mid, type)
  "?reduce=false&key=[\"#{mid}\",\"#{type}\"]&include_docs=true"
end

#broadcast_view_params(zender, start_time, end_time) ⇒ Object

private



81
82
83
84
# File 'lib/poms.rb', line 81

def broadcast_view_params(zender, start_time, end_time)
  zender = zender.capitalize
  "?startkey=[\"#{zender}\",#{start_time.to_i * 1000}]&endkey=[\"#{zender}\",#{end_time.to_i * 1000}]&reduce=false&include_docs=true"
end

#channel_params(channel, start_time, end_time) ⇒ Object



97
98
99
# File 'lib/poms.rb', line 97

def channel_params(channel, start_time, end_time)
  "?startkey=[\"#{channel}\",#{start_time.to_i * 1000}]&endkey=[\"#{channel}\",#{end_time.to_i * 1000}]&reduce=false&include_docs=true"
end

#fetch(mid) ⇒ Object

?startkey=&endkey=&reduce=false&include_docs=true



23
24
25
26
27
# File 'lib/poms.rb', line 23

def fetch(mid)
  return nil if mid.nil?
  hash = fetch_raw_json mid
  Poms::Builder.process_hash hash
end

#fetch_broadcasts_by_channel_and_start(channel, start_time = 1.week.ago, end_time = Time.now) ⇒ Object



60
61
62
63
64
# File 'lib/poms.rb', line 60

def fetch_broadcasts_by_channel_and_start(channel, start_time=1.week.ago, end_time=Time.now)
  uri = [CHANNEL_AND_START_PATH, channel_params(channel, start_time, end_time) ].join
  hash = get_json(uri)
  hash['rows'].map {|item| Poms::Builder.process_hash item['doc']}
end

#fetch_current_broadcast(channel) ⇒ Object



66
67
68
69
70
71
# File 'lib/poms.rb', line 66

def fetch_current_broadcast(channel)
  uri = [CHANNEL_AND_START_PATH, channel_params(channel, Time.now, 1.day.ago), '&descending=true&limit=1' ].join
  hash = get_json(uri)
  rows = hash['rows']
  Poms::Builder.process_hash(rows.empty? ? {} : rows.first['doc'])
end

#fetch_descendant_mids(mid, type = 'BROADCAST') ⇒ Object



54
55
56
57
58
# File 'lib/poms.rb', line 54

def fetch_descendant_mids(mid, type='BROADCAST')
  uri = [ANCESTOR_AND_TYPE_PATH, ancestor_type_params(mid, type), "&include_docs=false"].join
  hash = get_json(uri) || {'rows' => []}
  hash['rows'].map {|item| item['id']}
end

#fetch_descendants_by_date_for_serie(mid, start_time = 1.week.ago) ⇒ Object



48
49
50
51
52
# File 'lib/poms.rb', line 48

def fetch_descendants_by_date_for_serie(mid, start_time=1.week.ago)
  uri = [ANCESTOR_AND_SORTDATE_PATH, ancestor_sortdate_params(mid, start_time) ].join
  hash = get_json(uri) || {'rows' => []}
  hash['rows'].map {|item| Poms::Builder.process_hash item['doc']}
end

#fetch_descendants_for_serie(mid, type = 'BROADCAST') ⇒ Object Also known as: fetch_broadcasts_for_serie



40
41
42
43
44
# File 'lib/poms.rb', line 40

def fetch_descendants_for_serie(mid, type='BROADCAST')
  uri = [ANCESTOR_AND_TYPE_PATH, ancestor_type_params(mid, type) ].join
  hash = get_json(uri) || {'rows' => []}
  hash['rows'].map {|item| Poms::Builder.process_hash item['doc']}
end

#fetch_next_broadcast(channel) ⇒ Object



73
74
75
76
77
78
# File 'lib/poms.rb', line 73

def fetch_next_broadcast(channel)
  uri = [CHANNEL_AND_START_PATH, channel_params(channel, Time.now, 1.day.from_now), '&limit=1' ].join
  hash = get_json(uri)
  rows = hash['rows']
  Poms::Builder.process_hash(rows.empty? ? {} : rows.first['doc'])
end

#fetch_raw_json(mid) ⇒ Object



29
30
31
32
# File 'lib/poms.rb', line 29

def fetch_raw_json(mid)
  uri = [MEDIA_PATH, mid].join
  get_json(uri)
end

#get_json(uri) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/poms.rb', line 101

def get_json(uri)
  begin
    JSON.parse(open(URI.escape [URL, uri].join).read)
  rescue OpenURI::HTTPError => e
    raise e unless e.message.match(/404/)
    nil
  end
end

#upcoming_broadcasts(zender, start_time = Time.now, end_time = Time.now+7.days) ⇒ Object



34
35
36
37
38
# File 'lib/poms.rb', line 34

def upcoming_broadcasts(zender, start_time = Time.now, end_time = Time.now+7.days)
  uri = [BROADCASTS_VIEW_PATH, broadcast_view_params(zender, start_time, end_time )].join
  hash = get_json(uri)
  hash['rows'].map {|item| Poms::Builder.process_hash item['doc']}
end