Class: ItunesAffiliate::ItunesLink

Inherits:
Object
  • Object
show all
Defined in:
lib/itunes_affiliate/itunes_link.rb

Constant Summary collapse

Partners =

The list of possible affiliate parters, as determined by the store.

[:linkshare,:linkshare_japan,:tradedoubler,:dgm].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_link) ⇒ ItunesLink

Initializes a new instance of the ItunesLink class

Parameters:

  • source_link (String)

    the source link, which should be a valid clean itunes link

Raises:

  • (ArgumentException)

    raised when source_link is nil



13
14
15
16
# File 'lib/itunes_affiliate/itunes_link.rb', line 13

def initialize(source_link)
  raise ArgumentException "You need to provide a source link" unless source_link 
  @source_link = source_link
end

Class Method Details

.is_valid_link?(link) ⇒ Boolean

Determines if the link is a valid itunes clean url.

Parameters:

  • link (String)

    the link that should be checked.

Returns:

  • (Boolean)

    true if the link is not nil and has the host itunes.apple.com, otherwise false



40
41
42
43
44
# File 'lib/itunes_affiliate/itunes_link.rb', line 40

def self.is_valid_link?(link)
  uri = URI.parse(link) rescue nil
  
  uri != nil && uri.host != nil && uri.host.downcase == "itunes.apple.com"
end

Instance Method Details

Returns an affiliate link

Parameters:

  • partner (Symbol)

    the partner, as determined by the store. Valid values are :linkshare,:linkshare_japan,:tradedoubler,:dgm

Returns:

  • (String)

    a modified link containing the affiliate information, or the original link if the link is not an itunes clean link.

Raises:

  • (ArgumentException)

    raised when the partner is not a valid symbol



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/itunes_affiliate/itunes_link.rb', line 22

def affiliate_link(partner)
  case partner
    when :linkshare
      append_to_link @source_link, "&partnerId=#{ItunesAffiliate.config.linkshare_partner_id}&siteID=#{ItunesAffiliate.config.linkshare_key}"
    when :linkshare_japan
      append_to_link @source_link, "&partnerId=#{ItunesAffiliate.config.linkshare_japan_partner_id}&siteID=#{ItunesAffiliate.config.linkshare_japan_key}"
    when :tradedoubler
      append_to_link @source_link, "&partnerId=#{ItunesAffiliate.config.tradedoubler_partner_id}&tduid=#{ItunesAffiliate.config.tradedoubler_key}"
    when :dgm
      append_to_link @source_link, "&partnerId=#{ItunesAffiliate.config.dgm_partner_id}&affToken=#{ItunesAffiliate.config.dgm_key}"
    else
      raise ArgumentException "Unrecognized partner #{partner} must be one of #{Partners}"
  end
end