-
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
Open
HashimotoLogly
wants to merge
27
commits into
master
Choose a base branch
from
feature_webAPI
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
b57d7ff
make api routes
HashimotoLogly 9fc95ea
add imp,click counter on api_controller
HashimotoLogly 159cd36
add errorlog if ad.id was matched nothing
HashimotoLogly ac3f826
fix Db tables
HashimotoLogly a543d86
add api html.erb
HashimotoLogly adfd2ca
fix ad_api
HashimotoLogly 0fa59bc
fix model name
HashimotoLogly 592f6e1
fix db
HashimotoLogly fc6c62b
fix model name
HashimotoLogly 3675ac0
autocorrected by rubocop
HashimotoLogly 1de31b3
fix model saving
HashimotoLogly 792c80f
add comment
HashimotoLogly 9f42631
change totalprice to price
HashimotoLogly a8952d5
fix db column type
HashimotoLogly 53645a0
add travel_to method for testing rspec
HashimotoLogly 7445b15
add view rspec
HashimotoLogly 69841ea
add click rspec
HashimotoLogly 26e04f9
add_ media.site.htm
HashimotoLogly 7296024
change action name(view,click)
HashimotoLogly 39d598f
add rspec
HashimotoLogly 299dc5a
rubocop --auto-correct
HashimotoLogly c2c9734
change html name corresponding to action names
HashimotoLogly 0a6a8c2
delete unused files
HashimotoLogly 7b9e86a
rename files
HashimotoLogly 11e2cc2
delete unused files
HashimotoLogly cf3dc25
delete comment
HashimotoLogly 2e9eefd
fix action names
HashimotoLogly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [] | ||
| 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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # frozen_string_literal: true | ||
| module AdApiHelper | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # frozen_string_literal: true | ||
| class Report < ApplicationRecord | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
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 #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 | ||
|
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 :update_clicks, params: { ad_id: @ad.id, adspot_id: 1 } }.to change(Report, :count).by(0) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
arrayは変えて