Skip to content

Commit

Permalink
Merge pull request #17 from libmartinito/update-subscribers-update
Browse files Browse the repository at this point in the history
Update subscribers update
  • Loading branch information
dinomh authored Nov 20, 2023
2 parents 80f17a6 + a6103bd commit f1d1645
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
61 changes: 61 additions & 0 deletions fixtures/subscribers/update.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions lib/mailerlite/subscribers/subscribers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def create(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip
client.http.post("#{API_URL}/subscribers", json: params.compact)
end

# Creates a new subscriber with the specified details.
# Updates an existing subscriber with the specified details.
#
# @param subscriber_id [String] the ID of the subscriber to update
# @param email [String] the email address of the new subscriber
# @param fields [Hash] a hash of custom fields and their values for the subscriber
# @param groups [Array] an array of group IDs to add the subscriber to
Expand All @@ -67,9 +68,10 @@ def create(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip
# @param optin_ip [String] the IP address of the subscriber when they confirmed their subscription
# @param unsubscribed_at [DateTime] the date and time when the subscriber was unsubscribed
# @return [HTTP::Response] the response from the API
def update(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil)
params = { 'email' => email }
def update(subscriber_id, email: nil, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil)
params = {}

params['email'] = email if email
params['fields'] = fields if fields
params['groups'] = groups if groups
params['status'] = status if status
Expand All @@ -79,7 +81,7 @@ def update(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip
params['optin_ip'] = optin_ip if optin_ip
params['unsubscribed_at'] = unsubscribed_at if unsubscribed_at

client.http.post("#{API_URL}/subscribers", json: params.compact)
client.http.put("#{API_URL}/subscribers/#{subscriber_id}", json: params.compact)
end

# Returns the details of the specified subscribers
Expand Down
12 changes: 12 additions & 0 deletions spec/subscribers_rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@
end
end

describe '#update' do
# Use VCR to record and replay the HTTP request
it 'updates a subscriber' do
VCR.use_cassette('subscribers/update') do
response = subscribers.update(98_112_484_192_290_662, email: '[email protected]')
body = JSON.parse(response.body)
expect(response.status).to eq 200
expect(body['data']['email']).to eq '[email protected]'
end
end
end

describe '#delete' do
# Use VCR to record and replay the HTTP request
it 'deletes a subscriber' do
Expand Down

0 comments on commit f1d1645

Please sign in to comment.