Class: APIWrapper

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/world_tides/api_wrapper.rb

Constant Summary collapse

EKEY =
<<~EKEY
R6S3-rUOOByRSSKoY1EW1dlxoqYZ8vn9bH0BzvlaaY9_KZ8SoinkGJ9URxzG
wDvugK7lIJS9TcoC2OoAtb8VN-0T2gqaGDkRC1so_C3oe_K8JBW68HjPm3jfPpDbWO4t
EKEY
SESSION =
'ipbm5c99jaf7rsf179jihn3ti1'
DAYS =
7
DATUM =
'LAT'
INPUTS =

From what I can tell, the ekey is ekey is verified against the cookie

%i(lat lon)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat:, lon:) ⇒ APIWrapper

Returns a new instance of APIWrapper.



26
27
28
29
30
31
# File 'lib/world_tides/api_wrapper.rb', line 26

def initialize(lat:, lon:)
  @lat = lat
  @lon = lon
  @res = make_request
  raise "Failed with #{@res['status']}" unless @res['status'] == 200
end

Instance Attribute Details

#resObject (readonly)

Returns the value of attribute res.



22
23
24
# File 'lib/world_tides/api_wrapper.rb', line 22

def res
  @res
end

Instance Method Details

#highs_and_lowsObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/world_tides/api_wrapper.rb', line 33

def highs_and_lows
  @highs_and_lows ||= begin
    @res['extremes'].map do |record|
      {
        date: record['date'].to_time.to_s(:short),
        type: record['type'].downcase,
        height: record['height']
      }
    end
  end
end

#highs_and_lows_csv(output_file) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/world_tides/api_wrapper.rb', line 45

def highs_and_lows_csv(output_file)
  File.open("#{output_file}.csv", 'w') do |f|
    f << CSV.generate do |csv|
      csv << highs_and_lows.first.keys
      highs_and_lows.each { |h_a_l| csv << h_a_l.values }
    end
  end
end