Class: ObservationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/observations_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#honeypot

Instance Method Details

#clearObject

DELETE /stations/:station_id/observations


64
65
66
67
68
69
70
71
# File 'app/controllers/observations_controller.rb', line 64

def clear
  # get station with Friendly Id, params[:id] can either be id or slug
  Observation.delete_all("station_id = #{@station.id}")
  respond_to do |format|
    format.html { redirect_to station_url(@station) }
    format.json { head :no_content }
  end
end

#createObject

TODO:

CLEANUP since the legacy Ardiuno routes which do not include the :station_id i think we can safely assume the presence of the station.

POST /observations


18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/observations_controller.rb', line 18

def create
  # Find station via ID or SLUG
  @observation = Observation.new(observation_params)
  if @observation.station.nil? && !@station.nil?
    @observation.station = @station
  end
  if @observation.save
    render nothing: true, status: :ok
  else
    render nothing: true, status: :unprocessable_entity
  end
end

#destroyObject

DELETE /observations/:id


53
54
55
56
57
58
59
60
61
# File 'app/controllers/observations_controller.rb', line 53

def destroy
  @observation = Observation.find(params[:id])
  @station = @observation.station
  @observation.destroy
  respond_to do |format|
    format.html { redirect_to station_observations_path(@station) }
    format.json { head :no_content }
  end
end

#indexObject

GET /stations/:station_id/observations


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/observations_controller.rb', line 32

def index
  expires_in expiry_time(@station), public: true
  if stale?(@station)
    respond_to do |format|
      @observations = @station.observations
      format.html do
        @observations = @observations.desc
                     .paginate(page: params[:page])
      end
      format.json do
        @observations = @station.load_observations!(
            @station.observations_per_day,
            query: Observation.desc.since(24.hours.ago)
        )
        render json: @observations
      end
    end
  end
end