Révision | 22b21a067414d43c97df1212a0d5f0c42371818e (tree) |
---|---|
l'heure | 2017-07-31 20:06:17 |
Auteur | HMML <hmml3939@gmai...> |
Commiter | HMML |
Implement VERY simple forecast API.
@@ -0,0 +1,3 @@ | ||
1 | +# Place all the behaviors and hooks related to the matching controller here. | |
2 | +# All this logic will automatically be available in application.js. | |
3 | +# You can use CoffeeScript in this file: http://coffeescript.org/ |
@@ -0,0 +1,3 @@ | ||
1 | +// Place all the styles related to the Api::V1::Forecasts controller here. | |
2 | +// They will automatically be included in application.css. | |
3 | +// You can use Sass (SCSS) here: http://sass-lang.com/ |
@@ -0,0 +1,17 @@ | ||
1 | +class Api::V1::ForecastsController < ApplicationController | |
2 | + respond_to :json | |
3 | + before_action :set_target | |
4 | + | |
5 | + def show | |
6 | + @area.timezone? and | |
7 | + Time.zone = @area.timezone | |
8 | + respond_with @area | |
9 | + end | |
10 | + | |
11 | + private | |
12 | + def set_target | |
13 | + @area = Area.find_by!(ns: params.require(:area_ns), code: params.require(:area_code)) | |
14 | + now = Time.now | |
15 | + @forecasts = @area.forecasts.where('date >= ? and date <= ?', now, now + 1.day) | |
16 | + end | |
17 | +end |
@@ -0,0 +1,2 @@ | ||
1 | +module Api::V1::ForecastsHelper | |
2 | +end |
@@ -1,3 +1,8 @@ | ||
1 | 1 | class Area < ApplicationRecord |
2 | 2 | has_many :forecasts |
3 | + | |
4 | + def utc_offset | |
5 | + timezone? or return nil | |
6 | + ActiveSupport::TimeZone[timezone].utc_offset | |
7 | + end | |
3 | 8 | end |
@@ -0,0 +1,2 @@ | ||
1 | +%h1 Api::V1::Forecasts#show | |
2 | +%p Find me in app/views/api/v1/forecasts/show.html.haml |
@@ -0,0 +1,6 @@ | ||
1 | +reported_at = nil | |
2 | +@forecasts.each {|f| f.reported_at.to_i > reported_at.to_i and reported_at = f.reported_at } | |
3 | + | |
4 | +json.extract! @area, :ns, :code, :country, :pref, :name, :updated_at, :timezone, :utc_offset | |
5 | +json.reported_at reported_at | |
6 | +json.daily_forecasts @forecasts, :weather_symbol, :weather_label, :date, :reported_at, :temp_min, :temp_max, :pop |
@@ -1,11 +1,17 @@ | ||
1 | 1 | Rails.application.routes.draw do |
2 | + namespace :api, defaults: {format: :json} do | |
3 | + namespace :v1 do | |
4 | + get ':area_ns/:area_code', to: 'forecasts#show' | |
5 | + end | |
6 | + end | |
7 | + | |
2 | 8 | devise_for :users |
3 | - resources :users | |
4 | - resources :forecasts | |
5 | - resources :areas | |
9 | + | |
6 | 10 | get 'sub', to: 'subscribe#verify' |
7 | 11 | post 'sub', to: 'subscribe#distribute' |
8 | - | |
12 | + resources :users, only: %i(index show) | |
13 | + resources :forecasts, only: %i(index show) | |
14 | + resources :areas, only: %i(index show) | |
9 | 15 | resources :dist_signals, only: %i(index show) |
10 | 16 | |
11 | 17 | root 'dummy_error_controller#index' |
@@ -0,0 +1,6 @@ | ||
1 | +class AddTimeZoneToArea < ActiveRecord::Migration[5.1] | |
2 | + def change | |
3 | + add_column :areas, :timezone, :string | |
4 | + execute "UPDATE areas SET timezone = 'Asia/Tokyo' WHERE country = 'ja'" | |
5 | + end | |
6 | +end |
@@ -0,0 +1,5 @@ | ||
1 | +class AddNsCodeIndexOnArea < ActiveRecord::Migration[5.1] | |
2 | + def change | |
3 | + add_index :areas, [:ns, :code], unique: true | |
4 | + end | |
5 | +end |
@@ -10,7 +10,7 @@ | ||
10 | 10 | # |
11 | 11 | # It's strongly recommended that you check this file into your version control system. |
12 | 12 | |
13 | -ActiveRecord::Schema.define(version: 20170724142504) do | |
13 | +ActiveRecord::Schema.define(version: 20170731102841) do | |
14 | 14 | |
15 | 15 | create_table "areas", force: :cascade do |t| |
16 | 16 | t.string "ns" |
@@ -20,6 +20,8 @@ ActiveRecord::Schema.define(version: 20170724142504) do | ||
20 | 20 | t.string "name" |
21 | 21 | t.datetime "created_at", null: false |
22 | 22 | t.datetime "updated_at", null: false |
23 | + t.string "timezone" | |
24 | + t.index ["ns", "code"], name: "index_areas_on_ns_and_code", unique: true | |
23 | 25 | end |
24 | 26 | |
25 | 27 | create_table "delayed_jobs", force: :cascade do |t| |
@@ -0,0 +1,12 @@ | ||
1 | +require 'rails_helper' | |
2 | + | |
3 | +RSpec.describe Api::V1::ForecastsController, type: :controller do | |
4 | + | |
5 | + describe "GET #show" do | |
6 | + it "returns http success" do | |
7 | + get :show | |
8 | + expect(response).to have_http_status(:success) | |
9 | + end | |
10 | + end | |
11 | + | |
12 | +end |