Class: Linkshare::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/linkshare/base.rb

Direct Known Subclasses

Commission, Product

Constant Summary collapse

@@credentials =
{}
@@default_params =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/linkshare/base.rb', line 11

def initialize(params)
  raise ArgumentError, "Init with a Hash; got #{params.class} instead" unless params.is_a?(Hash)

  params.each do |key, val|
    instance_variable_set("@#{key}".intern, val)
    instance_eval " class << self ; attr_reader #{key.intern.inspect} ; end "
  end
end

Instance Attribute Details

#page_numberObject (readonly)

Returns the value of attribute page_number.



6
7
8
# File 'lib/linkshare/base.rb', line 6

def page_number
  @page_number
end

#total_matchesObject (readonly)

Returns the value of attribute total_matches.



6
7
8
# File 'lib/linkshare/base.rb', line 6

def total_matches
  @total_matches
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



6
7
8
# File 'lib/linkshare/base.rb', line 6

def total_pages
  @total_pages
end

Class Method Details

.base_urlObject



29
30
31
# File 'lib/linkshare/base.rb', line 29

def base_url
  "http://cli.linksynergy.com/"
end

.credentialsObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/linkshare/base.rb', line 56

def credentials
  unless @@credentials && @@credentials.length > 0
    config_file = ["config/linkshare.yml", File.join(ENV['HOME'], '.linkshare.yaml')].select{|f| File.exist?(f)}.first

    unless File.exist?(config_file)
      warn "Warning: config/linkshare.yaml does not exist. Put your CJ developer key and website ID in ~/.linkshare.yml to enable live testing."
    else
      @@credentials = YAML.load(File.read(config_file))
    end
  end
  @@credentials
end

.first(params) ⇒ Object



73
74
75
# File 'lib/linkshare/base.rb', line 73

def first(params)
  find(params).first
end

.get_service(path, query) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/linkshare/base.rb', line 40

def get_service(path, query)
  query.keys.each{|k| query[k.to_s] = query.delete(k)}

  results = []
  begin
    # pairs = [] ; query.each_pair{|k,v| pairs << "#{k}=#{v}" } ; p "#{path}&#{pairs.join('&')}"
    response = get(path, :query => query, :timeout => 30)
  rescue Timeout::Error
    nil
  end

  raise_if_invalid_response(response)

  Crack::XML.parse(response.body)
end

.raise_if_invalid_response(response) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
# File 'lib/linkshare/base.rb', line 69

def raise_if_invalid_response(response)
  raise ArgumentError, "There was an error connecting to LinkShare's reporting server." if response.body.include?("REPORTING ERROR")
end

.validate_params!(provided_params, available_params, default_params = {}) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
# File 'lib/linkshare/base.rb', line 33

def validate_params!(provided_params, available_params, default_params = {})
  params = default_params.merge(provided_params)
  invalid_params = params.select{|k,v| !available_params.include?(k.to_s)}.map{|k,v| k}
  raise ArgumentError.new("Invalid parameters: #{invalid_params.join(', ')}") if invalid_params.length > 0
  params
end

Instance Method Details

#pass=(pass) ⇒ Object



24
25
26
# File 'lib/linkshare/base.rb', line 24

def pass=(pass)
  @@credentials['pass'] = pass.to_s
end

#user_id=(id) ⇒ Object



20
21
22
# File 'lib/linkshare/base.rb', line 20

def user_id=(id)
  @@credentials['user_id'] = id.to_s
end