mailchimp-rest-api 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,8 @@
3
3
  require "json"
4
4
 
5
5
  module MailchimpAPI
6
- #
7
- # MailchimpAPI::Response object
8
- #
6
+ # Response class for handling Mailchimp API responses
7
+ # @api public
9
8
  class Response
10
9
  # List of Net::HTTP responses that can be retried
11
10
  RETRYABLE_RESPONSES = [
@@ -19,14 +18,12 @@ module MailchimpAPI
19
18
  # @return [Request] Request object
20
19
  attr_reader :request
21
20
 
22
- #
23
21
  # Initializes Response object
24
- #
25
22
  # @param http_response [Net::HTTP::Response] original response
26
23
  # @param request [Request] Request that generates this response
27
- #
28
24
  # @return [Response] Initialized Response object
29
- #
25
+ # @example
26
+ # response = MailchimpAPI::Response.new(http_response, request: request)
30
27
  def initialize(http_response, request:)
31
28
  @request = request
32
29
  @http_response = http_response
@@ -38,74 +35,94 @@ module MailchimpAPI
38
35
 
39
36
  # Parses JSON body if response body contains JSON or returns original
40
37
  # http body string
41
- #
42
38
  # @return [Hash, String] Parsed response body (with symbolized keys)
39
+ # @example
40
+ # response.body # => { id: "123", name: "Example List" }
43
41
  def body
44
42
  @body ||= json_response? ? parse_json(http_body) : http_body
45
43
  end
46
44
 
47
45
  # @return [Integer] HTTP status as Integer
46
+ # @example
47
+ # response.http_status # => 200
48
48
  def http_status
49
49
  @http_status ||= http_response.code.to_i
50
50
  end
51
51
 
52
52
  # @return [Hash] HTTP headers as Hash
53
+ # @example
54
+ # response.http_headers # => { "content-type" => "application/json" }
53
55
  def http_headers
54
56
  @http_headers ||= http_response.each_header.to_h
55
57
  end
56
58
 
57
59
  # @return [String] Original http body
60
+ # @example
61
+ # response.http_body # => '{"id":"123","name":"Example List"}'
58
62
  def http_body
59
63
  @http_body ||= http_response.body
60
64
  end
61
65
 
62
66
  # Takes specific key from body, returns nil if key not present in parsed body
67
+ # @param key [String, Symbol] Key to fetch from response body
68
+ # @return [Object, nil] Value for the given key or nil if not found
69
+ # @example
70
+ # response[:id] # => "123"
63
71
  def [](key)
64
72
  body[key.to_sym] if body.is_a?(Hash)
65
73
  end
66
74
 
67
75
  # Fetches specific key from body, raises error if key not exists
76
+ # @param key [String, Symbol] Key to fetch from response body
77
+ # @return [Object] Value for the given key
78
+ # @raise [KeyError] if key is not found in response body
79
+ # @example
80
+ # response.fetch(:id) # => "123"
68
81
  def fetch(key)
69
82
  data = body.is_a?(Hash) ? body : {}
70
83
  data.fetch(key.to_sym)
71
84
  end
72
85
 
73
86
  # Checks http status code is 2xx
74
- #
75
87
  # @return [Boolean] Returns true if response has success status code (2xx)
88
+ # @example
89
+ # response.success? # => true
76
90
  def success?
77
91
  http_response.is_a?(Net::HTTPSuccess)
78
92
  end
79
93
 
80
94
  # Checks http status code is not 2xx
81
- #
82
95
  # @return [Boolean] Returns true if response has not success status code
96
+ # @example
97
+ # response.failed? # => false
83
98
  def failed?
84
99
  !success?
85
100
  end
86
101
 
87
102
  # Checks if response status code is retriable (5xx, 409, 429)
88
103
  # @api private
89
- #
90
104
  # @return [Boolean] Returns true if status code is retriable (5xx, 409, 429)
91
105
  def retryable?
92
106
  failed? && RETRYABLE_RESPONSES.any? { |retryable_class| http_response.is_a?(retryable_class) }
93
107
  end
94
108
 
95
- #
96
109
  # Instance representation string. Default was overwritten to hide secrets
97
- #
110
+ # @return [String] String representation of the response
111
+ # @example
112
+ # response.inspect # => "#<MailchimpAPI::Response (200)>"
98
113
  def inspect
99
114
  "#<#{self.class.name} (#{http_response.code})>"
100
115
  end
101
116
 
102
117
  private
103
118
 
119
+ # @api private
104
120
  def json_response?
105
121
  content_type = http_response["content-type"]
106
122
  !content_type.nil? && content_type.include?("json")
107
123
  end
108
124
 
125
+ # @api private
109
126
  def parse_json(json)
110
127
  JSON.parse(json, symbolize_names: true)
111
128
  rescue JSON::ParserError, TypeError
@@ -1,8 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MailchimpAPI
4
+ # Internal class for building and manipulating URIs
5
+ # @api private
4
6
  class URIBuilder
5
7
  class << self
8
+ # Builds a URI from base URL, path, and query parameters
9
+ # @param url [String] Base URL
10
+ # @param path [String] Path to append to URL
11
+ # @param query [Hash, nil] Optional query parameters
12
+ # @return [URI] Built URI with query parameters
13
+ # @example
14
+ # URIBuilder.call(
15
+ # url: "https://api.mailchimp.com/3.0",
16
+ # path: "lists",
17
+ # query: { count: 10 }
18
+ # )
6
19
  def call(url:, path:, query:)
7
20
  uri =
8
21
  if path.start_with?("/")
data/lib/mailchimp-api.rb CHANGED
@@ -2,45 +2,152 @@
2
2
 
3
3
  require "forwardable"
4
4
 
5
+ # Main module for the Mailchimp REST API client
6
+ # @api public
5
7
  module MailchimpAPI
8
+ # Current Mailchimp API version
6
9
  API_VERSION = "3.0"
7
10
 
8
11
  class << self
9
12
  extend Forwardable
10
13
 
14
+ # @!attribute [w] client
15
+ # Sets the client instance for the MailchimpAPI module
16
+ # @param value [Client] the client instance to set
11
17
  attr_writer :client
12
18
 
13
- # HTTP methods
14
- def_delegators :@client,
15
- :post,
16
- :get,
17
- :patch,
18
- :put,
19
- :delete
20
-
21
- # Batch methods
22
- def_delegators :@client,
23
- :batch,
24
- :batch_get_request,
25
- :batch_post_request,
26
- :batch_put_request,
27
- :batch_patch_request,
28
- :batch_delete_request
29
-
30
- # Pagination methods
31
- def_delegators :@client,
32
- :each_page,
33
- :each_page_item,
34
- :next_page
35
-
36
- # Resources
37
- def_delegators :@client,
38
- :audience_interest_categories,
39
- :audience_interests,
40
- :audience_member_tags,
41
- :audience_members,
42
- :audience_webhooks
19
+ # @!group HTTP Methods
43
20
 
21
+ # @!method post
22
+ # Delegates POST requests to the client
23
+ # @return [Response] API response
24
+ # @see Client#post
25
+ def_delegators :@client, :post
26
+
27
+ # @!method get
28
+ # Delegates GET requests to the client
29
+ # @return [Response] API response
30
+ # @see Client#get
31
+ def_delegators :@client, :get
32
+
33
+ # @!method patch
34
+ # Delegates PATCH requests to the client
35
+ # @return [Response] API response
36
+ # @see Client#patch
37
+ def_delegators :@client, :patch
38
+
39
+ # @!method put
40
+ # Delegates PUT requests to the client
41
+ # @return [Response] API response
42
+ # @see Client#put
43
+ def_delegators :@client, :put
44
+
45
+ # @!method delete
46
+ # Delegates DELETE requests to the client
47
+ # @return [Response] API response
48
+ # @see Client#delete
49
+ def_delegators :@client, :delete
50
+
51
+ # @!endgroup
52
+
53
+ # @!group Batch Methods
54
+
55
+ # @!method batch
56
+ # Sends batch request
57
+ # @return [Response] API response
58
+ # @see Client#batch
59
+ def_delegators :@client, :batch
60
+
61
+ # @!method batch_get_request
62
+ # Prepares a GET request for batch operation
63
+ # @return [BatchRequest] batch request instance
64
+ # @see Client#batch_get_request
65
+ def_delegators :@client, :batch_get_request
66
+
67
+ # @!method batch_post_request
68
+ # Prepares a POST request for batch operation
69
+ # @return [BatchRequest] batch request instance
70
+ # @see Client#batch_post_request
71
+ def_delegators :@client, :batch_post_request
72
+
73
+ # @!method batch_put_request
74
+ # Prepares a PUT request for batch operation
75
+ # @return [BatchRequest] batch request instance
76
+ # @see Client#batch_put_request
77
+ def_delegators :@client, :batch_put_request
78
+
79
+ # @!method batch_patch_request
80
+ # Prepares a PATCH request for batch operation
81
+ # @return [BatchRequest] batch request instance
82
+ # @see Client#batch_patch_request
83
+ def_delegators :@client, :batch_patch_request
84
+
85
+ # @!method batch_delete_request
86
+ # Prepares a DELETE request for batch operation
87
+ # @return [BatchRequest] batch request instance
88
+ # @see Client#batch_delete_request
89
+ def_delegators :@client, :batch_delete_request
90
+
91
+ # @!endgroup
92
+
93
+ # @!group API Resources
94
+
95
+ # @!method audience_interest_categories
96
+ # Builds audience interest categories API resource
97
+ # @return [Audience::InterestCategories]
98
+ # the interest categories APIs collection
99
+ # @see Audience::InterestCategories#audience_interest_categories
100
+ def_delegators :@client, :audience_interest_categories
101
+
102
+ # @!method audience_interests
103
+ # Builds audience interests API resource
104
+ # @return [Audience::Interests]
105
+ # the interests APIs collection
106
+ # @see Audience::Interests#audience_interests
107
+ def_delegators :@client, :audience_interests
108
+
109
+ # @!method audience_member_tags
110
+ # Builds audience member tags API resource
111
+ # @return [Audience::MemberTags]
112
+ # the member tags APIs collection
113
+ # @see Audience::MemberTags#audience_member_tags
114
+ def_delegators :@client, :audience_member_tags
115
+
116
+ # @!method audience_members
117
+ # Builds audience members API resource
118
+ # @return [Audience::Members]
119
+ # the members APIs collection
120
+ # @see Audience::Members#audience_members
121
+ def_delegators :@client, :audience_members
122
+
123
+ # @!method audience_segment_members
124
+ # Builds audience segment members API resource
125
+ # @return [Audience::SegmentMembers]
126
+ # the segment members APIs collection
127
+ # @see Audience::SegmentMembers#audience_segment_members
128
+ def_delegators :@client, :audience_segment_members
129
+
130
+ # @!method audience_segments
131
+ # Builds audience segments API resource
132
+ # @return [Audience::Segments]
133
+ # the segments APIs collection
134
+ # @see Audience::Segments#audience_segments
135
+ def_delegators :@client, :audience_segments
136
+
137
+ # @!method audience_webhooks
138
+ # Builds audience webhooks API resource
139
+ # @return [Audience::Webhooks]
140
+ # the webhooks APIs collection
141
+ # @see Audience::Webhooks#audience_webhooks
142
+ def_delegators :@client, :audience_webhooks
143
+
144
+ # @!endgroup
145
+
146
+ # Returns the configured client instance
147
+ # @raise [RuntimeError] if client is not configured
148
+ # @return [Client] the configured client instance
149
+ # @example
150
+ # MailchimpAPI.client # => #<MailchimpAPI::Client>
44
151
  def client
45
152
  raise "#{name}.client must be set" unless @client
46
153
 
@@ -58,6 +165,8 @@ require_relative "mailchimp-api/resources/audience/interests"
58
165
  require_relative "mailchimp-api/resources/audience/member_tags"
59
166
  require_relative "mailchimp-api/resources/audience/members"
60
167
  require_relative "mailchimp-api/resources/audience/webhooks"
168
+ require_relative "mailchimp-api/resources/audience/segments"
169
+ require_relative "mailchimp-api/resources/audience/segment_members"
61
170
  require_relative "mailchimp-api/batch_request"
62
171
  require_relative "mailchimp-api/client/api_methods"
63
172
  require_relative "mailchimp-api/client/batch_methods"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailchimp-rest-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov
@@ -36,6 +36,8 @@ files:
36
36
  - lib/mailchimp-api/resources/audience/interests.rb
37
37
  - lib/mailchimp-api/resources/audience/member_tags.rb
38
38
  - lib/mailchimp-api/resources/audience/members.rb
39
+ - lib/mailchimp-api/resources/audience/segment_members.rb
40
+ - lib/mailchimp-api/resources/audience/segments.rb
39
41
  - lib/mailchimp-api/resources/audience/utils/subscriber_hash.rb
40
42
  - lib/mailchimp-api/resources/audience/webhooks.rb
41
43
  - lib/mailchimp-api/response.rb
@@ -46,9 +48,11 @@ homepage: https://github.com/andreyruby/mailchimp-rest-api
46
48
  licenses:
47
49
  - MIT
48
50
  metadata:
49
- source_code_uri: https://github.com/andreyruby/mailchimp-rest-api
50
- documentation_uri: https://www.rubydoc.info/gems/mailchimp-rest-api
51
- changelog_uri: https://github.com/andreyruby/mailchimp-rest-api/blob/master/CHANGELOG.md
51
+ bug_tracker_uri: https://github.com/andreyruby/mailchimp-rest-api/issues
52
+ changelog_uri: https://github.com/andreyruby/mailchimp-rest-api/CHANGELOG.md
53
+ documentation_uri: https://github.com/andreyruby/mailchimp-rest-api/README.md
54
+ homepage_uri: https://github.com/andreyruby/mailchimp-rest-api
55
+ source_code_uri: https://github.com/andreyruby/mailchimp-rest-api/tree/v0.4.1
52
56
  rdoc_options: []
53
57
  require_paths:
54
58
  - lib