Module: INatChannel::API

Defined in:
lib/inat-channel/api.rb

Class Method Summary collapse

Class Method Details

.load_list(**query) ⇒ Object



29
30
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
61
62
63
64
# File 'lib/inat-channel/api.rb', line 29

def load_list **query
  result = []
  page = 1
  
  loop do 
    IC::logger.debug "Fetch page #{page} with per_page=#{PER_PAGE}"

    response = faraday.get API_ENDPOINT do |req|
      req.params[:page] = page
      req.params[:per_page] = PER_PAGE
      req.params[:fields] = LIST_FIELDS
      req.params.merge! query
    end

    unless response.success?
      IC::logger.error "❌ Failed to fetch observations page #{page}: HTTP #{response.status}"
      IC::notify_admin "❌ Failed to fetch observations page #{page}: HTTP #{response.status}"
      break
    end

    data = JSON.parse response.body, symbolize_names: true
    result += data[:results]

    total = data[:total_results] || 0
    IC::logger.debug "Page #{page}: fetched #{data.size} records, total expected #{total}"
    break if data.empty? || result.size >= total
    page += 1
    sleep PAGE_DELAY
  end

  IC::logger.debug "Loaded total #{result.size} records"
  result
rescue => e
  IC::logger.error "❌ Exception while loading news: #{e.full_message}"
  IC::notify_admin "❌ Exception while loading news: #{e.message}"
end

.load_newsObject



25
26
27
# File 'lib/inat-channel/api.rb', line 25

def load_news 
  load_list(**IC::CONFIG[:base_query], updated_since: (Date.today - IC::CONFIG.dig(:days_back, :fresh)).to_s)
end

.load_observation(uuid) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/inat-channel/api.rb', line 66

def load_observation uuid
  response = faraday.get API_ENDPOINT do |req|
    req.params['uuid'] = uuid
    req.params['locale'] = IC::CONFIG[:base_query][:locale] if IC::CONFIG[:base_query][:locale]
    req.params['fields'] = SINGLE_FIELDS
  end

  if response.success?
    data = JSON.parse response.body, symbolize_names: true
    obs = data[:results]&.first
    IC::logger.debug "Loaded observation: #{uuid}"
    obs
  else
    IC::logger.error "Error loading observation #{uuid}: HTTP #{response.status}"
    IC::notify_admin "Error loading observation #{uuid}: HTTP #{response.status}"
    nil
  end
rescue => e
  IC::notify_admin "Exception while loading observation #{uuid}: #{e.message}"
  IC::logger.error e.full_message
  nil
end