Class: Jung::Drivers::Mailchimp::Api
- Inherits:
-
Object
- Object
- Jung::Drivers::Mailchimp::Api
- Defined in:
- lib/jung/drivers/mailchimp/api.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#gb ⇒ Object
readonly
Returns the value of attribute gb.
-
#list_id ⇒ Object
readonly
Returns the value of attribute list_id.
Instance Method Summary collapse
-
#all_campaigns ⇒ Object
Campaign Related.
- #campaign(id) ⇒ Object
- #campaign_content(id) ⇒ Object
- #campaign_create(campaign) ⇒ Object
- #campaign_delete(id) ⇒ Object
- #campaign_schedule(id, time) ⇒ Object
- #campaign_send_now(id) ⇒ Object
- #campaign_send_test(id, recipients) ⇒ Object
- #campaign_unschedule(id) ⇒ Object
- #campaign_update(campaign) ⇒ Object
- #campaign_update_static_segment(id, static_segment_id) ⇒ Object
- #find_or_add_static_segment(name) ⇒ Object
-
#initialize(config) ⇒ Api
constructor
A new instance of Api.
-
#list_ensure_merge_vars(recipient) ⇒ Object
List Related.
- #list_members ⇒ Object
- #list_merge_var_add(merge_var) ⇒ Object
- #list_merge_vars ⇒ Object
- #list_static_segment_add(name) ⇒ Object
- #list_static_segment_delete(static_segment_id) ⇒ Object
- #list_static_segment_find(name) ⇒ Object
- #list_static_segment_members_add(static_segment_id, emails) ⇒ Object
- #list_static_segment_reset(static_segment_id) ⇒ Object
- #list_static_segments ⇒ Object
- #list_subscribe(recipient) ⇒ Object
- #list_unsubscribe(address) ⇒ Object
- #list_wipe_static_segments ⇒ Object
Constructor Details
#initialize(config) ⇒ Api
Returns a new instance of Api.
6 7 8 9 10 11 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 6 def initialize(config) @config = config @gb = Gibbon.new config.["api_key"] @list_id = config.["list_id"] @errors = [] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 3 def config @config end |
#errors ⇒ Object
Returns the value of attribute errors.
4 5 6 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 4 def errors @errors end |
#gb ⇒ Object (readonly)
Returns the value of attribute gb.
3 4 5 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 3 def gb @gb end |
#list_id ⇒ Object (readonly)
Returns the value of attribute list_id.
3 4 5 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 3 def list_id @list_id end |
Instance Method Details
#all_campaigns ⇒ Object
Campaign Related
131 132 133 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 131 def all_campaigns gb.campaigns({ :filters => { :list_id => list_id } }) end |
#campaign(id) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 159 def campaign id add_errors_and_return(gb.campaigns({ :filters => { :campaign_id => id } })) { self["data"].first } end |
#campaign_content(id) ⇒ Object
167 168 169 170 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 167 def campaign_content id campaign_content = gb.campaign_content :cid => id campaign_content["html"] end |
#campaign_create(campaign) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 135 def campaign_create campaign add_errors_and_return(gb.campaign_create({ :type => :regular, :options => { :list_id => list_id, :title => campaign.name, :subject => campaign.subject, :from_name => campaign.sender.name, :from_email => campaign.sender.address, :to_name => '*|FNAME|*', :generate_text => true, :fb_comments => false, :inline_css => config.["inline_css"] || true }, :content => { :html => campaign. } })) { gsub(/\"/, '') } end |
#campaign_delete(id) ⇒ Object
180 181 182 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 180 def campaign_delete id gb.campaign_delete :cid => id end |
#campaign_schedule(id, time) ⇒ Object
191 192 193 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 191 def campaign_schedule id, time gb.campaign_schedule :cid => id, :schedule_time => time.utc.strftime('%Y-%m-%d %H:%M:%S') end |
#campaign_send_now(id) ⇒ Object
172 173 174 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 172 def campaign_send_now id gb.campaign_send_now :cid => id end |
#campaign_send_test(id, recipients) ⇒ Object
176 177 178 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 176 def campaign_send_test id, recipients gb.campaign_send_test :cid => id, :test_emails => recipients end |
#campaign_unschedule(id) ⇒ Object
195 196 197 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 195 def campaign_unschedule id gb.campaign_unschedule :cid => id end |
#campaign_update(campaign) ⇒ Object
155 156 157 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 155 def campaign_update campaign # campaign.pry end |
#campaign_update_static_segment(id, static_segment_id) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 184 def campaign_update_static_segment id, static_segment_id gb.campaign_update :cid => id, :name => "segment_opts", :value => { :match => "all", :conditions => [{ :field => "static_segment", :op => "eq", :value => static_segment_id }] } end |
#find_or_add_static_segment(name) ⇒ Object
99 100 101 102 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 99 def find_or_add_static_segment name list_static_segment_find(name) || list_static_segment_add(name) end |
#list_ensure_merge_vars(recipient) ⇒ Object
List Related
15 16 17 18 19 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 15 def list_ensure_merge_vars(recipient) recipient.attribute_names.map do |attribute_name| list_merge_var_add(attribute_name) end.reduce &:& end |
#list_members ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 68 def list_members current_members = [] members_array = gb.list_members :id => list_id, :limit => 15000 members_array["data"].each do |member| # TODO: Batch this method (Mailchimp supports up to 50 per call) info = gb.list_member_info :id => list_id, :email_address => member["email"] attributes = { :name => info["data"][0]["merges"].delete("FNAME"), :address => info["data"][0]["merges"].delete("EMAIL") } info["data"][0]["merges"].each_pair do |k, v| attributes[k.downcase.to_sym] = v end current_members << Jung::Recipient.new(attributes) end current_members end |
#list_merge_var_add(merge_var) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 21 def list_merge_var_add(merge_var) tag = merge_var.to_s.upcase name = tag.capitalize @merge_vars ||= list_merge_vars return true unless !@merge_vars.include? tag # Cache is never ugly @merge_vars << tag gb.list_merge_var_add({ :id => list_id, :tag => tag, :name => name }) end |
#list_merge_vars ⇒ Object
39 40 41 42 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 39 def list_merge_vars merge_vars_array = gb.list_merge_vars :id => list_id merge_vars_array.map { |e| e["tag"] } end |
#list_static_segment_add(name) ⇒ Object
104 105 106 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 104 def list_static_segment_add name gb.list_static_segment_add :id => list_id, :name => name end |
#list_static_segment_delete(static_segment_id) ⇒ Object
117 118 119 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 117 def list_static_segment_delete static_segment_id gb.list_static_segment_del :id => list_id, :seg_id => static_segment_id end |
#list_static_segment_find(name) ⇒ Object
108 109 110 111 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 108 def list_static_segment_find name static_segments = list_static_segments static_segments[name] end |
#list_static_segment_members_add(static_segment_id, emails) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 121 def list_static_segment_members_add static_segment_id, emails add_errors_and_return(gb.list_static_segment_members_add({ :id => list_id, :seg_id => static_segment_id, :batch => emails })) { self["success"] == emails.count } end |
#list_static_segment_reset(static_segment_id) ⇒ Object
113 114 115 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 113 def list_static_segment_reset static_segment_id gb.list_static_segment_members_add :id => list_id, :seg_id => static_segment_id end |
#list_static_segments ⇒ Object
85 86 87 88 89 90 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 85 def list_static_segments gb.list_static_segments(:id => list_id).reduce({}) do | acc, v | acc[v["name"]] = v["id"] acc end end |
#list_subscribe(recipient) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 44 def list_subscribe(recipient) merge_vars = recipient.attributes.inject("FNAME" => recipient.name) do |attributes, (k, v)| attributes[k.to_s.upcase] = v attributes end add_errors_and_return(gb.list_subscribe({ :id => list_id, :email_address => recipient.address, :merge_vars => merge_vars, :double_optin => false, :update_existing => true })) { self == true } end |
#list_unsubscribe(address) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 59 def list_unsubscribe(address) gb.list_unsubscribe({ :id => list_id, :email_address => address, :delete_member => false, :send_goodbye => false }) end |
#list_wipe_static_segments ⇒ Object
92 93 94 95 96 97 |
# File 'lib/jung/drivers/mailchimp/api.rb', line 92 def list_wipe_static_segments # TODO: Call this only to unused segments. Internal use only for now. list_static_segments.each do | campaign_id, segment_id | list_static_segment_delete segment_id end end |