repro-client 0.0.1

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: d0e76df6ad582a03ec2d95426330edf46312b504c932b7c7e42e3846ef81e94e
4
+ data.tar.gz: 3167b8c60a64241ff0d3603e0284ebdc77a20b520e6dbb9bb35e65cf3a567093
5
+ SHA512:
6
+ metadata.gz: c3656f352d1e381c077126893ac5279399c9cf97e65b6fe5b5bfdb2708827c369c50cdf587b606c041c44445139acaac785e9c9fa6d8a00509f3c5eeacf27107
7
+ data.tar.gz: 65fae39130625658fdd04a02a1229e785c6ca2f93514de5c46b3ddf734482e36eb952bbbdeea0aa4500acc90ce140651dae23e3c3e587d1b0b44ac9f70850b48
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Documentation cache and generated files:
17
+ /.yardoc/
18
+ /_yardoc/
19
+ /doc/
20
+ /rdoc/
21
+
22
+ ## Environment normalization:
23
+ /.bundle/
24
+ /vendor/bundle
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ - 2.4.3
6
+ before_install: gem install bundler -v 1.16.1 --no-document
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in repro-client.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Tatsuya Itakura
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,63 @@
1
+ [![Build Status](https://travis-ci.org/itkrt2y/repro-client.svg?branch=master)](https://travis-ci.org/itkrt2y/repro-client)
2
+
3
+ # repro-client
4
+
5
+ Repro API Client
6
+
7
+ ## Install
8
+
9
+ ```
10
+ $ gem install repro-client
11
+ ```
12
+
13
+ or add to your Gemfile
14
+
15
+ ```ruby
16
+ gem 'repro-client'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### [Send push notification](http://docs.repro.io/en/dev/push-api/index.html)
22
+
23
+ ```ruby
24
+ require 'repro/client'
25
+ client = Repro::Client.new('repro_api_token')
26
+ user_ids = [1, 2, 3]
27
+ payload = { message: 'Hello Repro!' }
28
+ client.push('push_id', user_ids, payload)
29
+ ```
30
+
31
+ #### [Payload format](http://docs.repro.io/en/dev/push-api/index.html#id7)
32
+
33
+ 1. [Standard format](http://docs.repro.io/en/dev/push-api/index.html#id8)
34
+
35
+ ```ruby
36
+ {
37
+ message: 'Hello Repro!',
38
+ deeplink_url: 'url',
39
+ sound: 'sound'
40
+ }
41
+ ```
42
+
43
+ 2. [Custom](http://docs.repro.io/en/dev/push-api/index.html#json)
44
+
45
+ You need to set the content as Hash
46
+
47
+ ### [Update user profile](http://docs.repro.io/en/dev/user-profile-api/index.html)
48
+
49
+ ```ruby
50
+ require 'repro/client'
51
+ client = Repro::Client.new('repro_api_token')
52
+ user_id = 'user-123'
53
+ payload = [{ key: 'Job', type: 'string', value: 'Developer' }]
54
+ client.update_user_profiles(user_id, payload)
55
+ ```
56
+
57
+ #### Payload format
58
+
59
+ [See Repro Official Document](http://docs.repro.io/en/dev/user-profile-api/index.html#user-profiles-payload)
60
+
61
+ ## Supported Ruby Versions
62
+
63
+ Ruby 2.4.0 or higher
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "repro/client"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/repro.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "repro/client"
4
+
5
+ module Repro
6
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "repro/connection"
4
+ require "repro/client/push"
5
+ require "repro/client/user_profile"
6
+ require "repro/response/rate_limit"
7
+
8
+ module Repro
9
+ class Client
10
+ include Repro::Connection
11
+ include Repro::Client::Push
12
+ include Repro::Client::UserProfile
13
+
14
+ attr_accessor :token
15
+
16
+ def initialize(token)
17
+ @token = token
18
+ end
19
+
20
+ def rate_limit
21
+ Repro::Response::RateLimit.from_response(last_response)
22
+ end
23
+
24
+ def retry_after
25
+ last_response && last_response.headers["Retry-After"]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Repro
4
+ class Client
5
+ module Push
6
+ def push(push_id, user_ids, options={})
7
+ post(push_endpoint, push_path(push_id), push_payload(user_ids, options))
8
+ end
9
+
10
+ def push_endpoint
11
+ "https://marketing.repro.io"
12
+ end
13
+
14
+ def push_path(push_id)
15
+ "/v1/push/#{push_id}/deliver"
16
+ end
17
+
18
+ def push_payload(user_ids, notification)
19
+ custom_payload = notification.delete(:custom_payload)
20
+
21
+ {
22
+ audience: { user_ids: user_ids },
23
+ notification: custom_payload ? { custom_payload: custom_payload } : notification
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Repro
4
+ class Client
5
+ module UserProfile
6
+ def update_user_profiles(user_id, user_profiles)
7
+ post(user_profile_endpoint, user_profile_path, user_profile_payload(user_id, user_profiles))
8
+ end
9
+
10
+ def user_profile_endpoint
11
+ "https://api.repro.io/v2/user_profiles"
12
+ end
13
+
14
+ def user_profile_path
15
+ ""
16
+ end
17
+
18
+ def user_profile_payload(user_id, user_profiles)
19
+ { user_id: user_id, user_profiles: user_profiles }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Repro
4
+ class Client
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+ require "repro/error"
6
+ require "repro/response"
7
+
8
+ module Repro
9
+ module Connection
10
+ def post(endpoint, path, payload)
11
+ @last_response = connection(endpoint).post do |request|
12
+ request.url path
13
+ request.body = JSON.generate(payload)
14
+ end
15
+
16
+ error = Repro::Error.from_response(last_response)
17
+ error ? raise(error) : Repro::Response.new(last_response)
18
+ end
19
+
20
+ def last_response
21
+ @last_response
22
+ end
23
+
24
+ private
25
+
26
+ def connection(endpoint)
27
+ @connection ||= {}
28
+ @connection[endpoint] ||= Faraday.new(endpoint) do |faraday|
29
+ faraday.headers["Content-Type"] = "application/json"
30
+ faraday.headers["X-Repro-Token"] = token
31
+ faraday.adapter Faraday.default_adapter
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Repro
4
+ class Error < StandardError
5
+ def self.from_response(response)
6
+ body = JSON.parse(response.body)
7
+
8
+ klass =
9
+ case response.status
10
+ when 400; error_for_400(body.dig("error", "code"))
11
+ when 401; Repro::Unauthorized
12
+ when 403; Repro::Forbidden
13
+ when 404; Repro::NotFound
14
+ when 422; Repro::UnprocessableEntity
15
+ when 429; Repro::TooManyRequests
16
+ when 400..499; Repro::ClientError
17
+ end
18
+
19
+ klass&.new(body)
20
+ end
21
+
22
+ def initialize(response_body)
23
+ error_message = response_body.dig("error", "messages")&.join("\n")
24
+ error_message ? super(error_message) : super
25
+ end
26
+
27
+ def self.error_for_400(code)
28
+ case code
29
+ when 1001; Repro::InvalidPayload
30
+ when 1002; Repro::NotRegisteredUser
31
+ when 1003; Repro::InvalidUserProfile
32
+ else; Repro::BadRequest
33
+ end
34
+ end
35
+ end
36
+
37
+ # 400-499
38
+ class ClientError < Error; end
39
+
40
+ # HTTP status code: 400
41
+ class BadRequest < ClientError; end
42
+
43
+ # HTTP status code: 400, error code: 1001
44
+ class InvalidPayload < ClientError; end
45
+
46
+ # HTTP status code: 400, error code: 1002
47
+ class NotRegisteredUser < ClientError; end
48
+
49
+ # HTTP status code: 400, error code: 1003
50
+ class InvalidUserProfile < ClientError; end
51
+
52
+ # HTTP status code: 401
53
+ class Unauthorized < ClientError
54
+ def to_s
55
+ "Please set your Repro API Token by `Repro.token = <token>` before calling API"
56
+ end
57
+ end
58
+
59
+ # HTTP status code: 403
60
+ class Forbidden < ClientError; end
61
+
62
+ # HTTP status code: 404
63
+ class NotFound < ClientError; end
64
+
65
+ # HTTP status code: 422
66
+ class UnprocessableEntity < ClientError; end
67
+
68
+ # HTTP status code: 429
69
+ class TooManyRequests < ClientError; end
70
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Repro
4
+ class Response
5
+ attr_reader :status, :body
6
+
7
+ def initialize(response)
8
+ @status = response.status
9
+ @body = JSON.parse(response.body)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Repro
4
+ class Response
5
+ class RateLimit < Struct.new(:limit, :remaining, :reset)
6
+ def self.from_response(response)
7
+ return unless response
8
+
9
+ new.tap do |rate_limit|
10
+ headers = response.headers
11
+ rate_limit.limit = headers["X-RateLimit-Limit"]
12
+ rate_limit.remaining = headers["X-RateLimit-Remaining"]
13
+ rate_limit.reset = headers["X-RateLimit-Reset"]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "repro/client/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "repro-client"
8
+ spec.version = Repro::Client::VERSION
9
+ spec.authors = ["Tatsuya Itakura"]
10
+ spec.email = ["itkrt2y.591721200@gmail.com"]
11
+ spec.summary = "Repro API Client"
12
+ spec.homepage = "https://github.com/itkrt2y/repro-client"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "faraday"
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "minitest"
27
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repro-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tatsuya Itakura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rake
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
+ description:
70
+ email:
71
+ - itkrt2y.591721200@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/repro.rb
85
+ - lib/repro/client.rb
86
+ - lib/repro/client/push.rb
87
+ - lib/repro/client/user_profile.rb
88
+ - lib/repro/client/version.rb
89
+ - lib/repro/connection.rb
90
+ - lib/repro/error.rb
91
+ - lib/repro/response.rb
92
+ - lib/repro/response/rate_limit.rb
93
+ - repro-client.gemspec
94
+ homepage: https://github.com/itkrt2y/repro-client
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.7.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Repro API Client
118
+ test_files: []