Skip to content

Commit

Permalink
Add Subscription.create (for importing existing subscriptions)
Browse files Browse the repository at this point in the history
  • Loading branch information
collimarco committed Sep 19, 2024
1 parent cd7fddb commit e977d94
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ Pushpad::Subscription.find 123
Pushpad::Subscription.find 123, project_id: 456
```

## Importing push subscriptions

If you need to [import](https://pushpad.xyz/docs/import) some existing push subscriptions (from another service to Pushpad, or from your backups) or if you simply need to create some test data, you can use this method:

```ruby
attributes = {
endpoint: "https://example.com/push/f7Q1Eyf7EyfAb1",
p256dh: "BCQVDTlYWdl05lal3lG5SKr3VxTrEWpZErbkxWrzknHrIKFwihDoZpc_2sH6Sh08h-CacUYI-H8gW4jH-uMYZQ4=",
auth: "cdKMlhgVeSPzCXZ3V7FtgQ==",
uid: "exampleUid",
tags: ["exampleTag1", "exampleTag2"]
}

subscription = Pushpad::Subscription.create(attributes, project_id: 5)
```

Please note that this is not the standard way to collect subscriptions on Pushpad: usually you subscribe the users to the notifications using the [JavaScript SDK](https://pushpad.xyz/docs/javascript_sdk_reference) in the frontend.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
17 changes: 17 additions & 0 deletions lib/pushpad/subscription.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Pushpad
class Subscription
class CreateError < RuntimeError
end

class CountError < RuntimeError
end

Expand All @@ -18,6 +21,20 @@ def initialize(options)
@last_click_at = options[:last_click_at] && Time.parse(options[:last_click_at])
@created_at = options[:created_at] && Time.parse(options[:created_at])
end

def self.create(attributes, options = {})
project_id = options[:project_id] || Pushpad.project_id
raise "You must set project_id" unless project_id

endpoint = "https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions"
response = Request.post(endpoint, attributes.to_json)

unless response.code == "201"
raise CreateError, "Response #{response.code} #{response.message}: #{response.body}"
end

new(JSON.parse(response.body, symbolize_names: true))
end

def self.count(options = {})
project_id = options[:project_id] || Pushpad.project_id
Expand Down
36 changes: 36 additions & 0 deletions spec/pushpad/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,42 @@ def stub_failing_subscriptions_get(options)
stub_request(:get, "https://pushpad.xyz/api/v1/projects/#{options[:project_id]}/subscriptions").
to_return(status: 403)
end

def stub_subscriptions_post(project_id, attributes = {})
stub_request(:post, "https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions").
with(body: hash_including(attributes)).
to_return(status: 201, body: attributes.to_json)
end

def stub_failing_subscriptions_post(project_id)
stub_request(:post, "https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions").
to_return(status: 403)
end

describe ".create" do
it "creates a new subscription with the given attributes and returns it" do
attributes = {
endpoint: "https://example.com/push/f7Q1Eyf7EyfAb1",
p256dh: "BCQVDTlYWdl05lal3lG5SKr3VxTrEWpZErbkxWrzknHrIKFwihDoZpc_2sH6Sh08h-CacUYI-H8gW4jH-uMYZQ4=",
auth: "cdKMlhgVeSPzCXZ3V7FtgQ==",
uid: "exampleUid",
tags: ["exampleTag1", "exampleTag2"]
}
stub_subscriptions_post(5, attributes)

subscription = Subscription.create(attributes, project_id: 5)
expect(subscription).to have_attributes(attributes)
end

it "fails with CreateError if response status code is not 201" do
attributes = { endpoint: "https://example.com/push/123" }
stub_failing_subscriptions_post(5)

expect {
Subscription.create(attributes, project_id: 5)
}.to raise_error(Subscription::CreateError)
end
end

describe ".count" do
it "returns value from X-Total-Count header" do
Expand Down

0 comments on commit e977d94

Please sign in to comment.