class MojuraAPI::MailChimpResource

Attributes

cached_atk[R]
cached_listids[R]

Public Instance Methods

all(params) click to toggle source
# File lib/mojura/api/resources/mailchimp/mailchimp.rest.rb, line 71
def all(params)
        now = Time.now.to_i
        result = []
        if (@cached_listids.nil? || (now - @cached_at.to_i > 300))
                data = get_from_mailchimp('lists')
                data[:lists].each { | list_data |
                        result.push({id: list_data[:id], name: list_data[:name]})
                }
                @cached_listids = result
                @cached_at = Time.now.to_i
                return result
        else
                result = @cached_listids
        end
        return result;
end
all_conditions() click to toggle source
# File lib/mojura/api/resources/mailchimp/mailchimp.rest.rb, line 88
def all_conditions
        {
                description: 'Returns an array of MailChimp Lists.',
        }
end
description() click to toggle source
# File lib/mojura/api/resources/mailchimp/mailchimp.rest.rb, line 14
def description
        'A resource which post subscribers to a MailChimp list.'
end
get_from_mailchimp(api_url, method = :get, payload = nil) click to toggle source
# File lib/mojura/api/resources/mailchimp/mailchimp.rest.rb, line 18
def get_from_mailchimp(api_url, method = :get, payload = nil)
        api_key = Settings.get_s(:api_key, :mailchimp)
        if (api_key.empty?)
                raise HTTPException.new('The api_key is not configured.')
        end
        data_center = api_key.scan(/\-(\w+)$/)[0][0]
        url = "https://#{data_center}.api.mailchimp.com/3.0/" + api_url

        headers = { Authorization: "apikey #{api_key}" }

        begin
                response = RestClient::Request.execute({
                 url: url,
                 method: method,
                 headers: headers,
                 payload: payload
                })
        rescue => e
                json = e.response.nil? ? { 'detail' => e.message} : JSON.parse(e.response)
                raise HTTPException.new(json['detail'])
        end

        data = JSON.parse(response)
        data.symbolize_keys!
        return data
end
name() click to toggle source
# File lib/mojura/api/resources/mailchimp/mailchimp.rest.rb, line 10
def name
        'MailChimp resource.'
end
post(params) click to toggle source
# File lib/mojura/api/resources/mailchimp/mailchimp.rest.rb, line 45
def post(params)
        request_json = JSON.generate({
                email_address: params[:email],
                status: 'pending',
          merge_fields: {
                  FNAME: params[:firstname],
            LNAME: params[:lastname]
          }
        })
        api_url = "lists/#{params[:listid]}/members"
        return get_from_mailchimp(api_url, :post, request_json)
end
post_conditions() click to toggle source
# File lib/mojura/api/resources/mailchimp/mailchimp.rest.rb, line 58
def post_conditions
        result = {
                description: 'Adds an email address as a subscriber to MailChimp.',
                attributes: {
                        listid: {required: true, type: String, description: 'The id of a MailChimp list.'},
                        email: {required: true, type: String, description: 'The email address.'},
                        firstname: {required: true, type: String, description: 'The first name.'},
                        lastname: {required: true, type: String, description: 'The last name.'},
                }
        }
        return result
end