zenps-ruby 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5e271f0e89ea5c2b50ee41b4b32341089ad9c806adcf65083de65fe04e01fa72
4
+ data.tar.gz: 0a5adda8c8afecc4f5f24ef3eb9f29623c9f749ebcb6db2ddfa6694bd2b05337
5
+ SHA512:
6
+ metadata.gz: f23b98117c2e18c3a8944dca873571a7b91a0eed744814c2ac6d11ab33d1e8c0d9e4a543b9fc89f206448f788f4435bc1dfac4dd0aa10ac27ec36f262db6d34d
7
+ data.tar.gz: ae19e9a9a630523b87eb3a0195ae5c4ffe70ff2ce43b2d6a0d799fb6b1a629531ed2bd5278bd39a798ec22308d3c3e74b034328aadc8133d5d18a65b011a59bd
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019 Alexis Philippart de Foy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ [![Codeship Status for aphilippartd/zenps-ruby](https://app.codeship.com/projects/beb50360-2413-0137-0f16-0e0a32caa97a/status?branch=master)](https://app.codeship.com/projects/330078)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/065b707a3ccd884c40d6/maintainability)](https://codeclimate.com/github/aphilippartd/zenps-ruby/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/065b707a3ccd884c40d6/test_coverage)](https://codeclimate.com/github/aphilippartd/zenps-ruby/test_coverage)
4
+
5
+ # zenps-ruby
6
+
7
+ Wrapper for Zenps API
8
+
9
+ ## Installation
10
+ ```sh
11
+ gem install zenps-ruby
12
+ ```
13
+
14
+ Or with bundler
15
+
16
+ ```ruby
17
+ gem 'zenps-ruby'
18
+ ```
19
+
20
+
21
+ ## Configuration
22
+
23
+ This library needs to be configured with a Zenps Api key. Configuration can be performed as follows using a hash
24
+
25
+ ```ruby
26
+ Zenps.configure(zenps_key: 'YOUR_ZENPS_KEY')
27
+ ```
28
+
29
+ or with a yml configuration file
30
+
31
+ ```ruby
32
+ Zenps.configure_with('path/to/your/configuration/file.yml')
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ This wrapper allows you to send NPS surveys to your user in batch...
38
+
39
+ ```ruby
40
+ Zenps::Survey.call([user_1, user_2])
41
+ ```
42
+
43
+ ...or on a per-user basis
44
+
45
+ ```ruby
46
+ Zenps::Survey.call(user)
47
+ ```
48
+
49
+ A user subject can either be:
50
+
51
+ - A string (email of user to be surveyed)
52
+ - A hash containing the following keys:
53
+ - email (compulsory)
54
+ - locale (optional --> defaults to `en`)
55
+ - An object that responds to
56
+ - email method (compulsory)
57
+ - locale (optional --> defaults to `en`)
58
+
59
+ This allows for the gem to be used eg. in a rails application with User models that respond to the email method (and/or locale) as follows
60
+
61
+ ```ruby
62
+ Zenps::Survey.call(User.limit(10))
63
+ ```
64
+
65
+ but also in a more simple way as follows
66
+
67
+ ```ruby
68
+ Zenps::Survey.call('john.doe@acme.com')
69
+ ```
70
+
71
+ The following options are available on the Survey call method:
72
+
73
+ - locale (String) - Overwrites the user's AND default locale
74
+ - event (String) - Typically the event triggering the NPS survey
75
+ - tags (Array) - Tags giving you granularity in your analytics
76
+
77
+ Example:
78
+
79
+ ```ruby
80
+ Zenps::Survey.call(user, locale: 'fr', event: 'sign_up', tags: ['man', 'facebook'])
81
+ ```
82
+
83
+
84
+ ## Contributing to pipedrive-ruby
85
+
86
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
87
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
88
+ * Fork the project.
89
+ * Start a feature/bugfix branch.
90
+ * Commit and push until you are happy with your contribution.
91
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
92
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
93
+
94
+ ## License
95
+
96
+ This gem is released under the [MIT License](http://www.opensource.org/licenses/MIT).
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = zenps-ruby
2
+
3
+ Ruby wrapper for the Zenps API
4
+
5
+ == Contributing to zenps-ruby
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2019 Alexis Philippart de Foy. See LICENSE.txt for
18
+ further details.
data/lib/zenps-ruby.rb ADDED
@@ -0,0 +1,47 @@
1
+
2
+ require 'yaml'
3
+ require 'logger'
4
+ require 'zenps/client.rb'
5
+ require 'zenps/payload.rb'
6
+ require 'zenps/survey.rb'
7
+ module Zenps
8
+ # Configuration defaults
9
+ @config = {
10
+ zenps_key: nil
11
+ }
12
+
13
+ @valid_config_keys = @config.keys
14
+
15
+ @logger = Logger.new(STDOUT)
16
+
17
+ # Configure through hash
18
+ def self.configure(options = {})
19
+ options.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
20
+ end
21
+
22
+ # Configure through yaml file
23
+ def self.configure_with(path_to_yaml_file)
24
+ begin
25
+ config = YAML::load(IO.read(path_to_yaml_file))
26
+ rescue Errno::ENOENT
27
+ raise ConfigurationPathError
28
+ end
29
+ configure(config)
30
+ end
31
+
32
+ def self.config
33
+ @config
34
+ end
35
+
36
+ class KeyMissingError < StandardError
37
+ def initialize(msg="Zenps configuration key missing")
38
+ super
39
+ end
40
+ end
41
+
42
+ class ConfigurationPathError < StandardError
43
+ def initialize(msg="YAML configuration file couldn't be found.")
44
+ super
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,83 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module Zenps
6
+ class Client
7
+
8
+ def initialize
9
+ check_configuration
10
+ end
11
+
12
+ def call(options={})
13
+ @options = options
14
+ perform_request
15
+ expose_response
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :options, :response
21
+
22
+ def check_configuration
23
+ raise KeyMissingError if ::Zenps.config[:zenps_key].nil?
24
+ end
25
+
26
+ def perform_request
27
+ request = Net::HTTP::Post.new(uri, header)
28
+ request.body = body
29
+ @response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) {|http| http.request request}
30
+ end
31
+
32
+ def expose_response
33
+ {
34
+ email: email,
35
+ code: response.code.to_i,
36
+ body: response.body
37
+ }
38
+ end
39
+
40
+ def body
41
+ {
42
+ to: {
43
+ email: email,
44
+ },
45
+ locale: locale,
46
+ event: event,
47
+ tags: tags
48
+ }.compact.to_json
49
+ end
50
+
51
+ def uri
52
+ @uri ||= URI.parse(end_point)
53
+ end
54
+
55
+ def header
56
+ @header ||= {
57
+ 'Content-Type': 'text/json',
58
+ 'Authorization': ::Zenps.config[:zenps_key]
59
+ }
60
+ end
61
+
62
+ def email
63
+ options[:email]
64
+ end
65
+
66
+ def locale
67
+ options['locale'] || options[:locale] || 'en'
68
+ end
69
+
70
+ def event
71
+ options['event'] || options[:event]
72
+ end
73
+
74
+ def tags
75
+ (options['tags'] || options[:tags] || []).join(', ')
76
+ rescue NoMethodError
77
+ end
78
+
79
+ def end_point
80
+ 'https://api.zenps.io/nps/v1/surveys'
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,46 @@
1
+ module Zenps
2
+ class Payload
3
+ # Returns array of hashes from:
4
+ # - (array of) string(s)
5
+ # - (array of) hash(es)
6
+ # - (array of) object(s)
7
+ def get(subjects)
8
+ @subjects = subjects
9
+ return payload
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :subjects
15
+
16
+ def payload
17
+ @subjects = [subjects] unless array_but_not_hash?
18
+ return get_payload
19
+ end
20
+
21
+ def get_payload
22
+ return subjects.map do |subject|
23
+ {
24
+ email: get_email(subject),
25
+ locale: get_locale(subject)
26
+ }.compact
27
+ end
28
+ end
29
+
30
+ def get_email(object)
31
+ return object if object.class == String
32
+ return object[:email] || object['email'] if object.class == Hash
33
+ return object.email if object.respond_to? :email
34
+ end
35
+
36
+ def get_locale(object)
37
+ return if object.class == String
38
+ return object[:locale] || object['locale'] if object.class == Hash
39
+ return object.locale if object.respond_to? :locale
40
+ end
41
+
42
+ def array_but_not_hash?
43
+ subjects.respond_to?(:each) && !subjects.respond_to?(:keys)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,32 @@
1
+ module Zenps
2
+ class Survey
3
+ # Takes list of subjects and sends NPS survey to all subjects
4
+ def self.call(subjects, options={})
5
+ new.call(subjects, options)
6
+ end
7
+
8
+ def call(subjects, options={})
9
+ @subjects = subjects
10
+ @options = options
11
+ perform_requests
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :subjects, :options
17
+
18
+ def perform_requests
19
+ payload_getter.get(subjects).map do |payload|
20
+ api_client.call(payload.merge(options))
21
+ end
22
+ end
23
+
24
+ def payload_getter
25
+ @payload_getter ||= Payload.new
26
+ end
27
+
28
+ def api_client
29
+ @api_client ||= Client.new
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zenps-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexis Philippart de Foy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.5.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.5.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.5.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: juwelier
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-console
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Ruby wrapper for Zenps
112
+ email: alexis.philippartdefoy@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.md
118
+ - README.rdoc
119
+ files:
120
+ - LICENSE.txt
121
+ - README.md
122
+ - README.rdoc
123
+ - lib/zenps-ruby.rb
124
+ - lib/zenps/client.rb
125
+ - lib/zenps/payload.rb
126
+ - lib/zenps/survey.rb
127
+ homepage: http://github.com/aphilippartd/zenps-ruby
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubygems_version: 3.0.3
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Ruby wrapper for Zenps
150
+ test_files: []