Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b57d7ff
make api routes
HashimotoLogly Jun 4, 2019
9fc95ea
add imp,click counter on api_controller
HashimotoLogly Jun 4, 2019
159cd36
add errorlog if ad.id was matched nothing
HashimotoLogly Jun 4, 2019
ac3f826
fix Db tables
HashimotoLogly Jun 14, 2019
a543d86
add api html.erb
HashimotoLogly Jun 4, 2019
adfd2ca
fix ad_api
HashimotoLogly Jun 21, 2019
0fa59bc
fix model name
HashimotoLogly Jun 21, 2019
592f6e1
fix db
HashimotoLogly Jun 21, 2019
fc6c62b
fix model name
HashimotoLogly Jun 21, 2019
3675ac0
autocorrected by rubocop
HashimotoLogly Jun 21, 2019
1de31b3
fix model saving
HashimotoLogly Jun 21, 2019
792c80f
add comment
HashimotoLogly Jun 21, 2019
9f42631
change totalprice to price
HashimotoLogly Jun 21, 2019
a8952d5
fix db column type
HashimotoLogly Jun 24, 2019
53645a0
add travel_to method for testing rspec
HashimotoLogly Jun 24, 2019
7445b15
add view rspec
HashimotoLogly Jun 24, 2019
69841ea
add click rspec
HashimotoLogly Jun 25, 2019
26e04f9
add_ media.site.htm
HashimotoLogly Jun 27, 2019
7296024
change action name(view,click)
HashimotoLogly Jun 27, 2019
39d598f
add rspec
HashimotoLogly Jun 27, 2019
299dc5a
rubocop --auto-correct
HashimotoLogly Jun 27, 2019
c2c9734
change html name corresponding to action names
HashimotoLogly Jun 27, 2019
0a6a8c2
delete unused files
HashimotoLogly Jun 27, 2019
7b9e86a
rename files
HashimotoLogly Jun 27, 2019
11e2cc2
delete unused files
HashimotoLogly Jun 27, 2019
cf3dc25
delete comment
HashimotoLogly Jun 27, 2019
2e9eefd
fix action names
HashimotoLogly Jun 28, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ gem 'carrierwave'
gem 'sassc'
gem 'rubocop', '~> 0.47.1'
gem 'htmlbeautifier'
gem 'activerecord-import'
19 changes: 11 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ GEM
activemodel (= 5.2.3)
activesupport (= 5.2.3)
arel (>= 9.0)
activerecord-import (1.0.2)
activerecord (>= 3.2)
activestorage (5.2.3)
actionpack (= 5.2.3)
activerecord (= 5.2.3)
Expand Down Expand Up @@ -102,7 +104,7 @@ GEM
io-like (0.3.0)
jbuilder (2.9.1)
activesupport (>= 4.2.0)
jquery-rails (4.3.3)
jquery-rails (4.3.5)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
Expand All @@ -125,7 +127,7 @@ GEM
mini_mime (1.0.1)
mini_portile2 (2.4.0)
minitest (5.11.3)
msgpack (1.2.10)
msgpack (1.3.0)
nio4r (2.3.1)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
Expand Down Expand Up @@ -173,12 +175,12 @@ GEM
rb-inotify (0.10.0)
ffi (~> 1.0)
regexp_parser (1.5.1)
rspec-core (3.8.0)
rspec-core (3.8.1)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.3)
rspec-expectations (3.8.4)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
rspec-mocks (3.8.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-rails (3.8.2)
Expand All @@ -189,7 +191,7 @@ GEM
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rspec-support (3.8.2)
rubocop (0.47.1)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
Expand Down Expand Up @@ -245,16 +247,17 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
websocket-driver (0.7.0)
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
websocket-extensions (0.1.4)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
activerecord-import
bootsnap (>= 1.1.0)
bootstrap
byebug
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/ad_api.coffee
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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/ad_api.scss
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/
39 changes: 39 additions & 0 deletions app/controllers/ad_api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true
class AdApiController < ApplicationController
def get_ads
array = []
Copy link

Choose a reason for hiding this comment

The 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 update_clicks
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

3 changes: 3 additions & 0 deletions app/helpers/ad_api_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true
module AdApiHelper
end
3 changes: 3 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true
class Report < ApplicationRecord
end
6 changes: 3 additions & 3 deletions app/views/ad/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<%= image_tag ad.image.to_s %>
</li>
<div class="edit_button">
<%= link_to("Edit", edit_ad_path(ad)) %>
<%= link_to("変更", edit_ad_path(ad)) %>
</div>
<div class="delete_button">
<%= link_to("Delete", ad_path(ad) ,method: :delete) %>
<%= link_to("削除", ad_path(ad) ,method: :delete) %>
</div>
<% end %>
<div>
<%= link_to("New", new_ad_path) %>
<%= link_to("新規作成", new_ad_path) %>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions app/views/ad_api/update_clicks.erb
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>
2 changes: 2 additions & 0 deletions app/views/ad_api/view_make_report.html.erb
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>
3 changes: 2 additions & 1 deletion config/routes.rb
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#get_ads'
get '/click' => 'ad_api#update_clicks'
resources :ad
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
4 changes: 2 additions & 2 deletions db/migrate/20190528085221_create_ads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
class CreateAds < ActiveRecord::Migration[5.2]
def change
create_table :ads do |t|
t.integer :advertiser_id, null: false, default: 0 # 広告主ID
t.integer :advertiser_id, null: false # 広告主ID
t.string :image, null: false, default: '' # 広告の画像URL
t.integer :price, null: false, default: 0 # 広告の価格
t.integer :price, null: false # 広告の価格
t.string :text, null: false, default: '' # 広告の説明文

t.timestamps
Expand Down
8 changes: 0 additions & 8 deletions db/migrate/20190604073026_add_report_column_to_ads.rb

This file was deleted.

15 changes: 15 additions & 0 deletions db/migrate/20190614041856_create_repos.rb
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
21 changes: 15 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_06_04_073026) do
ActiveRecord::Schema.define(version: 2019_06_14_041856) do
create_table 'ads', force: :cascade do |t|
t.integer 'advertiser_id', default: 0, null: false
t.integer 'advertiser_id', null: false
t.string 'image', default: '', null: false
t.integer 'price', default: 0, null: false
t.integer 'price', null: false
t.string 'text', default: '', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.integer 'click'
t.integer 'imp'
t.integer 'cv'
end

create_table 'reports', force: :cascade do |t|
t.integer 'ad_id', null: false
t.integer 'adspot_id', null: false
t.integer 'click', default: 0, null: false
t.integer 'imp', default: 0, null: false
t.integer 'cv', default: 0, null: false
t.integer 'price', default: 0, null: false
t.date 'date', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end
end
58 changes: 58 additions & 0 deletions public/media.site.htm
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>
Binary file added public/uploads/ad/image/2/bg_topicon_p04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions spec/controllers/ad_api_controller_spec.rb
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 #get_ads' do
it 'returns http success with valid params' do
get :get_ads, 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 :get_ads, params: { adspot_id: 1, count: 1 } }.to change(Report, :count).by(1)
end

it "record 'imp' should be increased" do
get :get_ads, 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 :get_ads, params: { adspot_id: 100, count: 1 }
expect { get :get_ads, params: { adspot_id: 100, count: 1 } }.to change(Report, :count).by(0)
end

it 'an ad should be recorded from other adspots' do
get :get_ads, params: { adspot_id: 100, count: 1 }
expect { get :get_ads, 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 :get_ads, 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 :get_ads, params: { adspot_id: 101, count: 1 }
travel 1.day do
expect { get :get_ads, 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 :get_ads, 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
Copy link

Choose a reason for hiding this comment

The 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 #update_clicks' do
before do
@ad = FactoryBot.create(:ad)
get :get_ads, params: { adspot_id: 1, count: 1 }
end

it 'returns http success' do
get :update_clicks, params: { ad_id: @ad.id, adspot_id: 1 }
expect(response).to have_http_status(:success)
end

describe 'Report' do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • describeの囲いは統一してほしい
  • if, unlessで別れていたら都度テストケースを書いてほしい

it 'an ad should be recorded ' do
expect { get :update_clicks, params: { ad_id: @ad.id, adspot_id: 1 } }.to change(Report, :count).by(0)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/controllers/ad_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

context 'when something empty' do
it 'without text should be rendered' do
patch :update, params: { ad: { 'text' =>nil }, id: @ad.id }
patch :update, params: { ad: { 'text' => nil }, id: @ad.id }
expect(response).to render_template :edit
end

Expand Down
Loading