Class: MojuraAPI::MailChimpResource
- Inherits:
-
RestResource
- Object
- RestResource
- MojuraAPI::MailChimpResource
- Defined in:
- lib/mojura/api/resources/mailchimp/mailchimp.rest.rb
Instance Attribute Summary collapse
-
#cached_atk ⇒ Object
readonly
Returns the value of attribute cached_atk.
-
#cached_listids ⇒ Object
readonly
Returns the value of attribute cached_listids.
Instance Method Summary collapse
- #all(params) ⇒ Object
- #all_conditions ⇒ Object
- #description ⇒ Object
- #get_from_mailchimp(api_url, method = :get, payload = nil) ⇒ Object
- #name ⇒ Object
- #post(params) ⇒ Object
- #post_conditions ⇒ Object
Instance Attribute Details
#cached_atk ⇒ Object (readonly)
Returns the value of attribute cached_atk.
8 9 10 |
# File 'lib/mojura/api/resources/mailchimp/mailchimp.rest.rb', line 8 def cached_atk @cached_atk end |
#cached_listids ⇒ Object (readonly)
Returns the value of attribute cached_listids.
8 9 10 |
# File 'lib/mojura/api/resources/mailchimp/mailchimp.rest.rb', line 8 def cached_listids @cached_listids end |
Instance Method Details
#all(params) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# 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 ⇒ Object
88 89 90 91 92 |
# File 'lib/mojura/api/resources/mailchimp/mailchimp.rest.rb', line 88 def all_conditions { description: 'Returns an array of MailChimp Lists.', } end |
#description ⇒ Object
14 15 16 |
# 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) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 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.} : JSON.parse(e.response) raise HTTPException.new(json['detail']) end data = JSON.parse(response) data.symbolize_keys! return data end |
#name ⇒ Object
10 11 12 |
# File 'lib/mojura/api/resources/mailchimp/mailchimp.rest.rb', line 10 def name 'MailChimp resource.' end |
#post(params) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 |