-
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 12 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,37 @@ | ||
|
|
||
| # frozen_string_literal: true | ||
| class AdApiController < ApplicationController | ||
| def view | ||
|
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 = [] | ||
|
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]) | ||
| 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, on_duplicate_key_update: [:imp] | ||
| render json: array | ||
| end | ||
|
|
||
| def click | ||
|
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. 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 | ||
|
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. totalcostって多分登録できてないよ |
||
| 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,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 |
This file was deleted.
| 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 :totalprice, null: false, default: 0 | ||
| t.timestamps | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| create_table 'ads', force: :cascade do |t| | ||
| t.integer 'advertiser_id', default: 0, null: false | ||
| ActiveRecord::Schema.define(version: 2019_06_14_041856) do | ||
| create_table 'ads', force: :cascade do |t| # reportsテーブルのtotalpriceにpriceを加算する為に使用しているのでここでも使っています | ||
|
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. totalpriceではなく、priceで良いです。 |
||
| 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 'totalprice', default: 0, null: false | ||
| t.datetime 'created_at', null: false | ||
| t.datetime 'updated_at', null: false | ||
| 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,20 @@ | ||
| # frozen_string_literal: true | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe AdApiController, type: :controller do | ||
| befoere do | ||
| end | ||
| describe 'GET #view' 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 'returns http success' do | ||
| get :view | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
|
|
||
| describe 'GET #click' 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 'returns http success' do | ||
| get :click | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
| end | ||
| 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 |
| 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 | ||
|
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. 消して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 | ||
| 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 |
| 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 |
| 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 |
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.
不要な行は削除しよう