Class: Station

Inherits:
Object
  • Object
show all
Defined in:
lib/rbtune/station.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, uri, name: '', ascii_name: '', description: '') ⇒ Station

Returns a new instance of Station.



63
64
65
66
67
68
69
# File 'lib/rbtune/station.rb', line 63

def initialize(id, uri, name: '', ascii_name: '', description: '')
  @id          = id.to_s.upcase
  @uri         = uri
  @name        = normalize_name name
  @ascii_name  = ascii_name
  @description = description
end

Instance Attribute Details

#ascii_nameObject (readonly)

Returns the value of attribute ascii_name.



6
7
8
# File 'lib/rbtune/station.rb', line 6

def ascii_name
  @ascii_name
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/rbtune/station.rb', line 7

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/rbtune/station.rb', line 5

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/rbtune/station.rb', line 4

def name
  @name
end

#uriObject (readonly)

Returns the value of attribute uri.



8
9
10
# File 'lib/rbtune/station.rb', line 8

def uri
  @uri
end

Class Method Details

.fetch_stationsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rbtune/station.rb', line 31

def self.fetch_stations
  db = Station::pstore_db
  Radio.bands.each do |radio_class|
    begin
      $stderr.puts ">>> fetching #{radio_class} stations..."
      radio = radio_class.new
      radio.open
      stations = radio.fetch_stations
      if stations.empty?
        $stderr.puts "    warning: no station found."
      else
        db.transaction { db[radio_class.to_s] = stations }
        $stderr.puts "    #{stations.size} stations fetched."
      end

    rescue SocketError
      $stderr.puts $!

    rescue Net::HTTPNotFound
      $stderr.puts $!

    rescue REXML::ParseException
      $stderr.puts $!

    rescue
      # 例外を握りつぶしてすべてのクラスの放送局情報の取得を試みる
      $stderr.puts $!
    end
  end
end

.list_stationsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbtune/station.rb', line 15

def self.list_stations
    Radio.bands.each do |radio|
      name = radio.to_s
      stations = radio.stations
      if stations.nil? || stations.empty?
        $stderr.puts "warning: #{name} に放送局が登録されていません。"
        $stderr.puts "         rbtune --fetch-stations を実行して、放送局情報を取得してください。"
      else
        puts "* #{name}"
        stations.each { |station| puts "    #{station}" }
        puts ''
      end
    end
end

.pstore_dbObject



10
11
12
# File 'lib/rbtune/station.rb', line 10

def self.pstore_db
  @db ||= PStore.new(File.join(ENV['HOME'], '.rbtune.db'))
end

Instance Method Details

#==(other) ⇒ Object



87
88
89
# File 'lib/rbtune/station.rb', line 87

def ==(other)
  id == other.id
end

#normalize_name(name) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/rbtune/station.rb', line 72

def normalize_name(name)
  # NHKの局名はnormalizeしない (NHKFM-* の-が取れてしまうから)
  return name if name.include?('NHK')
  name.strip
  .sub(/[ -]?FM[ -]?/i, 'FM')
  .sub(/fm|FM|エフエム|えふえむ/, 'FM')
  .tr(' !@', ' !@')
end

#to_sObject Also known as: inspect



81
82
83
# File 'lib/rbtune/station.rb', line 81

def to_s
  "#{id}:\t\t#{name}\t\t#{ascii_name}"
end