0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails: rake taskを使ってアフィリエイトリンク生成

Posted at

やりたいこと

取得してきたリンクをアフィリエイトリンクに変える。

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?