Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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.
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/
51 changes: 51 additions & 0 deletions app/controllers/ad_api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

不要な行は削除しよう

class AdApiController < ApplicationController
def view
Copy link
Copy Markdown

@tmk-hsn tmk-hsn Jun 21, 2019

Choose a reason for hiding this comment

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

  • view はメソッド名としてよくない(viewsというディレクトリが存在するので初見の人が混乱する)ので、register_impとかに変えてほしい

array=[]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

イコールの左右は空けること


target_ids = Ad.pluck(:id).sample( params[:count].to_i)
ads=Ad.find(target_ids)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

イコールの左右は空けること
同じことを言われないように注意すること

ads.each do |ad|

repo = Repo.find_by(ad_id: ad.id, adspot_id: params[:adspot_id])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名をreportに変えてほしい
(他部分も同様)

unless repo
repo = Repo.new(ad_id: ad.id,adspot_id: params[:adspot_id])
end

repo.imp += 1
repo.save
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

saveはなるべくloopの外で使おう。
DBへの書き込みがloop分だけ発生し、パフォーマンスに影響します。
具体的なやり方がわからなかったら聞いてほしい

p repo.imp

array.push(
{ img_url: ad.image,
body: ad.text,
ad_id: ad.id }
)
end
render json: array
end

def click
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

clickをどうするのか、明確にしたメソッド名にしてほしい


if repo = Repo.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

スペース2つ分空いてる?から1つ分に変更して


repo.click += 1
repo.totalcost += Ad.find( params[:ad_id]).price
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

不要スペースは削除で

repo.save

p repo.click
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

デバッグ消し忘れかな?

p repo.totalcost
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

デバッグ消し忘れかな?


else
render status: 500, json: { status: 500, message: 'Ad was not existed! ' }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

レスポンス500ではない気がするので、改めて検討してみて

end
end


private
def count_p
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

このprivateメソッド使ってる?


end


end
2 changes: 2 additions & 0 deletions app/helpers/ad_api_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AdApiHelper
end
2 changes: 2 additions & 0 deletions app/models/repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Repo < ApplicationRecord
end
2 changes: 2 additions & 0 deletions app/views/ad_api/click.html.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.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>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true
Rails.application.routes.draw do
get '/view' => 'ad_api#view'
get '/click' => 'ad_api#click'
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 @@
class CreateRepos < ActiveRecord::Migration[5.2]
def change
create_table :repos do |t|
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

カラムを以下の順番に変更して。

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • created_at, updated_atを足してほしい

t.integer :click, null: false, default: 0
t.integer :imp, null: false, default: 0
t.integer :cv, null: false, default: 0
t.integer :ad_id,null: false
t.integer :totalcost, null: false, default: 0
t.integer :adspot_id, null: false


t.timestamps
end
end
end
33 changes: 21 additions & 12 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -11,16 +10,26 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_06_04_073026) do
create_table 'ads', force: :cascade do |t|
t.integer 'advertiser_id', default: 0, null: false
t.string 'image', default: '', null: false
t.integer 'price', default: 0, 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'
ActiveRecord::Schema.define(version: 2019_06_14_041856) do

create_table "ads", force: :cascade do |t|
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

adsテーブルってここで作るんだっけ?

t.integer "advertiser_id", null: false
t.string "image", default: "", 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
end

create_table "repos", force: :cascade do |t|
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • テーブル名を変更してほしい
  • カラムの並び順を変えてほしい
  • created_at, updated_atを足してほしい

t.integer "click", default: 0, null: false
t.integer "imp", default: 0, null: false
t.integer "cv", default: 0, null: false
t.integer "ad_id", null: false
t.integer "totalcost", default: 0, null: false
t.integer "adspot_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
58 changes: 58 additions & 0 deletions 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/1/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.
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.
19 changes: 19 additions & 0 deletions spec/controllers/ad_api_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.describe AdApiController, type: :controller do

describe "GET #view" do
it "returns http success" do
get :view
expect(response).to have_http_status(:success)
end
end

describe "GET #click" do
it "returns http success" do
get :click
expect(response).to have_http_status(:success)
end
end

end
9 changes: 9 additions & 0 deletions spec/factories/repos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :repo do
click { 1 }
imp { 1 }
cv { 1 }
totalcost { 1 }
adspot_id { 1 }
end
end
15 changes: 15 additions & 0 deletions spec/helpers/ad_api_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

消してOK

# the AdApiHelper. For example:
#
# describe AdApiHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe AdApiHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/repo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Repo, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/ad_api/click.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe "ad_api/click.html.erb", type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/ad_api/view.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe "ad_api/view.html.erb", type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end