gaurun-ruby 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c60c41677db7fb92b254cb614e04e5fc1b97581
4
+ data.tar.gz: d0e8e86c97b291ca33705b8ae89322eb2d2811e3
5
+ SHA512:
6
+ metadata.gz: 98f3a4363650024255e77bf963035761e0f0ead3f39bdd37a1048b8239e83466a58d067a82595f76645aa659b26e5180626dd5beec10393347a7768047b90e8a
7
+ data.tar.gz: 56486c9d20713155aae896f19cfbfdff9f4b8d41c4e3973a4875d8e4c95eaa44d2ba2077d0f888968776ed32a7e3f729e0a4f4a8041416bff603d67c5322b472
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+
12
+ .envrc
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'virtus', '~> 1.0', '>= 1.0.5'
6
+ gem 'typhoeus', '~> 1.3'
7
+
8
+ # Specify your gem's dependencies in gaurun-ruby.gemspec
9
+ gemspec
10
+
11
+ group :development, :test do
12
+ gem 'webmock', '~> 3.1'
13
+ gem 'sinatra', '~> 2.0'
14
+ end
@@ -0,0 +1,72 @@
1
+ [![Build Status](https://travis-ci.org/siukaido/gaurun-ruby.svg?branch=master)](https://travis-ci.org/siukaido/gaurun-ruby)
2
+
3
+ # Gaurun
4
+
5
+ ## .push
6
+
7
+ [POST /push](https://github.com/mercari/gaurun/blob/master/SPEC.md#post-push)
8
+
9
+ ```ruby
10
+ client = Gaurun::Client.new
11
+ notification = Gaurun::Notification.new(message: 'this is test message')
12
+
13
+ notification.ios.token = ['device_token1', 'device_token2']
14
+ notification.ios.badge = 10
15
+ notification.ios.extend = { hoge: 'piyo' }
16
+
17
+ notification.android.token = ['registration_id1', 'registration_id2']
18
+ notification.android.extend = { foo: 'bar' }
19
+
20
+ res = client.push(notification)
21
+
22
+ p res.body
23
+ ```
24
+
25
+ ## .parallel_push
26
+
27
+ ``` ruby
28
+ client = Gaurun::Client.new
29
+ notifications = 10.times.map do
30
+ notification = Gaurun::Notification.new(message: 'hoge')
31
+ ...
32
+ notification
33
+ end
34
+
35
+ res = client.parallel_push(notifications)
36
+
37
+ p res.map(&:body)
38
+
39
+ ```
40
+
41
+ ## .stat_go
42
+
43
+ [GET /stat/go](https://github.com/mercari/gaurun/blob/master/SPEC.md#get-statgo)
44
+
45
+ ```ruby
46
+ client = Gaurun::Client.new
47
+ res = client.stat_go
48
+
49
+ p res.body
50
+ ```
51
+
52
+ ## .stat_app
53
+
54
+ [GET /stat/app](https://github.com/mercari/gaurun/blob/master/SPEC.md#get-statapp)
55
+
56
+ ```ruby
57
+ client = Gaurun::Client.new
58
+ res = client.stat_app
59
+
60
+ p res.body
61
+ ```
62
+
63
+ ## .config_pushers
64
+
65
+ [PUT /config/pushers](https://github.com/mercari/gaurun/blob/master/SPEC.md#put-configpushers)
66
+
67
+ ``` ruby
68
+ client = Gaurun::Client.new
69
+ res = client.config_pushers(24)
70
+
71
+ p res.body
72
+ ```
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gaurun"
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__)
@@ -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,38 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "gaurun-ruby/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "gaurun-ruby"
9
+ spec.version = Gaurun::VERSION
10
+ spec.authors = ["siukaido"]
11
+ spec.email = ["siukaido@gmail.com"]
12
+
13
+ spec.summary = "gaurun API for ruby"
14
+ spec.description = "gaurun API for ruby"
15
+ spec.homepage = "https://github.com/siukaido"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = " Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "virtus", "~> 1.0", ">= 1.0.5"
34
+ spec.add_dependency "typhoeus", "~> 1.3"
35
+ spec.add_development_dependency "bundler", "~> 1.15"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ end
@@ -0,0 +1,8 @@
1
+ require "gaurun-ruby/version"
2
+ require "gaurun-ruby/notification"
3
+ require "gaurun-ruby/client"
4
+
5
+ module Gaurun
6
+ PLATFORM_IOS = 1
7
+ PLATFORM_ANDROID = 2
8
+ end
@@ -0,0 +1,79 @@
1
+ require "gaurun-ruby/response"
2
+
3
+ module Gaurun
4
+ class Client
5
+ def initialize(endpoint: 'http://localhost:1056')
6
+ @endpoint = endpoint_from_env || endpoint
7
+ end
8
+
9
+ def push(notification)
10
+ req = generate_push_request(notification)
11
+ do_request(req)
12
+ end
13
+
14
+ def parallel_push(notifications)
15
+ hydra = Typhoeus::Hydra.new
16
+ reqs = notifications.map do |notification|
17
+ req = generate_push_request(notification)
18
+ hydra.queue(req)
19
+ req
20
+ end
21
+ hydra.run
22
+
23
+ reqs.map do |req|
24
+ Gaurun::Response.new(req.response)
25
+ end
26
+ end
27
+
28
+ def config_pushers(max)
29
+ req = generate_request(
30
+ path: "/config/pushers?max=#{max}",
31
+ method: 'put'
32
+ )
33
+ do_request(req)
34
+ end
35
+
36
+ def stat_go
37
+ req = generate_request(path: '/stat/go', method: 'get')
38
+ do_request(req)
39
+ end
40
+
41
+ def stat_app
42
+ req = generate_request(path: '/stat/app', method: 'get')
43
+ do_request(req)
44
+ end
45
+
46
+ private
47
+
48
+ def endpoint_from_env
49
+ ENV['GAURUN_ENDPOINT'] || build_endpoint_from_env
50
+ end
51
+
52
+ def build_endpoint_from_env
53
+ return unless ENV['GAURUN_PROTOCOL'] && ENV['GAURUN_HOST']
54
+
55
+ endpoint = "#{ENV['GAURUN_PROTOCOL']}://#{ENV['GAURUN_HOST']}"
56
+ endpoint += ":#{ENV['GAURUN_PORT']}" if ENV['GAURUN_PORT']
57
+ endpoint
58
+ end
59
+
60
+ def generate_request(path:, method:, body: nil)
61
+ path = "/#{path}" unless path[0] == '/'
62
+
63
+ Typhoeus::Request.new(
64
+ "#{@endpoint}#{path}",
65
+ method: method.to_sym,
66
+ body: body
67
+ )
68
+ end
69
+
70
+ def generate_push_request(notification)
71
+ body = notification.payload.to_json
72
+ generate_request(path: '/push', method: 'post', body: body)
73
+ end
74
+
75
+ def do_request(req)
76
+ Gaurun::Response.new(req.run)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,80 @@
1
+ require 'virtus'
2
+
3
+ module Gaurun
4
+ class Notification
5
+ attr_accessor :ios, :android
6
+
7
+ def initialize(message: '')
8
+ @ios = Gaurun::Notification::IOS.new(message: message)
9
+ @android = Gaurun::Notification::Android.new(message: message)
10
+ end
11
+
12
+ def payload
13
+ notifications = [
14
+ @ios.payload,
15
+ @android.payload,
16
+ ].delete_if { |v| v.empty? }
17
+
18
+ return nil if notifications.empty?
19
+ {
20
+ notifications: notifications,
21
+ }
22
+ end
23
+
24
+ class PlatformAbstract
25
+ include Virtus.model
26
+
27
+ attribute :token, Array[String]
28
+ attribute :message, String
29
+ attribute :extend, Hash
30
+
31
+ def payload(platform)
32
+ return {} if token.nil? || token.empty?
33
+
34
+ attributes.merge(
35
+ token: token.uniq,
36
+ platform: platform,
37
+ extend: expand_extend
38
+ ).delete_if { |_, v| v.nil? }
39
+ end
40
+
41
+ private
42
+
43
+ def expand_extend
44
+ return nil if extend.nil?
45
+
46
+ extend.map do |k, v|
47
+ {
48
+ key: k.to_s,
49
+ val: v.to_s,
50
+ }
51
+ end
52
+ end
53
+ end
54
+
55
+ class IOS < PlatformAbstract
56
+ attribute :title, String
57
+ attribute :subtitle, String
58
+ attribute :badge, Integer
59
+ attribute :category, String
60
+ attribute :sound, String
61
+ attribute :expiry, Integer
62
+ attribute :content_available, Boolean
63
+ attribute :mutable_content, Boolean
64
+
65
+ def payload
66
+ super(Gaurun::PLATFORM_IOS)
67
+ end
68
+ end
69
+
70
+ class Android < PlatformAbstract
71
+ attribute :collapse_key, String
72
+ attribute :delay_while_idle, Boolean
73
+ attribute :time_to_live, Integer
74
+
75
+ def payload
76
+ super(Gaurun::PLATFORM_ANDROID)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,22 @@
1
+ require 'json'
2
+
3
+ module Gaurun
4
+ class Response
5
+ def initialize(res)
6
+ @res = res
7
+ end
8
+
9
+ def status_code
10
+ @res.response_code.to_i
11
+ end
12
+
13
+ def status_message
14
+ @res.return_code
15
+ end
16
+
17
+ def body
18
+ return nil unless @res.response_body.length > 1
19
+ JSON.parse(@res.response_body, { symbolize_names: true })
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Gaurun
2
+ VERSION = '0.2.4'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gaurun-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.4
5
+ platform: ruby
6
+ authors:
7
+ - siukaido
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.5
33
+ - !ruby/object:Gem::Dependency
34
+ name: typhoeus
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.15'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.15'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ description: gaurun API for ruby
90
+ email:
91
+ - siukaido@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - ".travis.yml"
99
+ - Gemfile
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - gaurun-ruby.gemspec
105
+ - lib/gaurun-ruby.rb
106
+ - lib/gaurun-ruby/client.rb
107
+ - lib/gaurun-ruby/notification.rb
108
+ - lib/gaurun-ruby/response.rb
109
+ - lib/gaurun-ruby/version.rb
110
+ homepage: https://github.com/siukaido
111
+ licenses: []
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.13
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: gaurun API for ruby
133
+ test_files: []