• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revision22b21a067414d43c97df1212a0d5f0c42371818e (tree)
Time2017-07-31 20:06:17
AuthorHMML <hmml3939@gmai...>
CommiterHMML

Log Message

Implement VERY simple forecast API.

Change Summary

Incremental Difference

--- /dev/null
+++ b/app/assets/javascripts/api/v1/forecasts.coffee
@@ -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/
--- /dev/null
+++ b/app/assets/stylesheets/api/v1/forecasts.scss
@@ -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/
--- /dev/null
+++ b/app/controllers/api/v1/forecasts_controller.rb
@@ -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
--- /dev/null
+++ b/app/helpers/api/v1/forecasts_helper.rb
@@ -0,0 +1,2 @@
1+module Api::V1::ForecastsHelper
2+end
--- a/app/models/area.rb
+++ b/app/models/area.rb
@@ -1,3 +1,8 @@
11 class Area < ApplicationRecord
22 has_many :forecasts
3+
4+ def utc_offset
5+ timezone? or return nil
6+ ActiveSupport::TimeZone[timezone].utc_offset
7+ end
38 end
--- /dev/null
+++ b/app/views/api/v1/forecasts/show.html.haml
@@ -0,0 +1,2 @@
1+%h1 Api::V1::Forecasts#show
2+%p Find me in app/views/api/v1/forecasts/show.html.haml
--- /dev/null
+++ b/app/views/api/v1/forecasts/show.json.jbuilder
@@ -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
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,11 +1,17 @@
11 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+
28 devise_for :users
3- resources :users
4- resources :forecasts
5- resources :areas
9+
610 get 'sub', to: 'subscribe#verify'
711 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)
915 resources :dist_signals, only: %i(index show)
1016
1117 root 'dummy_error_controller#index'
--- /dev/null
+++ b/db/migrate/20170731101544_add_time_zone_to_area.rb
@@ -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
--- /dev/null
+++ b/db/migrate/20170731102841_add_ns_code_index_on_area.rb
@@ -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
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
1010 #
1111 # It's strongly recommended that you check this file into your version control system.
1212
13-ActiveRecord::Schema.define(version: 20170724142504) do
13+ActiveRecord::Schema.define(version: 20170731102841) do
1414
1515 create_table "areas", force: :cascade do |t|
1616 t.string "ns"
@@ -20,6 +20,8 @@ ActiveRecord::Schema.define(version: 20170724142504) do
2020 t.string "name"
2121 t.datetime "created_at", null: false
2222 t.datetime "updated_at", null: false
23+ t.string "timezone"
24+ t.index ["ns", "code"], name: "index_areas_on_ns_and_code", unique: true
2325 end
2426
2527 create_table "delayed_jobs", force: :cascade do |t|
--- /dev/null
+++ b/spec/controllers/api/v1/forecasts_controller_spec.rb
@@ -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