repro-api-client 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f93516229f022a4a3074c01bb9d405902d09e500
4
+ data.tar.gz: 0a8bd27ad22f64f2ba0f299b7cabd08d208a89d0
5
+ SHA512:
6
+ metadata.gz: '014969b8fbc9feb499814b5319cd9caf3631754f958e838e14f03f4da8ccb39a969716edd549b45c06623ea7030c79f8f213e93b196c576ca44e535622ec74d3'
7
+ data.tar.gz: 4b0f02d32ac468b1b87b7eadd1e6fff110ee45415e7b41e5a9e2fbb3c5e7baecbe1f042124586258bd074d2b5becee442bc76134438f77ddd042e53cb832e1cc
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in repro-api-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Takafumi Hirata
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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Repro::Api::Client
2
+
3
+ Repro Push API Client for ruby
4
+
5
+ see: http://docs.repro.io/ja/dev/push-api
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'repro-api-client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install repro-api-client
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'repro/api/client'
27
+
28
+ Repro::Api::Client.configure do |options|
29
+ options[:token] = 'YOUR REPRO API MARKETING TOKEN'
30
+ end
31
+
32
+ Repro::Api::Client.push_deliver('PUSH ID', ['USER ID'], {body: 'body'})
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Takafumi Hirata/repro-api-client.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "repro/api/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
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'repro/api/client'
4
+ require 'json'
5
+ require 'optparse'
6
+
7
+ class CLI
8
+ def initialize(args)
9
+ @token = @push_id = @body = @title = @user_ids = @json = nil
10
+ opts = OptionParser.new
11
+ opts.on('-t', '--token TOKEN', 'Repro API Marketing Token') {|v| @token = v}
12
+ opts.on('-i', '--push-id PUSH_ID', 'Repro Push ID') {|v| @push_id = v}
13
+ opts.on('-u', '--users USER_ID[,USER_ID,...]', 'Comma separated user ids') {|v| @user_ids = v.split(',')}
14
+ opts.on('--title TITLE', 'Title in push notification') {|v| @title = v}
15
+ opts.on('-b', '--body BODY', 'Message body in push notification') {|v| @body = v}
16
+ opts.on('--json JSON', 'JSON data to use as custom payload') {|v| @payload = JSON.parse(v, symbolize_names: true)}
17
+ opts.on('--file FILE', 'FILE that contains json data as custom payload') {|v| @payload = JSON.parse(File.read(v, symbolize_names: true))}
18
+ opts.parse!(args)
19
+ end
20
+
21
+ def run
22
+ raise '-t,--token is required' if @token.nil?
23
+ raise '-i,--push-id is required' if @push_id.nil?
24
+ raise '-u,--users are required' if @user_ids.nil?
25
+ raise '-b,--body or --json or --file is required' if @body.nil? && @payload.nil?
26
+
27
+ Repro::Api::Client.configure do |c|
28
+ c[:token] = @token
29
+ end
30
+
31
+ if @payload
32
+ custom_payload = @payload
33
+ else
34
+ custom_payload = {}
35
+ custom_payload[:body] = @body if @body
36
+ custom_payload[:title] = @title if @title
37
+ end
38
+
39
+ begin
40
+ response = Repro::Api::Client.push_deliver(@push_id, @user_ids, custom_payload)
41
+ puts response.body
42
+ rescue => ex
43
+ puts ex.message
44
+ end
45
+ end
46
+ end
47
+
48
+ CLI.new(ARGV).run
@@ -0,0 +1,7 @@
1
+ module Repro
2
+ module Api
3
+ module Client
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,125 @@
1
+ require 'repro/api/client/version'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'repro/api/request_error'
5
+ require 'repro/api/response'
6
+
7
+ module Repro
8
+ module Api
9
+
10
+ module Client
11
+
12
+ API_VERSION = 'v1'.freeze
13
+ API_ENDPOINT = "https://marketing.repro.io/#{API_VERSION}".freeze
14
+
15
+ @@options = {
16
+ method: 'post'
17
+ }
18
+
19
+ def self.options
20
+ @@options
21
+ end
22
+
23
+ def self.options=(opts)
24
+ @@options = opts
25
+ end
26
+
27
+ def self.configure
28
+ raise ArgumentError, 'Block is required.' unless block_given?
29
+ yield @@options
30
+ end
31
+
32
+ # /push/:push_id/deliver
33
+ # @param [String] push_id Push ID in Repro
34
+ # @param [Array] user_ids UserIDs to send push notification
35
+ # @option opts [String] :body Message body for push notification. (required)
36
+ # @option opts [String] :title Title for push notification
37
+ # @option opts [Integer] :badge Badge number
38
+ # @option opts [String] :sound Sound file name for notification
39
+ # @option opts [String] :url Custom url or Deep Link
40
+ # @option opts [Hash] :attachment Attachment for rich push. Hash must have keys :type and :url.
41
+ def self.push_deliver(push_id, user_ids, opts = {})
42
+ raise ArgumentError, ':body is required.' unless opts[:body]
43
+ opts[:path] = "/push/#{push_id}/deliver"
44
+ body = { audience: { user_ids: user_ids } }
45
+ body[:notification] = {custom_payload: build_payload(opts)}
46
+ opts[:body] = body
47
+ send_request(opts)
48
+ end
49
+
50
+ class << self
51
+
52
+ def token
53
+ @@options[:token]
54
+ end
55
+
56
+ def user_agent
57
+ @@options[:user_agent] || "Repro Ruby Client/#{VERSION}"
58
+ end
59
+
60
+ def endpoint
61
+ @@options[:endpoint] || API_ENDPOINT
62
+ end
63
+
64
+ private
65
+
66
+ def build_payload(opts)
67
+ payload = {aps: {alert: {}}}
68
+ payload[:aps][:alert][:body] = opts.delete(:body)
69
+ payload[:aps][:alert][:title] = opts.delete(:title)
70
+ payload[:aps][:badge] = opts.delete(:badge) if opts[:badge]
71
+ payload[:aps][:sound] = opts.key?(:sound) ? opts.delete(:sound) : 'default'
72
+ payload[:rpr_url] = opts.delete(:url) if opts[:url]
73
+ if opts[:attachment]
74
+ payload[:aps]['mutable-content'] = 1
75
+ payload[:rpr_attachment] = opts.delete(:attachment)
76
+ end
77
+ JSON.generate(payload)
78
+ end
79
+
80
+ def send_request(opts)
81
+ opts = options.merge(opts) if options
82
+ http_response = call_api(opts)
83
+ res = Response.new(http_response)
84
+ unless http_response.is_a? Net::HTTPSuccess
85
+ err_msg = "HTTP Response: #{res.code} #{res.message}"
86
+ err_msg += " - #{res.error}" if res.error
87
+ raise Repro::Api::RequestError, err_msg
88
+ end
89
+ res
90
+ end
91
+
92
+ def call_api(opts)
93
+ if opts.delete(:method) == 'post'
94
+ request_url = prepare_url(path: opts.delete(:path))
95
+ request = prepare_request(request_url, body: opts.delete(:body))
96
+ Net::HTTP.start(request_url.host, request_url.port, use_ssl: true) do |http|
97
+ http.request(request)
98
+ end
99
+ else
100
+ request_url = prepare_url(opts)
101
+ Net::HTTP.get_response(request_url)
102
+ end
103
+ end
104
+
105
+ def prepare_request(uri, opts)
106
+ request = Net::HTTP::Post.new(uri.path)
107
+ request['Accept'] = 'application/json'
108
+ request['Content-Type'] = 'application/json'
109
+ request['X-Repro-Token'] = token
110
+ request['User-Agent'] = user_agent
111
+ request.body = JSON.generate(opts[:body])
112
+ request
113
+ end
114
+
115
+ def prepare_url(opts)
116
+ path = opts.delete(:path)
117
+ query_string = opts.empty? ? '' : '?' + URI.encode_www_form(opts)
118
+ URI.parse(endpoint + path + query_string)
119
+ end
120
+
121
+ end
122
+
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,6 @@
1
+ module Repro
2
+ module Api
3
+ class RequestError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ class Response
4
+
5
+ attr_reader :body, :message, :error
6
+
7
+ def initialize(res)
8
+ @code = res.code
9
+ @message = res.is_a?(Net::HTTPSuccess) ? nil : res.message
10
+ @body = JSON.parse(res.body)
11
+ @error = if @body['error'] && @body['error']['messages']
12
+ if @body['error']['messages'].is_a?(Array)
13
+ @body['error']['messages'].join('. ')
14
+ else
15
+ @body['error']['messages']
16
+ end
17
+ else
18
+ @body['error']
19
+ end
20
+ end
21
+
22
+ def code
23
+ @code.to_i
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'repro/api/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'repro-api-client'
8
+ spec.version = Repro::Api::Client::VERSION
9
+ spec.authors = ['Takafumi Hirata']
10
+ spec.email = ['takhirata6@gmail.com']
11
+
12
+ spec.summary = 'Repro API client'
13
+ spec.description = 'Repro API client'
14
+ spec.homepage = 'https://github.com/totem3/repro-api-client'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'webmock'
28
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repro-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takafumi Hirata
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-02 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: '0'
20
+ type: :development
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: rake
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: rspec
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: webmock
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: Repro API client
70
+ email:
71
+ - takhirata6@gmail.com
72
+ executables:
73
+ - repro-api-client
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - exe/repro-api-client
87
+ - lib/repro/api/client.rb
88
+ - lib/repro/api/client/version.rb
89
+ - lib/repro/api/request_error.rb
90
+ - lib/repro/api/response.rb
91
+ - repro-api-client.gemspec
92
+ homepage: https://github.com/totem3/repro-api-client
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.8
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Repro API client
116
+ test_files: []