-
Notifications
You must be signed in to change notification settings - Fork 0
Feature web api #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 26 commits
b57d7ff
9fc95ea
159cd36
ac3f826
a543d86
adfd2ca
0fa59bc
592f6e1
fc6c62b
3675ac0
1de31b3
792c80f
9f42631
a8952d5
53645a0
7445b15
69841ea
26e04f9
7296024
39d598f
299dc5a
c2c9734
0a6a8c2
7b9e86a
11e2cc2
cf3dc25
2e9eefd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,3 +70,4 @@ gem 'carrierwave' | |
| gem 'sassc' | ||
| gem 'rubocop', '~> 0.47.1' | ||
| gem 'htmlbeautifier' | ||
| gem 'activerecord-import' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Place all the behaviors and hooks related to the matching controller here. | ||
| # All this logic will automatically be available in application.js. | ||
| # You can use CoffeeScript in this file: http://coffeescript.org/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Place all the styles related to the Ad_API controller here. | ||
| // They will automatically be included in application.css. | ||
| // You can use Sass (SCSS) here: http://sass-lang.com/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
|
|
||
| # frozen_string_literal: true | ||
| class AdApiController < ApplicationController | ||
| def view_make_report | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 本来であれば1メソッドで1つの役割を持たせるべきで、一言で説明できないメソッドにすべきではないです。 |
||
| array = [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. arrayは変えて |
||
| reports = [] | ||
|
|
||
| target_ids = Ad.pluck(:id).sample(params[:count].to_i) | ||
| ads = Ad.find(target_ids) | ||
| ads.each do |ad| | ||
| report = Report.find_by(ad_id: ad.id, adspot_id: params[:adspot_id],date: Date.today) | ||
| unless report | ||
| report = Report.new(ad_id: ad.id, adspot_id: params[:adspot_id], date: Date.today) | ||
| end | ||
|
|
||
| report.imp += 1 | ||
| reports.push(report) | ||
|
|
||
| array.push( | ||
| img_url: ad.image.url, | ||
| body: ad.text, | ||
| ad_id: ad.id | ||
| ) | ||
| end | ||
| Report.import reports, on_duplicate_key_update: [:imp] | ||
| render json: array | ||
| end | ||
|
|
||
| def click_make_report | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. メソッド名変更で |
||
| report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id],date: Date.today) | ||
| unless report | ||
| report = Report.new(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today) | ||
| end | ||
| report.click += 1 | ||
| report.price += Ad.find(params[:ad_id]).price | ||
| report.save | ||
| end | ||
|
|
||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # frozen_string_literal: true | ||
| module AdApiHelper | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # frozen_string_literal: true | ||
| class Report < ApplicationRecord | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1>AdApi#click</h1> | ||
| <p>Find me in app/views/ad_api/click.html.erb</p> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1>AdApi#view</h1> | ||
| <p>Find me in app/views/ad_api/view.html.erb</p> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| # frozen_string_literal: true | ||
| Rails.application.routes.draw do | ||
| get '/view' => 'ad_api#view_make_report' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. routing変更 |
||
| get '/click' => 'ad_api#click_make_report' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. routing変更 |
||
| resources :ad | ||
| # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
| end | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
| class CreateRepos < ActiveRecord::Migration[5.2] | ||
| def change | ||
| create_table :reports do |t| | ||
| t.integer :ad_id, null: false | ||
| t.integer :adspot_id, null: false | ||
| t.integer :click, null: false, default: 0 | ||
| t.integer :imp, null: false, default: 0 | ||
| t.integer :cv, null: false, default: 0 | ||
| t.integer :price, null: false, default: 0 | ||
| t.date :date, null: false, unique: true | ||
| t.timestamps | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>媒体TEST</title> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
| <style> | ||
| .display {} | ||
| .display_img { width: 80px; height: 80px; } | ||
| .display_link { color: #000000; } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div id="display"> | ||
|
|
||
| </div> | ||
| <script> | ||
| // ここで自分の自分のテストパラメーターを入れてください。 | ||
| site_prefix = "http://localhost:3000/"; | ||
| adspot_id = 102; | ||
| count = 2; | ||
| window.onload = function(){ | ||
| call_api() | ||
| } | ||
| function call_api(){ | ||
| var xhr = new XMLHttpRequest(); | ||
| xhr.open("GET", site_prefix + "view?adspot_id=" + adspot_id + "&count=" + count, true); | ||
| xhr.onload = function(){ | ||
| displayAd = function(ad){ | ||
| t = document.getElementById("display"); | ||
| ad_node = document.createElement("DIV"); | ||
| link_node = document.createElement("A"); | ||
| href_attr = document.createAttribute("HREF"); | ||
| href_attr.value = site_prefix + "click?adspot_id=" + adspot_id + "&ad_id=" + ad.ad_id; | ||
| class_attr = document.createAttribute("CLASS"); | ||
| class_attr.value = "display_link"; | ||
| link_node.setAttributeNode(href_attr); | ||
| link_node.setAttributeNode(class_attr); | ||
| image_node = document.createElement("IMG"); | ||
| src_attr = document.createAttribute("SRC"); | ||
| src_attr.value = ad.img_url; | ||
| class_attr = document.createAttribute("CLASS"); | ||
| class_attr.value = "display_img" | ||
| image_node.setAttributeNode(src_attr); | ||
| image_node.setAttributeNode(class_attr); | ||
| link_node.appendChild(image_node); | ||
| link_node.appendChild(document.createTextNode(ad.body)); | ||
| ad_node.appendChild(link_node); | ||
| t.appendChild(ad_node) | ||
| console.log(ad); | ||
| } | ||
| ads = JSON.parse(xhr.responseText); | ||
| ads.forEach(ad => displayAd(ad)); | ||
| } | ||
| xhr.send(); | ||
| } | ||
| </script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # frozen_string_literal: true | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe AdApiController, type: :controller do | ||
| before do | ||
| @ad1 = FactoryBot.create(:ad) | ||
| end | ||
|
|
||
| describe 'GET #view_make_report' do | ||
| it 'returns http success with valid params' do | ||
| get :view_make_report, params: { adspot_id: 1, count: 1 } | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
|
|
||
| describe 'Report' do | ||
| it 'an ad should be recorded' do | ||
| expect { get :view_make_report, params: { adspot_id: 1, count: 1 } }.to change(Report, :count).by(1) | ||
| end | ||
|
|
||
| it "record 'imp' should be increased" do | ||
| get :view_make_report, params: { adspot_id: 1, count: 1 } | ||
| report = Report.find_by(ad_id: @ad1.id, adspot_id: 1, date: Date.today) | ||
| expect(report.imp).to eq 1 | ||
| end | ||
|
|
||
| it "an ad shouldn't be recorded twice from the same adspot_id on the same day" do | ||
| get :view_make_report, params: { adspot_id: 100, count: 1 } | ||
| expect { get :view_make_report, params: { adspot_id: 100, count: 1 } }.to change(Report, :count).by(0) | ||
| end | ||
|
|
||
| it 'an ad should be recorded from other adspots' do | ||
| get :view_make_report, params: { adspot_id: 100, count: 1 } | ||
| expect { get :view_make_report, params: { adspot_id: 101, count: 1 } }.to change(Report, :count).from(1).to(2) | ||
| end | ||
|
|
||
| it 'ads should be recorded at the same time' do | ||
| ad2 = FactoryBot.create(:ad) | ||
| expect { get :view_make_report, params: { adspot_id: 101, count: 2 } }.to change(Report, :count).from(0).to(2) | ||
| end | ||
|
|
||
| it 'an ad should be recorded on the other day' do | ||
| get :view_make_report, params: { adspot_id: 101, count: 1 } | ||
| travel 1.day do | ||
| expect { get :view_make_report, params: { adspot_id: 101, count: 1 } }.to change(Report, :count).from(1).to(2) | ||
| end | ||
| end | ||
|
|
||
| context 'when many ads are requested' do | ||
| it 'all ads should be visible ' do | ||
| @ad2 = FactoryBot.create(:ad) | ||
| @ad3 = FactoryBot.create(:ad) | ||
| get :view_make_report, params: { adspot_id: 1, count: 3 } | ||
| jsons = JSON.parse(response.body) | ||
|
|
||
| ad1_ispresent = false | ||
| ad2_ispresent = false | ||
| ad3_ispresent = false | ||
|
|
||
| jsons.each do |json| | ||
| if json['ad_id'] == @ad1.id | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rspecではif文は使わないで |
||
| ad1_ispresent = true | ||
| elsif json['ad_id'] == @ad2.id | ||
| ad2_ispresent = true | ||
| elsif json['ad_id'] == @ad3.id | ||
| ad3_ispresent = true | ||
| end | ||
| end | ||
| expect(ad1_ispresent).to be_truthy | ||
| expect(ad2_ispresent).to be_truthy | ||
| expect(ad3_ispresent).to be_truthy | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'GET #click_make_report' do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. メソッド名変更で |
||
| before do | ||
| @ad = FactoryBot.create(:ad) | ||
| get :view_make_report, params: { adspot_id: 1, count: 1 } | ||
| end | ||
|
|
||
| it 'returns http success' do | ||
| get :click_make_report, params: { ad_id: @ad.id, adspot_id: 1 } | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
|
|
||
| describe 'Report' do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| it 'an ad should be recorded ' do | ||
| expect { get :click_make_report, params: { ad_id: @ad.id, adspot_id: 1 } }.to change(Report, :count).by(0) | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要な行は削除しよう