やりたいこと
取得してきたリンクをアフィリエイトリンクに変える。
rake taskについて
Rakeタスクをつくるを参考。
taskとしてではなくmethodつくる
結論から言うと使い勝手の問題。methodを使えば他のmethodやtaskとも合わせて使えることができるから。
セットアップ
auto_item_create_module.rb
# app/lib
module AutoItemCreateModule
def create_affi_link_of(shop_item_link)
shop_item_link_id = shop_item_link.gsub("http://store.shopping.yahoo.co.jp/", "").gsub("/", "%2F")
affi_url = "http://ck.jp.ap.valuecommerce.com/servlet/referral?sid=3285280&pid=884218179&vc_url=http%3A%2F%2Fstore.shopping.yahoo.co.jp%2F#{shop_item_link_id}"
# ShopItem.update(url: affi_url)
p affi_url
end
end
item.rake
# lib/tasks
require 'auto_item_create_module.rb'
Module.new do
extend Rake::DSL
extend self
extend AutoItemCreateModule
namespace :item do
desc "test link affiliation task"
task :test_affiliate_link => :environment do
shop_item_links = [
"http://store.shopping.yahoo.co.jp/ishibashi/80-313326490-3892.html",
"http://store.shopping.yahoo.co.jp/ishibashi/80-313326490-3894.html",
"http://store.shopping.yahoo.co.jp/ishibashi/80-313362200-0011.html"
]
shop_item_links.each do |link|
create_affi_link_of(link)
end
end
end
end