esputnik 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 802198ceb5a5916d2f852ef144328876799848205ac3da21e697034f7e054872
4
+ data.tar.gz: f69fc117fe5fa41dcf3e6c37f529623a28d424a2f5b59653195b6b6a4357102f
5
+ SHA512:
6
+ metadata.gz: bc18dd10717740026bb63c90dd483e8f600a6fbacd65a568af58ebf1fc3bf4181b64bba9930c0acc3e0d930b58e5fe591ffb5e64ea21d068d3df39c8636e0dbe
7
+ data.tar.gz: 391f92b9231c0750bb5c30a0635d69981b1868e4b8f6a04890136256854950446eb491c2028e1f68f162876fe13d2a6911785721d3aa051641ece57feadefd84
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ Style/AsciiComments:
8
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.2
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in esputnik.gemspec
8
+ gemspec
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ esputnik (0.0.1)
5
+ activemodel
6
+ faraday
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.2.3)
12
+ activesupport (= 5.2.3)
13
+ activesupport (5.2.3)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 0.7, < 2)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ addressable (2.6.0)
19
+ public_suffix (>= 2.0.2, < 4.0)
20
+ coderay (1.1.2)
21
+ concurrent-ruby (1.1.5)
22
+ crack (0.4.3)
23
+ safe_yaml (~> 1.0.0)
24
+ docile (1.3.2)
25
+ faraday (0.15.4)
26
+ multipart-post (>= 1.2, < 3)
27
+ hashdiff (0.4.0)
28
+ i18n (1.6.0)
29
+ concurrent-ruby (~> 1.0)
30
+ json (2.2.0)
31
+ method_source (0.9.2)
32
+ minitest (5.11.3)
33
+ multipart-post (2.1.1)
34
+ pry (0.12.2)
35
+ coderay (~> 1.1.0)
36
+ method_source (~> 0.9.0)
37
+ public_suffix (3.1.1)
38
+ rake (12.3.2)
39
+ safe_yaml (1.0.5)
40
+ simplecov (0.16.1)
41
+ docile (~> 1.1)
42
+ json (>= 1.8, < 3)
43
+ simplecov-html (~> 0.10.0)
44
+ simplecov-html (0.10.2)
45
+ thread_safe (0.3.6)
46
+ tzinfo (1.2.5)
47
+ thread_safe (~> 0.1)
48
+ vcr (5.0.0)
49
+ webmock (3.6.0)
50
+ addressable (>= 2.3.6)
51
+ crack (>= 0.3.2)
52
+ hashdiff (>= 0.4.0, < 2.0.0)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ bundler
59
+ esputnik!
60
+ minitest
61
+ pry
62
+ rake
63
+ simplecov
64
+ vcr
65
+ webmock
66
+
67
+ BUNDLED WITH
68
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Alexander Sviridov
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # Esputnik
2
+
3
+ Обертка над API маркетинговой системы eSputnik. Поддерживает минимальный необходимый объем функциональности:
4
+ - подписка новых пользователей
5
+ - обновление данных пользователей
6
+ - отправка событий
7
+
8
+ Пример
9
+
10
+ ```
11
+ client = Esputnik::Client.new(login: 'email@busfor.com', password: 'password', logger: Logger.new(STDOUT))
12
+
13
+ client.simple_contact_subscribe(email: 'test123@example.com', phone: '+79160001122', additional_data: [{id: 98748, value: 'м'}])
14
+
15
+ client.simple_contact_update(id: 564407320, additional_data: [{id: 98748, value: 'ж'}])
16
+ ```
17
+
18
+ Здесь email и phone - каналы, а такие данные как имя, фамилия и адрес пропущены.
19
+ id=98748 - id дополнительного поля Gender, настроенного для конкретного аккаунта eSputnik.
20
+
21
+ ```
22
+ client.event(event_type_key: 'test', key_value: 'test@example.com', params: {foo: 'bar')
23
+ ```
24
+
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/esputnik.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'esputnik'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'esputnik/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'esputnik'
9
+ spec.version = Esputnik::VERSION
10
+ spec.authors = ['Alexander Sviridov']
11
+ spec.email = ['alexander.sviridov@gmail.com']
12
+
13
+ spec.summary = 'Ruby wrapper for eSputnik'
14
+ spec.description = 'Ruby wrapper for eSputnik'
15
+ spec.homepage = 'https://github.com/busfor/esputnik'
16
+ spec.license = 'MIT'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'activemodel'
28
+ spec.add_dependency 'faraday'
29
+
30
+ spec.add_development_dependency 'bundler'
31
+ spec.add_development_dependency 'minitest'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'rake'
34
+ spec.add_development_dependency 'vcr'
35
+ spec.add_development_dependency 'webmock'
36
+ spec.add_development_dependency 'simplecov'
37
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'faraday'
5
+ require 'active_model'
6
+ require 'esputnik/version'
7
+ require 'esputnik/contact'
8
+ require 'esputnik/channel'
9
+ require 'esputnik/response'
10
+ require 'esputnik/client'
11
+
12
+ module Esputnik
13
+ class Error < StandardError; end
14
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esputnik
4
+ Channel = Struct.new(:type,
5
+ :value,
6
+ :device,
7
+ :web_push_subscription) do
8
+ include ActiveModel::Validations
9
+
10
+ validates_presence_of :type
11
+ validates_presence_of :value
12
+
13
+ def as_json
14
+ self.class.members.each_with_object({}) do |method_name, h|
15
+ value = public_send(method_name)
16
+ value = value.as_json if value.respond_to? :as_json
17
+ h[method_name.to_s.camelize(:lower)] = value if value.present?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esputnik
4
+ class Client
5
+ BASE_URL = 'https://esputnik.com/api/v1'
6
+
7
+ def initialize(login:, password:, logger: nil)
8
+ @login = login
9
+ @password = password
10
+ @logger = logger
11
+ end
12
+
13
+ def version
14
+ get '/version'
15
+ end
16
+
17
+ def send_event(event_type_key:, key_value:, params: {})
18
+ post '/event',
19
+ 'eventTypeKey' => event_type_key,
20
+ 'keyValue' => key_value,
21
+ 'params' => wrap_key_value(params)
22
+ end
23
+
24
+ # Метод, упрощающий работу с API eSputnik
25
+ # additional_data - [{id: <key_id>, value: value}, ... ]
26
+
27
+ def simple_contact_subscribe(email:, phone:, additional_data:)
28
+ contact = Esputnik::Contact.new
29
+ contact.channels = channels_pair(email: email, phone: phone)
30
+ contact.fields = additional_data
31
+
32
+ contact_subscribe(contact: contact)
33
+ end
34
+
35
+ def simple_contact_update(id:, email:, phone:, additional_data:)
36
+ contact = Esputnik::Contact.new
37
+ contact.channels = channels_pair(email: email, phone: phone)
38
+ contact.fields = additional_data
39
+
40
+ contact_update(id: id, contact: contact)
41
+ end
42
+
43
+ # Метод применяется для подключения форм подписки и в других случаях.
44
+ # Создает новые контакты, существующие обновляет. Создает в системе события для запуска сценариев.
45
+ # Новые контакты создаются в статусе "неподтвержденный", что позволяет реализовать double opt-in.
46
+
47
+ def contact_subscribe(contact:, groups: nil, form_type: nil)
48
+ raise Error, 'Esputnik::Contact expected' unless contact.is_a?(Esputnik::Contact)
49
+ raise Error, contact.errors unless contact.valid?
50
+
51
+ post '/contact/subscribe',
52
+ 'contact' => contact.as_json,
53
+ 'groups' => groups,
54
+ 'formType' => form_type
55
+ end
56
+
57
+ # Метод для обновления одного контакта. Новые контакты не создает.
58
+ # Чтобы обновить контакт этим методом надо знать id контакта в нашей системе. Подробнее.
59
+
60
+ def contact_update(id:, contact:, groups: nil, form_type: nil)
61
+ raise Error, 'Esputnik::Contact expected' unless contact.is_a?(Esputnik::Contact)
62
+ raise Error, contact.errors unless contact.valid?
63
+
64
+ put "/contact/#{id}",
65
+ 'contact' => contact.as_json,
66
+ 'groups' => groups,
67
+ 'formType' => form_type
68
+ end
69
+
70
+ private
71
+
72
+ def channels_pair(email:, phone:)
73
+ [
74
+ Esputnik::Channel.new.tap do |c|
75
+ c.type = 'email'
76
+ c.value = email
77
+ end,
78
+ Esputnik::Channel.new.tap do |c|
79
+ c.type = 'sms'
80
+ c.value = phone
81
+ end
82
+ ]
83
+ end
84
+
85
+ def get(path, **data)
86
+ send_request(:get, path, data)
87
+ end
88
+
89
+ def post(path, data)
90
+ send_request(:post, path, data)
91
+ end
92
+
93
+ def put(path, data)
94
+ send_request(:put, path, data)
95
+ end
96
+
97
+ def send_request(method, path, data)
98
+ response = http_client.send(method, BASE_URL + path) do |req|
99
+ req.body = data.to_json if data.any?
100
+ req.headers['Content-Type'] = 'application/json'
101
+ req.headers['Accept'] = 'application/json'
102
+ end
103
+
104
+ Response.new(response)
105
+ end
106
+
107
+ def http_client
108
+ @http_client ||= build_http_client
109
+ end
110
+
111
+ def build_http_client
112
+ connection = Faraday.new do |builder|
113
+ builder.request :url_encoded
114
+ builder.response :logger, @logger, bodies: true if @logger
115
+ builder.adapter Faraday.default_adapter
116
+ end
117
+
118
+ connection.basic_auth(@login, @password)
119
+ connection
120
+ end
121
+
122
+ def wrap_key_value(hash)
123
+ hash.each_with_object([]) { |(k, v), list| list << { name: k, value: v } }
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esputnik
4
+ Contact = Struct.new(:first_name,
5
+ :last_name,
6
+ :channels,
7
+ :address,
8
+ :fields,
9
+ :address_book_id,
10
+ :id,
11
+ :contact_key,
12
+ :orders_info,
13
+ :groups) do
14
+ include ActiveModel::Validations
15
+
16
+ validates_presence_of :channels
17
+
18
+ def as_json
19
+ hash = self.class.members.each_with_object({}) do |method_name, h|
20
+ value = public_send(method_name)
21
+ value = value.as_json if value.respond_to? :as_json
22
+ h[method_name.to_s.camelize(:lower)] = value if value.present?
23
+ end
24
+ hash['channels'] = channels&.map(&:as_json)
25
+ hash
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esputnik
4
+ class Response
5
+ def initialize(faraday_response)
6
+ @raw_response = faraday_response
7
+ end
8
+
9
+ def success?
10
+ @raw_response.success?
11
+ end
12
+
13
+ def json
14
+ JSON.parse(@raw_response.body) if @raw_response.body.present?
15
+ end
16
+
17
+ def raw_body
18
+ @raw_response.body
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esputnik
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: esputnik
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Sviridov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
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: vcr
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
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Ruby wrapper for eSputnik
140
+ email:
141
+ - alexander.sviridov@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rubocop.yml"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - esputnik.gemspec
156
+ - lib/esputnik.rb
157
+ - lib/esputnik/channel.rb
158
+ - lib/esputnik/client.rb
159
+ - lib/esputnik/contact.rb
160
+ - lib/esputnik/response.rb
161
+ - lib/esputnik/version.rb
162
+ homepage: https://github.com/busfor/esputnik
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubygems_version: 3.0.4
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Ruby wrapper for eSputnik
185
+ test_files: []