eventify_pro 0.1.0
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.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/LICENSE +21 -0
- data/README.md +115 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/eventify_pro.rb +5 -0
- data/lib/eventify_pro/client.rb +103 -0
- data/lib/eventify_pro/logger.rb +23 -0
- data/lib/eventify_pro/version.rb +5 -0
- data/spec/client_spec.rb +195 -0
- data/spec/eventify_pro_spec.rb +9 -0
- data/spec/spec_helper.rb +17 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 29aecc530d08b11f6c5426524fe151cf64776c40
|
4
|
+
data.tar.gz: 695c9e9bf367ea9a59c282f2bc7ae3f0246376d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3b7b006a0011b24ac0ad2bc8abfe9d58f447b309d37d43f636cfd165f8e842dc1921a54e231a728ec34cd15b95b47bcc5f2923a1ed4634b0f13bae52685aacff
|
7
|
+
data.tar.gz: d185e4cddc80384e2dafa67a71f9e5857d67db4282b4660e64f20fbabd2588ed2e56f6a7ac23f800f740aeacb4f08fd961ed9580ff78f7353a61c5c48456d44c
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at makagon87@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Sergii Makagon
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
[](https://travis-ci.org/smakagon/eventify_pro)
|
2
|
+
|
3
|
+
# EventifyPro
|
4
|
+
|
5
|
+
Client for [EventifyPro API](http://api.eventify.pro). Allows to publish events from Ruby-applications.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'eventify_pro'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install eventify_pro
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
___
|
25
|
+
### Getting Started:
|
26
|
+
This guide assumes you have created an account and obtained an API key from [EventifyPro](http://eventify.pro)
|
27
|
+
|
28
|
+
It's really easy to start publishing events with EventifyPro, just 2 lines of code:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
client = EventifyPro::Client.new(api_key: 'secret')
|
32
|
+
client.publish(type: 'OrderCreated', data: { order_id: 1, amount: 500 })
|
33
|
+
```
|
34
|
+
|
35
|
+
### Ruby On Rails
|
36
|
+
In a basic Ruby On Rails application you could create class under `lib` directory:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# lib/eventify.rb
|
40
|
+
|
41
|
+
class Eventify
|
42
|
+
def self.client
|
43
|
+
@client ||= EventifyPro::Client.new(api_key: 'secret', logger: Rails.logger)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.publish(event_type, data)
|
47
|
+
client.publish(type: event_type, data: data)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
Great, now to publish event from any place of your app use:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
Eventify.publish('UserSignedUp', user_id: 1, first_name: 'John', last_name: 'Doe')
|
55
|
+
```
|
56
|
+
|
57
|
+
* `type`: type of the event
|
58
|
+
|
59
|
+
* `data`: key and value pairs pertaining to the event
|
60
|
+
|
61
|
+
Don't forget to enable loading of the `lib` directory in application.rb file:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# config/application.rb
|
65
|
+
|
66
|
+
config.autoload_paths << Rails.root.join('lib')
|
67
|
+
```
|
68
|
+
___
|
69
|
+
#### Example:
|
70
|
+
```ruby
|
71
|
+
class OrdersController < ApplicationController
|
72
|
+
def create
|
73
|
+
@order = Order.new(params[:order])
|
74
|
+
|
75
|
+
if @order.save
|
76
|
+
Eventify.publish(type: 'OrderCreated', data: { order_id: @order.id, amount: @order.amount })
|
77
|
+
redirect_to @order, notice: 'Post was successfully created.'
|
78
|
+
else
|
79
|
+
render :new
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
___
|
85
|
+
## Configuration
|
86
|
+
|
87
|
+
#### `raise_errors:`
|
88
|
+
* By default, `EventifyPro::Client` will swallow errors. It will return `true` or `false` depending on the result of publishing.
|
89
|
+
* It's possible to pass `raise_errors: true`. In that case client will throw `EventifyPro::Error` exception if something went wrong.
|
90
|
+
|
91
|
+
Example:
|
92
|
+
```ruby
|
93
|
+
client = EventifyPro::Client.new(api_key: 'secret', raise_errors: true)
|
94
|
+
begin
|
95
|
+
client.publish(type: 'OrderCreated', data: { order_id: 1, amount: 1500 })
|
96
|
+
rescue EventifyPro::Error
|
97
|
+
# exception handling
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
#### `logger`
|
102
|
+
* By default, client will use STDOUT to print error details.
|
103
|
+
* You can provide any logger that responds to `.info(message)`. For example `Rails.logger`.
|
104
|
+
|
105
|
+
Example:
|
106
|
+
```ruby
|
107
|
+
client = EventifyPro::Client.new(api_key: 'secret', logger: Rails.logger)
|
108
|
+
```
|
109
|
+
|
110
|
+
## License
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
112
|
+
|
113
|
+
## Code of Conduct
|
114
|
+
|
115
|
+
Everyone interacting in the Eventify project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/smakagon/eventify/blob/master/CODE_OF_CONDUCT.md).
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'eventify'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/eventify_pro.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# Eventify
|
8
|
+
module EventifyPro
|
9
|
+
Error = Class.new(StandardError)
|
10
|
+
ServiceUnavailableError = Class.new(Error)
|
11
|
+
|
12
|
+
# EventifyPro::Client allows to publish events.
|
13
|
+
#
|
14
|
+
# To instantiate very basic client use this code:
|
15
|
+
# `client = EventifyPro::Client.new(api_key: 'personal_api_key')`
|
16
|
+
# Then use it this way:
|
17
|
+
# `client.publish(type: 'OrderPosted', data: { order_id: 10, amount: 3000 })`
|
18
|
+
#
|
19
|
+
# Configuration
|
20
|
+
#
|
21
|
+
# raise_errors:
|
22
|
+
# By default client will not throw any exception. `publish` will return either
|
23
|
+
# `true` or `false` depending on the result of publishing.
|
24
|
+
# It's possible to configure client to throw an `EventifyPro::Error`
|
25
|
+
# if exception wasn't published.
|
26
|
+
# `EventifyPro::Client.new(api_key: 'personal_api_key', raise_errors: true)`
|
27
|
+
#
|
28
|
+
# logger:
|
29
|
+
# By default client will write errors to STDOUT, but it's possible to pass
|
30
|
+
# any custom logger that responds to .info(message):
|
31
|
+
# `EventifyPro::Client.new(api_key: 'personal_api_key', logger: Rails.logger)`
|
32
|
+
class Client
|
33
|
+
BASE_URI = 'http://api.eventify.pro/v1'
|
34
|
+
|
35
|
+
def initialize(api_key: nil, raise_errors: false, logger: EventifyPro::DefaultLogger.new) # rubocop:disable LineLength
|
36
|
+
@api_key = api_key || ENV['EVENTIFY_PRO_API_KEY']
|
37
|
+
validate_api_key_presence!
|
38
|
+
|
39
|
+
@raise_errors = raise_errors
|
40
|
+
@logger = logger
|
41
|
+
end
|
42
|
+
|
43
|
+
def publish(type:, data:)
|
44
|
+
response = post_request('events', type, data)
|
45
|
+
|
46
|
+
error_message = response['error_message'] || ''
|
47
|
+
raise Error, error_message unless error_message.empty?
|
48
|
+
|
49
|
+
true
|
50
|
+
rescue => e
|
51
|
+
process_error(e, 'publish', type: type, data: data)
|
52
|
+
false
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
attr_reader :api_key, :raise_errors, :logger
|
58
|
+
|
59
|
+
def process_error(error, method, params)
|
60
|
+
error = Error.new('Could not publish event') unless error.is_a?(Error)
|
61
|
+
log_error(error.message, method, params)
|
62
|
+
|
63
|
+
raise error if raise_errors
|
64
|
+
end
|
65
|
+
|
66
|
+
def log_error(message, method, params)
|
67
|
+
message = "[EVENTIFY_PRO] ##{method} call returned error: #{message}\nParams: #{params}" # rubocop:disable LineLength
|
68
|
+
return logger.info(message) if logger.respond_to?(:info)
|
69
|
+
|
70
|
+
raise NotImplementedError, 'Logger should respond to #info(message) call'
|
71
|
+
end
|
72
|
+
|
73
|
+
def post_request(end_point, type, data)
|
74
|
+
url = "#{BASE_URI}/#{end_point}"
|
75
|
+
|
76
|
+
uri = URI.parse(url)
|
77
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
78
|
+
request = Net::HTTP::Post.new(uri.path, headers)
|
79
|
+
|
80
|
+
request.set_form_data(type: type, data: data.to_json)
|
81
|
+
|
82
|
+
response = http.request(request)
|
83
|
+
JSON.parse(response.body)
|
84
|
+
rescue JSON::ParserError
|
85
|
+
raise Error, 'Could not process response from EventifyPro'
|
86
|
+
rescue
|
87
|
+
raise ServiceUnavailableError, 'EventifyPro is currently unavaliable'
|
88
|
+
end
|
89
|
+
|
90
|
+
def headers
|
91
|
+
{
|
92
|
+
'Authorization' => api_key,
|
93
|
+
'Content-Type' => 'application/json; charset=utf-8'
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
def validate_api_key_presence!
|
98
|
+
return unless api_key.to_s.empty?
|
99
|
+
|
100
|
+
raise Error, 'Please provide api_key param or set EVENTIFY_PRO_API_KEY environment variable' # rubocop:disable LineLength
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module EventifyPro
|
6
|
+
# Default Logger for EventifyPro::Client.
|
7
|
+
# Used if logger is not provided during client initialization
|
8
|
+
class DefaultLogger
|
9
|
+
def initialize
|
10
|
+
@logger = Logger.new(STDOUT)
|
11
|
+
@logger.level = Logger::INFO
|
12
|
+
@logger.datetime_format = '%Y-%m-%d %H:%M:%S '
|
13
|
+
rescue
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def info(message)
|
18
|
+
@logger.info(message)
|
19
|
+
rescue
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe EventifyPro::Client do
|
6
|
+
let(:base_uri) { EventifyPro::Client::BASE_URI }
|
7
|
+
|
8
|
+
context '#publish' do
|
9
|
+
let(:event_type) { 'ProfileCreated' }
|
10
|
+
let(:event_data) { { name: 'John', email: 'john@doe.com' } }
|
11
|
+
|
12
|
+
let(:expected_request) do
|
13
|
+
{
|
14
|
+
body: { type: event_type, data: event_data.to_json },
|
15
|
+
headers: {
|
16
|
+
'Authorization' => 'secret',
|
17
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:response) do
|
23
|
+
'{
|
24
|
+
"id": "a98185f9-61a2-44f2-93d0-1dfd8f7a7790",
|
25
|
+
"type": "ProfileCreated",
|
26
|
+
"data": "{\"name\": \"John\", \"email\": \"john@doe.com\"}",
|
27
|
+
"created_at": "2017-06-07 18:28:59"
|
28
|
+
}'
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:response_with_error) { '{ "error_message": "Invalid API key" }' }
|
32
|
+
let(:invalid_response) { 'foo' }
|
33
|
+
|
34
|
+
context 'with raise_errors = true' do
|
35
|
+
let(:client) do
|
36
|
+
described_class.new(api_key: 'secret', raise_errors: true)
|
37
|
+
end
|
38
|
+
|
39
|
+
context '#publish' do
|
40
|
+
context 'with successful response from API' do
|
41
|
+
it 'returns true' do
|
42
|
+
stub_request(:post, "#{base_uri}/events")
|
43
|
+
.with(expected_request).to_return(body: response, status: 200)
|
44
|
+
|
45
|
+
expect(
|
46
|
+
client.publish(type: event_type, data: event_data)
|
47
|
+
).to be_truthy
|
48
|
+
|
49
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
50
|
+
.with(expected_request).once
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'with error response from API' do
|
55
|
+
it 'raises an EventifyPro::Error' do
|
56
|
+
stub_request(:post, "#{base_uri}/events")
|
57
|
+
.with(expected_request)
|
58
|
+
.to_return(body: response_with_error, status: 401)
|
59
|
+
|
60
|
+
expect do
|
61
|
+
client.publish(type: event_type, data: event_data)
|
62
|
+
end.to raise_error(EventifyPro::Error, 'Invalid API key')
|
63
|
+
|
64
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
65
|
+
.with(expected_request).once
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'with invalid response from API' do
|
70
|
+
it 'raises an EventifyPro::Error' do
|
71
|
+
stub_request(:post, "#{base_uri}/events")
|
72
|
+
.with(expected_request)
|
73
|
+
.to_return(body: invalid_response, status: 401)
|
74
|
+
|
75
|
+
expect do
|
76
|
+
client.publish(type: event_type, data: event_data)
|
77
|
+
end.to raise_error(
|
78
|
+
EventifyPro::Error, 'Could not process response from EventifyPro'
|
79
|
+
)
|
80
|
+
|
81
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
82
|
+
.with(expected_request).once
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'when API is unavailable' do
|
87
|
+
it 'raises an EventifyPro::ServiceUnavailableError' do
|
88
|
+
stub_request(:post, "#{base_uri}/events").to_timeout
|
89
|
+
expect do
|
90
|
+
client.publish(type: event_type, data: event_data)
|
91
|
+
end.to raise_error(
|
92
|
+
EventifyPro::ServiceUnavailableError,
|
93
|
+
'EventifyPro is currently unavaliable'
|
94
|
+
)
|
95
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'with raise_errors = false (default)' do
|
102
|
+
let(:client) { described_class.new(api_key: 'secret') }
|
103
|
+
|
104
|
+
context '#publish' do
|
105
|
+
context 'with successful response from API' do
|
106
|
+
it 'returns true' do
|
107
|
+
stub_request(:post, "#{base_uri}/events")
|
108
|
+
.with(expected_request).to_return(body: response, status: 200)
|
109
|
+
|
110
|
+
expect(
|
111
|
+
client.publish(type: event_type, data: event_data)
|
112
|
+
).to be_truthy
|
113
|
+
|
114
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
115
|
+
.with(expected_request).once
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'with error response from API' do
|
120
|
+
it 'returns false' do
|
121
|
+
stub_request(:post, "#{base_uri}/events")
|
122
|
+
.with(expected_request)
|
123
|
+
.to_return(body: response_with_error, status: 401)
|
124
|
+
|
125
|
+
expect(client.publish(type: event_type, data: event_data)).to(
|
126
|
+
be_falsey
|
127
|
+
)
|
128
|
+
|
129
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
130
|
+
.with(expected_request).once
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'with invalid response from API' do
|
135
|
+
it 'returns false' do
|
136
|
+
stub_request(:post, "#{base_uri}/events")
|
137
|
+
.with(expected_request)
|
138
|
+
.to_return(body: invalid_response, status: 401)
|
139
|
+
|
140
|
+
expect(client.publish(type: event_type, data: event_data)).to(
|
141
|
+
be_falsey
|
142
|
+
)
|
143
|
+
|
144
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
145
|
+
.with(expected_request).once
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when API is unavailable' do
|
150
|
+
it 'returns false' do
|
151
|
+
stub_request(:post, "#{base_uri}/events").to_timeout
|
152
|
+
expect(client.publish(type: event_type, data: event_data)).to(
|
153
|
+
be_falsey
|
154
|
+
)
|
155
|
+
expect(WebMock).to have_requested(:post, "#{base_uri}/events")
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'logger' do
|
162
|
+
let(:expected_message) { "[EVENTIFY_PRO] #publish call returned error: Invalid API key\nParams: {:type=>\"ProfileCreated\", :data=>{:name=>\"John\", :email=>\"john@doe.com\"}}" } # rubocop:disable LineLength
|
163
|
+
|
164
|
+
before do
|
165
|
+
stub_request(:post, "#{base_uri}/events")
|
166
|
+
.with(expected_request)
|
167
|
+
.to_return(body: response_with_error, status: 401)
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'with default logger' do
|
171
|
+
let(:client) { described_class.new(api_key: 'secret') }
|
172
|
+
|
173
|
+
it 'calls .info with error message' do
|
174
|
+
expect_any_instance_of(EventifyPro::DefaultLogger).to(
|
175
|
+
receive(:info).with(expected_message)
|
176
|
+
)
|
177
|
+
client.publish(type: event_type, data: event_data)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context 'with custom logger' do
|
182
|
+
let(:logger) { double('logger', info: true) }
|
183
|
+
|
184
|
+
let(:client) do
|
185
|
+
described_class.new(api_key: 'secret', logger: logger)
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'calls .info with error message' do
|
189
|
+
expect(logger).to receive(:info).with(expected_message)
|
190
|
+
client.publish(type: event_type, data: event_data)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'webmock/rspec'
|
5
|
+
require 'eventify_pro'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
# Enable flags like --only-failures and --next-failure
|
9
|
+
config.example_status_persistence_file_path = '.rspec_status'
|
10
|
+
|
11
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
12
|
+
config.disable_monkey_patching!
|
13
|
+
|
14
|
+
config.expect_with :rspec do |c|
|
15
|
+
c.syntax = :expect
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eventify_pro
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergii Makagon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.49'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.49'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.10'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.10'
|
97
|
+
description: Allows to publish events from Ruby applications
|
98
|
+
email:
|
99
|
+
- makagon87@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- CODE_OF_CONDUCT.md
|
105
|
+
- LICENSE
|
106
|
+
- README.md
|
107
|
+
- bin/console
|
108
|
+
- bin/setup
|
109
|
+
- lib/eventify_pro.rb
|
110
|
+
- lib/eventify_pro/client.rb
|
111
|
+
- lib/eventify_pro/logger.rb
|
112
|
+
- lib/eventify_pro/version.rb
|
113
|
+
- spec/client_spec.rb
|
114
|
+
- spec/eventify_pro_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
homepage: https://github.com/smakagon/eventify_pro
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.6.10
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Client for EventifyPro API
|
140
|
+
test_files:
|
141
|
+
- spec/client_spec.rb
|
142
|
+
- spec/eventify_pro_spec.rb
|
143
|
+
- spec/spec_helper.rb
|