module GtfsEngine::Concerns::Controllers::Gtfs

This file is part of the KNOWtime server.

The KNOWtime server is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The KNOWtime server is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the KNOWtime server. If not, see <www.gnu.org/licenses/>.

This controller Concern provides the functionality common among most of the GTFS controllers.

Public Instance Methods

filter() click to toggle source

@return <ActionController::Parameters> the map of fields to filter;

derived from the query string
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 69
  def filter
    @filter ||= begin
      query = self.query
      unknown = query.map do |q, v|
        query[q] = true if v.blank? # blank value indicates boolean filter
        filters.include?(q.to_sym) ? nil : q
      end.compact
require 'pry'
      binding.pry
      unless unknown.empty?
        raise GtfsEngine::UnknownFilters.new(unknown), 'unknown filter'
      end
      query_params = ActionController::Parameters.new query
      query_params.permit filters
    end
  end
index() click to toggle source

GET / collection of elements for the given GTFS type The returned collection may be filtered with query parameters

# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 56
def index
  @records = query.empty? ? collection : filtered_collection
  respond_with @records, template: 'gtfs_engine/gtfs/index'
end
show() click to toggle source

GET /:id for a specific element of the given GTFS type

# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 62
def show
  @record = record
  respond_with @record, template: 'gtfs_engine/gtfs/show'
end

Protected Instance Methods

collection() click to toggle source

@return [ActiveRecord::Relation] all the records in this GTFS association

# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 89
def collection
  data.send controller_name
end
filtered_collection() click to toggle source

@return [ActiveRecord::Relation] all the records in this GTFS

association that match the filter specified in the query string
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 95
def filtered_collection
  collection.where filter
end
filters() click to toggle source
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 109
def filters
  self.class.filters
end
gtfs_id() click to toggle source
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 105
def gtfs_id
  self.class.gtfs_id
end
query() click to toggle source
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 113
def query
  request.query_parameters
end
record() click to toggle source

@return [ActiveRecord::Base] the record identified by params[:id] in this

GTFS association
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 101
def record
  collection.find_by! gtfs_id => params[:id]
end

Private Instance Methods

format() click to toggle source
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 119
def format
  request.format.to_sym
end
gtfs_cache() { || ... } click to toggle source
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 123
def gtfs_cache
  options = {
    etag: [controller_name, data, query],
    last_modified: data.updated_at,
    public: true
  }

  expires_in 3.hours
  yield if stale? options
end
record_not_found(ex) click to toggle source
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 154
def record_not_found(ex)
  json = {status: 'fail', data: {gtfs_id => params[:id]}}
  render status: :not_found, format => json
end
statement_invalid(ex) click to toggle source
# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 140
def statement_invalid(ex)
  inner = ex.original_exception
  case inner
  when PG::InvalidDatetimeFormat, PG::DatetimeFieldOverflow
    lines = inner.message.split "\n"
    /.*"([^"]+)"[^"]+/ === lines[1][0..lines[2].size] and begin
      json = {status: 'fail', data: {$1 => 'invalid date'}}
      render status: :bad_request, format => json
    end
  else
    raise ex
  end
end
unknown_filter(ex) click to toggle source

@param ex [GtfsEngine::UnknownFilter]

# File lib/gtfs_engine/concerns/controllers/gtfs.rb, line 135
def unknown_filter(ex)
  json = {status: 'fail', data: ex.to_hash}
  render status: :bad_request, format => json
end