linkshare 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in linkshare.gemspec
4
+ gemspec
@@ -0,0 +1,18 @@
1
+ linkshare
2
+
3
+ LinkShare HTTP web service interface
4
+
5
+ Currently you will need a ~/.linkshare.yml to use this.
6
+
7
+ Retrieves data from LinkShare via HTTP web service and returns simple objects:
8
+
9
+ [Example]
10
+
11
+ Linkshare::Commission.find("bdate" =>"20110401", "edate" => "20110415")
12
+
13
+ [Example YAML]
14
+
15
+ user_id: test_user
16
+ pass: secure_password
17
+
18
+ Methods accept the same parameters as the LinkShare API.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,6 @@
1
+ require 'httparty'
2
+ require 'fastercsv'
3
+
4
+ directory = File.expand_path(File.dirname(__FILE__))
5
+ require File.join(directory, "linkshare", "base")
6
+ require File.join(directory, "linkshare", "commission")
@@ -0,0 +1,84 @@
1
+ module Linkshare
2
+ class Base
3
+ include HTTParty
4
+ format :xml
5
+
6
+ @@credentials = {}
7
+ @@default_params = {}
8
+
9
+ def initialize(params)
10
+ raise ArgumentError, "Init with a Hash; got #{params.class} instead" unless params.is_a?(Hash)
11
+
12
+ params.each do |key, val|
13
+ instance_variable_set("@#{key}".intern, val)
14
+ instance_eval " class << self ; attr_reader #{key.intern.inspect} ; end "
15
+ end
16
+ end
17
+
18
+ def user_id=(id)
19
+ @@credentials['user_id'] = id.to_s
20
+ end
21
+
22
+ def pass=(pass)
23
+ @@credentials['pass'] = pass.to_s
24
+ end
25
+
26
+
27
+ class << self
28
+ def base_url
29
+ "http://cli.linksynergy.com/"
30
+ end
31
+
32
+ def validate_params!(provided_params, available_params, default_params = {})
33
+ params = default_params.merge(provided_params)
34
+ invalid_params = params.select{|k,v| !available_params.include?(k.to_s)}.map{|k,v| k}
35
+ raise ArgumentError.new("Invalid parameters: #{invalid_params.join(', ')}") if invalid_params.length > 0
36
+ end
37
+
38
+ def get_service(path, query)
39
+ query.keys.each{|k| query[k.to_s] = query.delete(k)}
40
+ query.merge!({'cuserid' => credentials['user_id'], 'cpi' => credentials['pass']})
41
+
42
+ results = []
43
+
44
+ begin
45
+ response = get(path, :query => query, :timeout => 30)
46
+ rescue Timeout::Error
47
+ nil
48
+ end
49
+
50
+ unless validate_response(response)
51
+ str = response.body #+ "1x1\t36342\tAdvertiser Y\t2163\t1/31/2002\t8:58\t32\t7.99\t1\t0.39\t2/1/2002\t12:46" #dummy data
52
+ str = str.gsub(" \t","\t").gsub("\t\n", "\n").gsub(" ", "_").gsub("($)", "").downcase!
53
+
54
+ results = FasterCSV.parse(str, {:col_sep => "\t", :row_sep => "\n", :headers => true})
55
+ end
56
+
57
+ results.map{|r| self.new(r.to_hash)}
58
+ end # get
59
+
60
+ def credentials
61
+ unless @@credentials && @@credentials.length > 0
62
+ # there is no offline or test mode for CJ - so I won't include any credentials in this gem
63
+ config_file = ["config/linkshare.yml", File.join(ENV['HOME'], '.linkshare.yaml')].select{|f| File.exist?(f)}.first
64
+
65
+ unless File.exist?(config_file)
66
+ warn "Warning: config/linkshare.yaml does not exist. Put your CJ developer key and website ID in ~/.linkshare.yml to enable live testing."
67
+ else
68
+ @@credentials = YAML.load(File.read(config_file))
69
+ end
70
+ end
71
+ @@credentials
72
+ end # credentails
73
+
74
+ def validate_response(response)
75
+ raise ArgumentError, "There was an error connecting to LinkShare's reporting server." if response.body.include?("REPORTING ERROR")
76
+ end
77
+
78
+ def first(params)
79
+ find(params).first
80
+ end
81
+
82
+ end # self
83
+ end # Base
84
+ end # Linkshare
@@ -0,0 +1,15 @@
1
+ module Linkshare
2
+ class Commission < Base
3
+ class << self
4
+ def service_url
5
+ base_url + "cli/publisher/reports/downloadReport.php"
6
+ end
7
+
8
+ def find(params = {})
9
+ validate_params!(params, %w{bdate edate eid nid})
10
+ get_service(service_url, params)
11
+ end
12
+
13
+ end # << self
14
+ end # class
15
+ end # module
@@ -0,0 +1,3 @@
1
+ module Linkshare
2
+ VERSION = "0.0.9"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "linkshare/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "linkshare"
7
+ s.version = Linkshare::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ian Ehlert"]
10
+ s.email = ["ian.ehlert@tstmedia.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Ruby gem to hit the Linkshare report api.}
13
+ s.description = %q{Ruby gem to hit the Linkshare report api.}
14
+
15
+ s.rubyforge_project = "linkshare"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.add_dependency(%q<httparty>)
22
+ s.add_dependency(%q<fastercsv>)
23
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkshare
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 9
10
+ version: 0.0.9
11
+ platform: ruby
12
+ authors:
13
+ - Ian Ehlert
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-26 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httparty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: fastercsv
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Ruby gem to hit the Linkshare report api.
49
+ email:
50
+ - ian.ehlert@tstmedia.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - README.rdoc
61
+ - Rakefile
62
+ - lib/linkshare.rb
63
+ - lib/linkshare/base.rb
64
+ - lib/linkshare/commission.rb
65
+ - lib/linkshare/version.rb
66
+ - linkshare.gemspec
67
+ homepage: ""
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project: linkshare
96
+ rubygems_version: 1.8.10
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Ruby gem to hit the Linkshare report api.
100
+ test_files: []
101
+