• 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

Revisionce3282006b6f1e30ca41326c089a7a4ffaa3a1a8 (tree)
Time2017-03-02 22:18:34
AuthorHMML <hmml3939@gmai...>
CommiterTatsuki Sugiura

Log Message

Create DistSignal on accept content distribution request.

Change Summary

Incremental Difference

--- /dev/null
+++ b/app/assets/javascripts/dist_signals.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/
--- a/app/assets/stylesheets/application.scss
+++ b/app/assets/stylesheets/application.scss
@@ -1,5 +1,5 @@
11 @import "bootstrap-sprockets";
22 @import "bootstrap";
3-/*
4- *= require_tree .
5- */
3+
4+@import "common";
5+@import "dist_signals";
\ No newline at end of file
--- /dev/null
+++ b/app/assets/stylesheets/common.scss
@@ -0,0 +1,6 @@
1+#main-header {
2+ display: none;
3+}
4+#main-container {
5+ //margin-top: 48px;
6+}
\ No newline at end of file
--- /dev/null
+++ b/app/assets/stylesheets/dist_signals.scss
@@ -0,0 +1,3 @@
1+// Place all the styles related to the DistSignals 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/assets/stylesheets/scaffolds.scss
@@ -0,0 +1,89 @@
1+body {
2+ background-color: #fff;
3+ color: #333;
4+ font-family: verdana, arial, helvetica, sans-serif;
5+ font-size: 13px;
6+ line-height: 18px;
7+ margin: 33px;
8+}
9+
10+p, ol, ul, td {
11+ font-family: verdana, arial, helvetica, sans-serif;
12+ font-size: 13px;
13+ line-height: 18px;
14+ margin: 33px;
15+}
16+
17+pre {
18+ background-color: #eee;
19+ padding: 10px;
20+ font-size: 11px;
21+}
22+
23+a {
24+ color: #000;
25+
26+ &:visited {
27+ color: #666;
28+ }
29+
30+ &:hover {
31+ color: #fff;
32+ background-color: #000;
33+ }
34+}
35+
36+th {
37+ padding-bottom: 5px;
38+}
39+
40+td {
41+ padding-bottom: 7px;
42+ padding-left: 5px;
43+ padding-right: 5px;
44+}
45+
46+div {
47+ &.field, &.actions {
48+ margin-bottom: 10px;
49+ }
50+}
51+
52+#notice {
53+ color: green;
54+}
55+
56+.field_with_errors {
57+ padding: 2px;
58+ background-color: red;
59+ display: table;
60+}
61+
62+#error_explanation {
63+ width: 450px;
64+ border: 2px solid red;
65+ padding: 7px;
66+ padding-bottom: 0;
67+ margin-bottom: 20px;
68+ background-color: #f0f0f0;
69+
70+ h2 {
71+ text-align: left;
72+ font-weight: bold;
73+ padding: 5px 5px 5px 15px;
74+ font-size: 12px;
75+ margin: -7px;
76+ margin-bottom: 0;
77+ background-color: #c00;
78+ color: #fff;
79+ }
80+
81+ ul li {
82+ font-size: 12px;
83+ list-style: square;
84+ }
85+}
86+
87+label {
88+ display: block;
89+}
--- /dev/null
+++ b/app/controllers/dist_signals_controller.rb
@@ -0,0 +1,47 @@
1+class DistSignalsController < ApplicationController
2+ before_action :set_dist_signal, only: [:show, :edit, :update, :destroy]
3+
4+ respond_to :html
5+
6+ def index
7+ @dist_signals = DistSignal.all.page(params[:page])
8+ respond_with(@dist_signals)
9+ end
10+
11+ def show
12+ respond_with(@dist_signal)
13+ end
14+
15+ def new
16+ @dist_signal = DistSignal.new
17+ respond_with(@dist_signal)
18+ end
19+
20+ def edit
21+ end
22+
23+ def create
24+ @dist_signal = DistSignal.new(dist_signal_params)
25+ @dist_signal.save
26+ respond_with(@dist_signal)
27+ end
28+
29+ def update
30+ @dist_signal.update(dist_signal_params)
31+ respond_with(@dist_signal)
32+ end
33+
34+ def destroy
35+ @dist_signal.destroy
36+ respond_with(@dist_signal)
37+ end
38+
39+ private
40+ def set_dist_signal
41+ @dist_signal = DistSignal.find(params[:id])
42+ end
43+
44+ def dist_signal_params
45+ params.require(:dist_signal).permit(:signature, :link, :content_type, :body)
46+ end
47+end
--- a/app/controllers/subscribe_controller.rb
+++ b/app/controllers/subscribe_controller.rb
@@ -1,4 +1,5 @@
11 class SubscribeController < ApplicationController
2+ skip_before_action :verify_authenticity_token
23 def verify
34 _log :info, "Verify request: #{params.inspect}"
45
@@ -18,7 +19,8 @@ class SubscribeController < ApplicationController
1819 def distribute
1920 req_body = request.body.read
2021 if req_body.blank?
21- _log :warn, "Empty body for distribute request!"
22+ _log :warn, "Empty body for distribute request! Skip all"
23+ return head 200
2224 end
2325
2426 sig = request.headers['X-Hub-Signature']
@@ -28,6 +30,10 @@ class SubscribeController < ApplicationController
2830 #hmac = OpenSSL::HMAC::hexdigest(OpenSSL::Digest::SHA1.new, secret, req_body)
2931 end
3032
33+ DistSignal.create! signature: sig, body: req_body,
34+ content_type: request.headers['Content-Type'],
35+ link: request.headers['Link']
36+
3137 head 200
3238 end
3339 end
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,2 +1,7 @@
11 module ApplicationHelper
2+ def bs_icon(sym, opts = {})
3+ opts[:class] ||= ""
4+ opts[:class] += " glyphicon glyphicon-#{sym}"
5+ content_tag :span, ''.html_safe, opts
6+ end
27 end
--- /dev/null
+++ b/app/helpers/dist_signals_helper.rb
@@ -0,0 +1,2 @@
1+module DistSignalsHelper
2+end
--- /dev/null
+++ b/app/helpers/resource_helper.rb
@@ -0,0 +1,45 @@
1+module ResourceHelper
2+ def res_link_show(obj, opts = {})
3+ if defined? can?
4+ can? :show, obj or return ''
5+ end
6+ label = bs_icon :search, title: t('res_actions.show', default: 'Show')
7+ if {show_text_label: true}.merge(opts)[:show_text_label]
8+ label += t('res_actions.show')
9+ end
10+ link_to label, obj, class: 'ar-action btn btn-default btn-xs'
11+ end
12+
13+ def res_link_edit(obj, opts = {})
14+ if defined? can?
15+ can? :update, obj or return ''
16+ end
17+ label = bs_icon :edit, title: t('res_actions.edit', default: 'Edit')
18+ if {show_text_label: true}.merge(opts)[:show_text_label]
19+ label += t('res_actions.edit')
20+ end
21+ link_to label, edit_polymorphic_path(obj), class: 'ar-action btn btn-default btn-xs'
22+ end
23+
24+ def res_actions(obj, opts = {})
25+ opts = {:actions => [:edit, :destroy], :show_text_label => true}.merge opts
26+ btns = []
27+ opts[:actions].member? :show and
28+ btns.push res_link_show(obj, opts)
29+ opts[:actions].member? :edit and
30+ btns.push res_link_edit(obj, opts)
31+ opts[:actions].member? :destroy and
32+ btns.push res_link_destroy(obj, opts)
33+ content_tag :span, btns.join(' ').html_safe, class: 'ar-actions'
34+ end
35+
36+ def res_label(obj, field)
37+ t "activerecord.attributes.#{(Class === obj.class ? obj.name : obj.class.name).underscore}.#{field}"
38+ end
39+
40+ def res_name(obj)
41+ obj or return ''
42+ obj.respond_to?(:name) and return obj.name
43+ "#{t "activerecord.objs.#{obj.class.name.underscore}"} ##{obj.id}"
44+ end
45+end
--- /dev/null
+++ b/app/models/dist_signal.rb
@@ -0,0 +1,4 @@
1+class DistSignal < ApplicationRecord
2+ default_scope -> { order(id: :desc) }
3+ enum status: {new: 0, complete: 1, failed: 2, progress: 3, ignored: 4}, _prefix: true
4+end
--- /dev/null
+++ b/app/views/dist_signals/_dist_signal.json.jbuilder
@@ -0,0 +1,2 @@
1+json.extract! dist_signal, :id, :signature, :link, :content_type, :body, :created_at, :updated_at
2+json.url dist_signal_url(dist_signal, format: :json)
--- /dev/null
+++ b/app/views/dist_signals/_form.html.haml
@@ -0,0 +1,11 @@
1+= simple_form_for(@dist_signal) do |f|
2+ = f.error_notification
3+
4+ .form-inputs
5+ = f.input :signature
6+ = f.input :link
7+ = f.input :content_type
8+ = f.input :body
9+
10+ .form-actions
11+ = f.button :submit
--- /dev/null
+++ b/app/views/dist_signals/edit.html.haml
@@ -0,0 +1,7 @@
1+%h1 Editing dist_signal
2+
3+= render 'form'
4+
5+= link_to 'Show', @dist_signal
6+\|
7+= link_to 'Back', dist_signals_path
--- /dev/null
+++ b/app/views/dist_signals/index.html.haml
@@ -0,0 +1,27 @@
1+%h1 Listing dist_signals
2+
3+= paginate @dist_signals
4+
5+%table.table
6+ %thead
7+ %tr
8+ %th Status
9+ %th Signature
10+ %th Link
11+ %th Content type
12+ %th Body
13+ %th
14+
15+ %tbody
16+ - @dist_signals.each do |dist_signal|
17+ %tr
18+ %td= dist_signal.status
19+ %td= dist_signal.signature
20+ %td= dist_signal.link
21+ %td= dist_signal.content_type
22+ %td= dist_signal.body.truncate(80)
23+ %td= res_actions dist_signal, actions: %i(show)
24+
25+= paginate @dist_signals
26+
27+//= link_to 'New Dist signal', new_dist_signal_path
--- /dev/null
+++ b/app/views/dist_signals/index.json.jbuilder
@@ -0,0 +1 @@
1+json.array! @dist_signals, partial: 'dist_signals/dist_signal', as: :dist_signal
--- /dev/null
+++ b/app/views/dist_signals/new.html.haml
@@ -0,0 +1,5 @@
1+%h1 New dist_signal
2+
3+= render 'form'
4+
5+= link_to 'Back', dist_signals_path
--- /dev/null
+++ b/app/views/dist_signals/show.html.haml
@@ -0,0 +1,21 @@
1+%p#notice= notice
2+
3+%p
4+ %b Status:
5+ = @dist_signal.status
6+%p
7+ %b Signature:
8+ = @dist_signal.signature
9+%p
10+ %b Link:
11+ = @dist_signal.link
12+%p
13+ %b Content type:
14+ = @dist_signal.content_type
15+%p
16+ %b Body:
17+ = simple_format @dist_signal.body
18+
19+//= link_to 'Edit', edit_dist_signal_path(@dist_signal)
20+\|
21+= link_to 'Back', dist_signals_path
--- /dev/null
+++ b/app/views/dist_signals/show.json.jbuilder
@@ -0,0 +1 @@
1+json.partial! "dist_signals/dist_signal", dist_signal: @dist_signal
--- a/app/views/layouts/application.html.haml
+++ b/app/views/layouts/application.html.haml
@@ -21,14 +21,13 @@
2121 = link_to t('site_title'), root_path, class: "navbar-brand"
2222 .collapse.navbar-collapse
2323 %ul.nav.navbar-nav
24- - if signed_in?
24+ - if false # signed_in?
2525 = render "layouts/topnav"
2626 // %li.visible-xs= link_to t('logout'), logout_path
2727 - else
2828 // %li.visible-xs= link_to t('login'), login_path
2929 %ul.nav.navbar-nav.pull-right.hidden-xs
30- /-
31- - if signed_in?
30+ - if false # signed_in?
3231 %li.dropdown
3332 %a.dropdown-toggle{href: '#', data: {toggle: 'dropdown'}}
3433 //= current_user.name
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2,5 +2,8 @@ Rails.application.routes.draw do
22 get 'sub', to: 'subscribe#verify'
33 post 'sub', to: 'subscribe#distribute'
44
5+ resources :dist_signals, only: %i(index show)
56 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
7+
8+ root 'dummy_error_controller#index'
69 end
--- /dev/null
+++ b/db/migrate/20170302111148_create_dist_signals.rb
@@ -0,0 +1,14 @@
1+class CreateDistSignals < ActiveRecord::Migration[5.0]
2+ def change
3+ create_table :dist_signals do |t|
4+ t.string :signature
5+ t.string :link
6+ t.string :content_type
7+ t.integer :status, default: 0, null: false
8+ t.text :body
9+
10+ t.timestamps
11+ end
12+ add_index :dist_signals, :status
13+ end
14+end
--- /dev/null
+++ b/db/schema.rb
@@ -0,0 +1,26 @@
1+# This file is auto-generated from the current state of the database. Instead
2+# of editing this file, please use the migrations feature of Active Record to
3+# incrementally modify your database, and then regenerate this schema definition.
4+#
5+# Note that this schema.rb definition is the authoritative source for your
6+# database schema. If you need to create the application database on another
7+# system, you should be using db:schema:load, not running all the migrations
8+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
9+# you'll amass, the slower it'll run and the greater likelihood for issues).
10+#
11+# It's strongly recommended that you check this file into your version control system.
12+
13+ActiveRecord::Schema.define(version: 20170302111148) do
14+
15+ create_table "dist_signals", force: :cascade do |t|
16+ t.string "signature"
17+ t.string "link"
18+ t.string "content_type"
19+ t.integer "status", default: 0, null: false
20+ t.text "body"
21+ t.datetime "created_at", null: false
22+ t.datetime "updated_at", null: false
23+ t.index ["status"], name: "index_dist_signals_on_status"
24+ end
25+
26+end
--- /dev/null
+++ b/spec/controllers/dist_signals_controller_spec.rb
@@ -0,0 +1,159 @@
1+require 'rails_helper'
2+
3+# This spec was generated by rspec-rails when you ran the scaffold generator.
4+# It demonstrates how one might use RSpec to specify the controller code that
5+# was generated by Rails when you ran the scaffold generator.
6+#
7+# It assumes that the implementation code is generated by the rails scaffold
8+# generator. If you are using any extension libraries to generate different
9+# controller code, this generated spec may or may not pass.
10+#
11+# It only uses APIs available in rails and/or rspec-rails. There are a number
12+# of tools you can use to make these specs even more expressive, but we're
13+# sticking to rails and rspec-rails APIs to keep things simple and stable.
14+#
15+# Compared to earlier versions of this generator, there is very limited use of
16+# stubs and message expectations in this spec. Stubs are only used when there
17+# is no simpler way to get a handle on the object needed for the example.
18+# Message expectations are only used when there is no simpler way to specify
19+# that an instance is receiving a specific message.
20+
21+RSpec.describe DistSignalsController, type: :controller do
22+
23+ # This should return the minimal set of attributes required to create a valid
24+ # DistSignal. As you add validations to DistSignal, be sure to
25+ # adjust the attributes here as well.
26+ let(:valid_attributes) {
27+ skip("Add a hash of attributes valid for your model")
28+ }
29+
30+ let(:invalid_attributes) {
31+ skip("Add a hash of attributes invalid for your model")
32+ }
33+
34+ # This should return the minimal set of values that should be in the session
35+ # in order to pass any filters (e.g. authentication) defined in
36+ # DistSignalsController. Be sure to keep this updated too.
37+ let(:valid_session) { {} }
38+
39+ describe "GET #index" do
40+ it "assigns all dist_signals as @dist_signals" do
41+ dist_signal = DistSignal.create! valid_attributes
42+ get :index, params: {}, session: valid_session
43+ expect(assigns(:dist_signals)).to eq([dist_signal])
44+ end
45+ end
46+
47+ describe "GET #show" do
48+ it "assigns the requested dist_signal as @dist_signal" do
49+ dist_signal = DistSignal.create! valid_attributes
50+ get :show, params: {id: dist_signal.to_param}, session: valid_session
51+ expect(assigns(:dist_signal)).to eq(dist_signal)
52+ end
53+ end
54+
55+ describe "GET #new" do
56+ xit "assigns a new dist_signal as @dist_signal" do
57+ get :new, params: {}, session: valid_session
58+ expect(assigns(:dist_signal)).to be_a_new(DistSignal)
59+ end
60+ end
61+
62+ describe "GET #edit" do
63+ xit "assigns the requested dist_signal as @dist_signal" do
64+ dist_signal = DistSignal.create! valid_attributes
65+ get :edit, params: {id: dist_signal.to_param}, session: valid_session
66+ expect(assigns(:dist_signal)).to eq(dist_signal)
67+ end
68+ end
69+
70+ describe "POST #create" do
71+ context "with valid params" do
72+ xit "creates a new DistSignal" do
73+ expect {
74+ post :create, params: {dist_signal: valid_attributes}, session: valid_session
75+ }.to change(DistSignal, :count).by(1)
76+ end
77+
78+ xit "assigns a newly created dist_signal as @dist_signal" do
79+ post :create, params: {dist_signal: valid_attributes}, session: valid_session
80+ expect(assigns(:dist_signal)).to be_a(DistSignal)
81+ expect(assigns(:dist_signal)).to be_persisted
82+ end
83+
84+ xit "redirects to the created dist_signal" do
85+ post :create, params: {dist_signal: valid_attributes}, session: valid_session
86+ expect(response).to redirect_to(DistSignal.last)
87+ end
88+ end
89+
90+ context "with invalid params" do
91+ xit "assigns a newly created but unsaved dist_signal as @dist_signal" do
92+ post :create, params: {dist_signal: invalid_attributes}, session: valid_session
93+ expect(assigns(:dist_signal)).to be_a_new(DistSignal)
94+ end
95+
96+ xit "re-renders the 'new' template" do
97+ post :create, params: {dist_signal: invalid_attributes}, session: valid_session
98+ expect(response).to render_template("new")
99+ end
100+ end
101+ end
102+
103+ describe "PUT #update" do
104+ context "with valid params" do
105+ let(:new_attributes) {
106+ skip("Add a hash of attributes valid for your model")
107+ }
108+
109+ xit "updates the requested dist_signal" do
110+ dist_signal = DistSignal.create! valid_attributes
111+ put :update, params: {id: dist_signal.to_param, dist_signal: new_attributes}, session: valid_session
112+ dist_signal.reload
113+ skip("Add assertions for updated state")
114+ end
115+
116+ xit "assigns the requested dist_signal as @dist_signal" do
117+ dist_signal = DistSignal.create! valid_attributes
118+ put :update, params: {id: dist_signal.to_param, dist_signal: valid_attributes}, session: valid_session
119+ expect(assigns(:dist_signal)).to eq(dist_signal)
120+ end
121+
122+ xit "redirects to the dist_signal" do
123+ dist_signal = DistSignal.create! valid_attributes
124+ put :update, params: {id: dist_signal.to_param, dist_signal: valid_attributes}, session: valid_session
125+ expect(response).to redirect_to(dist_signal)
126+ end
127+ end
128+
129+ context "with invalid params" do
130+ xit "assigns the dist_signal as @dist_signal" do
131+ dist_signal = DistSignal.create! valid_attributes
132+ put :update, params: {id: dist_signal.to_param, dist_signal: invalid_attributes}, session: valid_session
133+ expect(assigns(:dist_signal)).to eq(dist_signal)
134+ end
135+
136+ xit "re-renders the 'edit' template" do
137+ dist_signal = DistSignal.create! valid_attributes
138+ put :update, params: {id: dist_signal.to_param, dist_signal: invalid_attributes}, session: valid_session
139+ expect(response).to render_template("edit")
140+ end
141+ end
142+ end
143+
144+ describe "DELETE #destroy" do
145+ xit "destroys the requested dist_signal" do
146+ dist_signal = DistSignal.create! valid_attributes
147+ expect {
148+ delete :destroy, params: {id: dist_signal.to_param}, session: valid_session
149+ }.to change(DistSignal, :count).by(-1)
150+ end
151+
152+ xit "redirects to the dist_signals list" do
153+ dist_signal = DistSignal.create! valid_attributes
154+ delete :destroy, params: {id: dist_signal.to_param}, session: valid_session
155+ expect(response).to redirect_to(dist_signals_url)
156+ end
157+ end
158+
159+end
--- a/spec/controllers/subscribe_controller_spec.rb
+++ b/spec/controllers/subscribe_controller_spec.rb
@@ -34,5 +34,29 @@ RSpec.describe SubscribeController, type: :controller do
3434 post :distribute
3535 expect(response).to have_http_status(:success)
3636 end
37+
38+ it "dose not create DistSignal instance with empty body" do
39+ id1 = DistSignal.reorder(id: :desc).last.try(:id)
40+ post :distribute
41+ id2 = DistSignal.reorder(id: :desc).last.try(:id)
42+ expect(id1).to eq id2
43+ end
44+
45+ it "creates DistSignal instance with body" do
46+ sig = "sig-" + SecureRandom.hex(32)
47+ body = "body-" + SecureRandom.hex(32)
48+ link = "link-" + SecureRandom.hex(32)
49+ @request.headers['Content-Type'] = 'application/atom+xml'
50+ @request.headers['Link'] = link
51+ @request.headers['X-Hub-Signature'] = sig
52+ post :distribute, body: body
53+ expect(response).to have_http_status(:success)
54+
55+ ds = DistSignal.reorder(id: :desc).last
56+ expect(ds.body).to eq body
57+ expect(ds.content_type).to eq 'application/atom+xml'
58+ expect(ds.signature).to eq sig
59+ expect(ds.link).to eq link
60+ end
3761 end
3862 end
--- /dev/null
+++ b/spec/factories/dist_signals.rb
@@ -0,0 +1,8 @@
1+FactoryGirl.define do
2+ factory :dist_signal do
3+ signature "MyString"
4+ link "MyString"
5+ content_type "MyString"
6+ body "MyText"
7+ end
8+end
--- /dev/null
+++ b/spec/models/dist_signal_spec.rb
@@ -0,0 +1,5 @@
1+require 'rails_helper'
2+
3+RSpec.describe DistSignal, type: :model do
4+ pending "add some examples to (or delete) #{__FILE__}"
5+end
--- /dev/null
+++ b/spec/routing/dist_signals_routing_spec.rb
@@ -0,0 +1,39 @@
1+require "rails_helper"
2+
3+RSpec.describe DistSignalsController, type: :routing do
4+ describe "routing" do
5+
6+ it "routes to #index" do
7+ expect(:get => "/dist_signals").to route_to("dist_signals#index")
8+ end
9+
10+ xit "routes to #new" do
11+ expect(:get => "/dist_signals/new").to route_to("dist_signals#new")
12+ end
13+
14+ it "routes to #show" do
15+ expect(:get => "/dist_signals/1").to route_to("dist_signals#show", :id => "1")
16+ end
17+
18+ xit "routes to #edit" do
19+ expect(:get => "/dist_signals/1/edit").to route_to("dist_signals#edit", :id => "1")
20+ end
21+
22+ xit "routes to #create" do
23+ expect(:post => "/dist_signals").to route_to("dist_signals#create")
24+ end
25+
26+ xit "routes to #update via PUT" do
27+ expect(:put => "/dist_signals/1").to route_to("dist_signals#update", :id => "1")
28+ end
29+
30+ xit "routes to #update via PATCH" do
31+ expect(:patch => "/dist_signals/1").to route_to("dist_signals#update", :id => "1")
32+ end
33+
34+ xit "routes to #destroy" do
35+ expect(:delete => "/dist_signals/1").to route_to("dist_signals#destroy", :id => "1")
36+ end
37+
38+ end
39+end