Class: Metar::Raw::Noaa

Inherits:
Base
  • Object
show all
Defined in:
lib/metar/raw.rb

Overview

Collects METAR data from the NOAA site via FTP

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(station) ⇒ Noaa

Station is a string containing the CCCC code, or an object with a ‘cccc’ method which returns the code



105
106
107
# File 'lib/metar/raw.rb', line 105

def initialize(station)
  @cccc = station.respond_to?(:cccc) ? station.cccc : station
end

Class Method Details

.fetch(cccc) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/metar/raw.rb', line 80

def self.fetch(cccc)
  connection = Net::FTP.new('tgftp.nws.noaa.gov')
  connection.
  connection.chdir('data/observations/metar/stations')
  connection.passive = true

  attempts = 0
  while attempts < 2
    begin
      s = ''
      connection.retrbinary("RETR #{cccc}.TXT", 1024) do |chunk|
        s += chunk
      end
      connection.close
      return s
    rescue Net::FTPPermError, Net::FTPTempError, EOFError
      attempts += 1
    end
  end

  raise "Net::FTP.retrbinary failed #{attempts} times"
end

Instance Method Details

#dataObject Also known as: raw



109
110
111
112
# File 'lib/metar/raw.rb', line 109

def data
  fetch
  @data
end

#metarObject



121
122
123
124
# File 'lib/metar/raw.rb', line 121

def metar
  fetch
  @metar
end

#timeObject



116
117
118
119
# File 'lib/metar/raw.rb', line 116

def time
  fetch
  @time
end