ihunter-whatcounts 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ require 'cgi'
6
6
  # require 'fastercsv'
7
7
 
8
8
  module WhatCounts
9
- VERSION = '0.3.7'
9
+ VERSION = '0.4.0'
10
10
  class WhatCountsError < StandardError; end
11
11
 
12
12
  class CreationError < WhatCountsError; end
@@ -26,7 +26,8 @@ module WhatCounts
26
26
 
27
27
  def execute(command,requestable)
28
28
  raise InvalidConfiguration,"Invalid WhatCounts configuration, are you sure it was configured?" unless @config
29
- url = [@config.api_url,"?","c=#{command}&",@config.to_param_string,"&",requestable.to_param_string].join
29
+ request_string = requestable.kind_of?(String) ? requestable : requestable.to_param_string
30
+ url = [@config.api_url,"?","c=#{command}&",@config.to_param_string,"&",request_string].join
30
31
  res = Curl::Easy.perform(url) do |easy|
31
32
  easy.connect_timeout = 10
32
33
  easy.timeout = 30
@@ -38,6 +38,15 @@ module WhatCounts
38
38
  end
39
39
  end
40
40
 
41
+ def id; @id end
42
+ def to_param_string
43
+ @param_string ||= OPTIONS_TO_HTTP_PARAMS_MAP.keys.inject([]) do |acc,k|
44
+ val = self.send :"#{k}"
45
+ acc << "#{OPTIONS_TO_HTTP_PARAMS_MAP[k]}=#{CGI.escape(val.to_s)}" unless val.nil?
46
+ acc
47
+ end.join("&")
48
+ end
49
+
41
50
  class << self
42
51
  def create!(options)
43
52
  o = self.new options
@@ -45,25 +54,59 @@ module WhatCounts
45
54
  end
46
55
  end
47
56
 
48
- DEPLOY_OPTIONS_TO_HTTP_PARAMS_MAP = {:list => :list_id, :template => :template_id,
57
+ SUBSCRIBE_OPTIONS_TO_HTTP_PARAMS_MAP = {:list => :list_id, :format => :format,
58
+ :force_subscribe => :force_sub, :data => :data }
59
+ SUBSCRIBE_REQUIRED_OPTIONS = [:list]
60
+ SUBSCRIBE_IRREGULAR_OPTIONS = [:data]
61
+ def add_recipients!(recipient_ary)
62
+ options = {:list => @id, :format => 99, :force_subscribe => 1}
63
+ options[:data] = (['email']+recipient_ary).join('^')
64
+ res = WhatCounts::HttpClient.execute('sub',convert_to_whatcounts_parameters(options,SUBSCRIBE_OPTIONS_TO_HTTP_PARAMS_MAP,SUBSCRIBE_IRREGULAR_OPTIONS))
65
+ end
66
+
67
+ LAUNCH_OPTIONS_TO_HTTP_PARAMS_MAP = {:list => :list_id, :template => :template_id,
49
68
  :subject => :subject, :format => :format,
50
69
  :segmentation_id => :segmentation_id, :campaign_alias => :campaign_alias,
51
70
  :send_rss => :target_rss, :virtual_mta => :vmta,
52
71
  :goodmail_token => :gm_token_type, :notification_email => :notify_email }
53
- DEPLOY_REQUIRED_OPTIONS = []
54
- def deploy!(options)
55
- options.each_key {|o| raise UnknownListOptionError,"Unknown option #{o}" unless VALID_OPTIONS.include? o}
56
- res = WhatCounts::HttpClient.execute('excreatelist',o)
57
- end
58
-
59
- def id; @id end
60
- def to_param_string
61
- @param_string ||= OPTIONS_TO_HTTP_PARAMS_MAP.keys.inject([]) do |acc,k|
62
- val = self.send :"#{k}"
63
- acc << "#{OPTIONS_TO_HTTP_PARAMS_MAP[k]}=#{CGI.escape(val.to_s)}" unless val.nil?
64
- acc
65
- end.join("&")
72
+ LAUNCH_REQUIRED_OPTIONS = [:list]
73
+ def launch!(options={})
74
+ # options.each_key {|o| raise UnknownListOptionError,"Unknown option #{o}" unless VALID_OPTIONS.include? o}
75
+ options = {:list => @id, :format => 99}.merge(options)
76
+ res = WhatCounts::HttpClient.execute('launch',convert_to_whatcounts_parameters(options,LAUNCH_OPTIONS_TO_HTTP_PARAMS_MAP))
77
+ if res.success?
78
+ return res
79
+ else
80
+ puts res.url
81
+ raise DeployError, res.body
82
+ end
66
83
  end
67
84
 
85
+ private
86
+ #TODO move into baseclass
87
+ def convert_to_whatcounts_parameters(params,lookup,irregular_options=[])
88
+ begin
89
+ pairs = (lookup.keys-irregular_options).inject([]) do |acc,k|
90
+ val = params[k]
91
+ acc << "#{lookup[k]}=#{CGI.escape(val.to_s)}" unless val.nil?
92
+ acc
93
+ end
94
+ unless params[:data].nil?
95
+ if params[:data].kind_of? String
96
+ pairs << "data=#{params[:data]}"
97
+ elsif params[:data].kind_of? Hash
98
+ data_pairs = params[:data].inject([[],[]]) do |acc,(k,v)|
99
+ acc[0] << CGI.escape(k)
100
+ acc[1] << CGI.escape(v)
101
+ acc
102
+ end
103
+ pairs << "data=#{data_pairs[0].join(',')}^#{data_pairs[1].join(',')}"
104
+ else
105
+ # TODO i duno what to do here, probably error? Maybe not since the param isn't required
106
+ end
107
+ end
108
+ pairs
109
+ end.join("&")
110
+ end
68
111
  end
69
112
  end
@@ -4,13 +4,13 @@ module WhatCounts
4
4
  SUCCESS = true
5
5
  FAILURE = false
6
6
 
7
- attr_reader :result, :body
7
+ attr_reader :result, :body, :url
8
8
  private
9
- attr_writer :result, :body
10
-
9
+ attr_writer :result, :body, :url
11
10
  public
12
11
 
13
12
  def initialize(url,document)
13
+ self.url = url
14
14
  if document =~ /^(SUCCESS|FAILURE):?(.*)/
15
15
  # simple response
16
16
  self.body = $2.strip
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ihunter-whatcounts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Hunter