Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.0.0 #23

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 7 additions & 35 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
name: Run all tests

on:
pull_request:
push:
branches:
- master
on: push

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby:
- 2.4
- 2.5
- 2.6
- 2.7
gemfile:
- Gemfile
- gemfiles/Gemfile.rails-4.2.x
- gemfiles/Gemfile.rails-5.0.x
- gemfiles/Gemfile.rails-5.1.x
- gemfiles/Gemfile.rails-5.2.x
exclude:
- gemfile: gemfiles/Gemfile.rails-4.2.x
ruby: 2.7
- 3.0
- 3.1
- 3.2

name: Run gem tests
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup ruby ${{ matrix.ruby }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Cache gems
uses: actions/cache@v1
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ matrix.ruby }}-${{ matrix.gemfile }}
restore-keys: |
${{ runner.os }}-gems-${{ matrix.ruby }}-${{ matrix.gemfile }}
- name: Install bundler 1.17.3
run: gem install bundler -v 1.17.3
- name: Install gems
run: |
bundle _1.17.3_ config path vendor/bundle
bundle _1.17.3_ install --gemfile=${{ matrix.gemfile }} \
--jobs 4 --retry 3
bundler-cache: true
- name: Run tests
run: |
bundle _1.17.3_ exec --gemfile=${{ matrix.gemfile }} \
rspec
bundle exec rspec
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ source 'https://rubygems.org'
gem 'magicbell'
```

and run `bundle install` a usual.
and run

`bundle install`

## Configuration

The library needs to be configured with your MagicBell project's API key and
secret.

### Global configuration
### Global Configuration

By default, this library will automatically pick your MagicBell project's API
key and secret from the `MAGICBELL_API_KEY` and `MAGICBELL_API_SECRET`
Expand Down Expand Up @@ -112,7 +114,7 @@ magicbell.create_notification(
```

This method has the benefit of allowing users to access their notifications when
their email address changes. Make sure you identify users by their `externalID`
their email address changes. Make sure you identify users by their `external_id`
when you [initialize the notification inbox](https://magicbell.com/docs/react/identifying-users), too.

You can also provide other data accepted by [our API](https://magicbell.com/docs/rest-api/reference):
Expand All @@ -121,7 +123,7 @@ You can also provide other data accepted by [our API](https://magicbell.com/docs
require 'magicbell'

magicbell = MagicBell::Client.new
magicbell.create_notification(
magicbell.create_broadcast(
title: 'Rob assigned to a task to you',
content: 'Hey Joe, can give this customer a demo of our app?',
action_url: 'https://example.com/task_path',
Expand Down
12 changes: 4 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
puts 'Run `gem install bundler` and `bundle install` to run rake tasks'
end

require 'rdoc/task'
Expand Down Expand Up @@ -30,12 +32,6 @@ task default: :test
# GEM_HOST_API_KEY="our_rubygems_api_key" bundle exec rake publish_to_rubygems
task :publish_to_rubygems do
`gem build magicbell.gemspec`
require_relative "lib/magicbell"
require_relative 'lib/magicbell'
`gem push magicbell-#{MagicBell::VERSION}.gem`
end

task :console do
require_relative "lib/magicbell"
require "pry"
binding.pry
end
6 changes: 0 additions & 6 deletions gemfiles/Gemfile.rails-4.2.x

This file was deleted.

6 changes: 0 additions & 6 deletions gemfiles/Gemfile.rails-5.0.x

This file was deleted.

6 changes: 0 additions & 6 deletions gemfiles/Gemfile.rails-5.1.x

This file was deleted.

6 changes: 0 additions & 6 deletions gemfiles/Gemfile.rails-5.2.x

This file was deleted.

5 changes: 3 additions & 2 deletions lib/magicbell.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'forwardable'

require 'openssl'
Expand All @@ -12,6 +14,7 @@
require 'magicbell/singleton_api_resource'
require 'magicbell/api_resource_collection'
require 'magicbell/api_resources/notification'
require 'magicbell/api_resources/broadcast'
require 'magicbell/api_resources/user'
require 'magicbell/api_resources/user_notification'
require 'magicbell/api_resources/user_notifications'
Expand All @@ -21,8 +24,6 @@
require 'magicbell/api_resources/user_notifications_seen'
require 'magicbell/api_resources/user_notification_preferences'

require 'magicbell/railtie' if defined?(Rails)

module MagicBell
class << self
extend Forwardable
Expand Down
21 changes: 0 additions & 21 deletions lib/magicbell/action_mailer_extension.rb

This file was deleted.

51 changes: 11 additions & 40 deletions lib/magicbell/api_operations.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,31 @@
require 'json'
require 'colorize'
# frozen_string_literal: true

module MagicBell
module ApiOperations
def get(url, options = {})
defaults = { headers: default_headers }
response = HTTParty.get(url, options.merge(defaults))
raise_http_error_unless_2xx_response(response)

response
HTTParty.get(url, options.merge(defaults))
end

def post(url, options = {})
defaults = { headers: default_headers }
response = HTTParty.post(url, options.merge(defaults))
raise_http_error_unless_2xx_response(response)

response
HTTParty.post(url, options.merge(defaults))
end

def put(url, options = {})
defaults = { headers: default_headers }
response = HTTParty.put(url, options.merge(defaults))
raise_http_error_unless_2xx_response(response)

response
HTTParty.put(url, options.merge(defaults))
end

protected

def default_headers
authentication_headers.merge({ 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
end

private

def raise_http_error_unless_2xx_response(response)
return if response.success?

e = MagicBell::Client::HTTPError.new
e.response_status = response.code
e.response_headers = response.headers.to_h
e.response_body = response.body
e.errors = []
unless e.response_body.nil? || e.response_body.empty?
body = JSON.parse(response.body)
e.errors = body['errors'].is_a?(Array) ? body['errors'] : []
e.errors.each do |error, _index|
suggestion = error['suggestion']
help_link = error['help_link']

puts suggestion.red.to_s if suggestion
puts help_link.blue.on_white.to_s if help_link
end
end

raise e
authentication_headers.merge(
{
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
)
end
end
end
29 changes: 6 additions & 23 deletions lib/magicbell/api_resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'active_support/inflector'
require 'active_support/core_ext/object/blank'
require 'json'

module MagicBell
class ApiResource
Expand Down Expand Up @@ -43,17 +44,14 @@ def attributes
@attributes
end

alias_method :to_h, :attributes
alias to_h attributes

def attribute(attribute_name)
attributes[attribute_name]
end

def retrieve
response = @client.get(url)
parse_response(response)

self
@client.get(url)
end

def name
Expand All @@ -77,25 +75,19 @@ def path
end

def create
response = @client.post(
@client.post(
create_url,
body: { name => attributes }.to_json,
headers: extra_headers
)
parse_response(response)

self
end

def update(new_attributes = {})
response = @client.put(
@client.put(
url,
body: new_attributes.to_json,
headers: extra_headers
)
parse_response(response)

self
end

protected
Expand All @@ -114,14 +106,5 @@ def retrieve_unless_retrieved

retrieve
end

def parse_response(response)
@response = response
unless response.body.blank?
@response_hash = JSON.parse(@response.body)
@attributes = @response_hash[name]
end
@retrieved = true
end
end
end
11 changes: 5 additions & 6 deletions lib/magicbell/api_resource_collection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MagicBell
class ApiResourceCollection
def initialize(client, query_params = {})
Expand All @@ -8,15 +10,12 @@ def initialize(client, query_params = {})

# @todo Add examples
def retrieve
@response = @client.get(
res = @client.get(
url,
query: @query_params
)
@response_hash = JSON.parse(response.body)
@resources = response_hash[name].map { |resource_attributes| resource_class.new(@client, resource_attributes) }
@retrieved = true

self
@retrieved = true if res.success?
res
end

def to_a
Expand Down
6 changes: 6 additions & 0 deletions lib/magicbell/api_resources/broadcast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module MagicBell
class Broadcast < ApiResource
end
end
2 changes: 2 additions & 0 deletions lib/magicbell/api_resources/notification.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MagicBell
class Notification < ApiResource
end
Expand Down
2 changes: 2 additions & 0 deletions lib/magicbell/api_resources/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MagicBell
class User < ApiResource
include ApiOperations
Expand Down
2 changes: 2 additions & 0 deletions lib/magicbell/api_resources/user_notification.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MagicBell
class UserNotification < ApiResource
class << self
Expand Down
Loading
Loading