~tim/mailchimp3

fc6a50c58310e92afe7c8a891cbee60b5fbd5b5b — Tim Morgan 5 years ago b6235e8
Add ability to work with 2.0 API
3 files changed, 39 insertions(+), 7 deletions(-)

M README.md
M lib/mailchimp3/endpoint.rb
M lib/mailchimp3/errors.rb
M README.md => README.md +17 -1
@@ 3,7 3,8 @@
[![Circle CI](https://circleci.com/gh/seven1m/mailchimp3/tree/master.svg?style=svg)](https://circleci.com/gh/seven1m/mailchimp3/tree/master)

`mailchimp3` is a Rubygem that provides a very thin, simple wrapper around the MailChimp RESTful JSON API version 3.0
documented at [kb.mailchimp.com/api](http://kb.mailchimp.com/api/).
documented at [kb.mailchimp.com/api](http://kb.mailchimp.com/api/). (The wrapper also works with the 2.0 API if you
just use the `post` method. See 2.0 section later in this document.)

This wrapper is very low-level -- you'll still be dealing with individual GET, POST, PATCH, and DELETE requests, but the gem
does handle the OAuth2 flow (getting a token) and passing the auth token in every request.


@@ 271,6 272,21 @@ The `message` should be a simple string given by the API, e.g. "Resource Not Fou
Alternatively, you may rescue `MailChimp3::Errors::BaseError` and branch your code based on
the status code returned by calling `error.status`.

## MailChimp 2.0 API

This wrapper works with MailChimp's soon-to-be-deprecated 2.0 API. You need only use the `v2` path and the `post` method
for all your calls, like so:

```ruby
api.v2.lists['member-activity'].post(id: 'abc123', emails: ['tim@timmorgan.org'])
```

Some notes:

1. Call `v2` to switch to the 2.0 API, then add other path elements after that.
2. Any path element with a hyphen will need to use the array access syntax, e.g. `lists['member-activity']` not `lists.member-activity`.
3. You must use the `post` method for every call.

## Copyright & License

Copyright 2015, Tim Morgan. Licensed MIT.

M lib/mailchimp3/endpoint.rb => lib/mailchimp3/endpoint.rb +19 -5
@@ 5,12 5,13 @@ module MailChimp3
  class Endpoint
    attr_reader :url, :last_result

    def initialize(oauth_access_token: nil, basic_auth_key: nil, dc: nil, url: nil)
    def initialize(oauth_access_token: nil, basic_auth_key: nil, dc: nil, url: nil, version: 3)
      @oauth_access_token = oauth_access_token
      @basic_auth_key = basic_auth_key
      @dc = dc
      @dc ||= @basic_auth_key.split('-').last if @basic_auth_key
      @url = url || _build_url
      @version = version
      fail Errors::DataCenterRequiredError, 'You must pass dc.' unless @dc || @url
      @cache = {}
    end


@@ 30,6 31,7 @@ module MailChimp3

    def post(body = {})
      @last_result = _connection.post(@url) do |req|
        body[:apikey] = @oauth_access_token || @basic_auth_key if @version == 2
        req.body = body.to_json
      end
      _build_response(@last_result)


@@ 51,6 53,15 @@ module MailChimp3
      end
    end

    def v2
      self.class.new(
        url: _build_v2_url,
        basic_auth_key: @basic_auth_key,
        oauth_access_token: @oauth_access_token,
        version: 2
      )
    end

    private

    def _build_response(result)


@@ 86,15 97,18 @@ module MailChimp3
        self.class.new(
          url: File.join(url, path.to_s),
          basic_auth_key: @basic_auth_key,
          oauth_access_token: @oauth_access_token
          oauth_access_token: @oauth_access_token,
          version: @version
        )
      end
    end

    def _build_url
      url = "https://#{@dc}.api.mailchimp.com/3.0"
      url << '/' if url =~ /3\.0\z/ # workaround for the api requiring a slash on the root urls
      url
      "https://#{@dc}.api.mailchimp.com/3.0/"
    end

    def _build_v2_url
      "https://#{@dc}.api.mailchimp.com/2.0/"
    end

    def _connection

M lib/mailchimp3/errors.rb => lib/mailchimp3/errors.rb +3 -1
@@ 8,7 8,9 @@ module MailChimp3

      def initialize(response)
        @status = response.status
        @message = if response.body.is_a?(Hash)
        @message = if response.body.is_a?(Hash) && response.body['error']
                     "#{response.body['name']}: #{response.body['error']}"
                   elsif response.body.is_a?(Hash)
                     "#{response.body['title']}: #{response.body['detail']}"
                   else
                     response.body.to_s