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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

不要な行は削除しよう

# frozen_string_literal: true
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.

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])
unless report
report = Report.new(ad_id: ad.id, adspot_id: params[:adspot_id])
end

report.imp += 1
reports.push(report)

array.push(
img_url: ad.image,
body: ad.text,
ad_id: ad.id
)
end
Report.import(reports)
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をどうするのか、明確にしたメソッド名にしてほしい

report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id])

report.click += 1
report.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.

totalcostって多分登録できてないよ

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
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/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.

14 changes: 14 additions & 0 deletions db/migrate/20190614041856_create_repos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.timestamps
end
end
end
20 changes: 14 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@
#
# 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.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.
21 changes: 21 additions & 0 deletions spec/controllers/ad_api_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe AdApiController, type: :controller do
befoere do

end
describe 'GET #view' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メソッド名を変えよう

it 'returns http success' do
get :view
expect(response).to have_http_status(:success)
end
end

describe 'GET #click' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メソッド名を変えよう

it 'returns http success' do
get :click
expect(response).to have_http_status(:success)
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
10 changes: 10 additions & 0 deletions spec/factories/repos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :report do
click { 1 }
imp { 1 }
cv { 1 }
totalcost { 1 }
adspot_id { 1 }
end
end
16 changes: 16 additions & 0 deletions spec/helpers/ad_api_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true
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
6 changes: 6 additions & 0 deletions spec/models/repo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe Report, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
6 changes: 6 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,6 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe 'ad_api/click.html.erb', type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end
6 changes: 6 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,6 @@
# frozen_string_literal: true
require 'rails_helper'

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