twinpush 0.0.4 → 0.0.10

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/measure.rb +90 -0
  3. data/lib/twinpush.rb +28 -4
  4. metadata +10 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3398de008e6bad0ae12c74b5e40918e0f384b441d2aa412c948f068110c88678
4
- data.tar.gz: 6541e84151d4c6c5c10856e514cac5717b663d62602212cf56c67c5b1b90bf2f
3
+ metadata.gz: 7af25d5705a6968e4b1ba122f8325ee9df5154d5db42c2ff1eb01393a491468b
4
+ data.tar.gz: 698e57e6265a70c0bdce905a63948cf8d7ec37b3e911cee9d3f1309a6b814853
5
5
  SHA512:
6
- metadata.gz: e53eae26dac4f3034bfe1dce8c35b8753060b5563959bb86ea0136f75b446e47e28c3b0e73d283851054f3cb45761a7e0db362028a974fbc2f7791a7874b544d
7
- data.tar.gz: e9c7eb38c36e7411f86bbcb8f1f8bd1e89d9472b5e82558290538effe0a2f6fbf7c88c8affcffd7cbda4a1020999b4b3e1fa196a8b5f188c42d4b64dadab7782
6
+ metadata.gz: 8c43b09576841ace12cb9ab9a658b3eadcfd19c4b7ba08f6079e0fb2df87b99a793cb7e8605c24b1b4b1a356a2c8c4388a80849956ac5ad93b0545510c4675e4
7
+ data.tar.gz: 85dff4fee862a75e9a7ef3c3b216028b0c95399d45802f4a0d6af22469dc4b286acde1038a66ffeb27172f3997a577050c38747da6ebe36496fc240668ce1dbf
data/lib/measure.rb ADDED
@@ -0,0 +1,90 @@
1
+ #!/bin/env ruby
2
+
3
+ # lazy hack from Robert Klemme
4
+
5
+ module Memory
6
+ # sizes are guessed, I was too lazy to look
7
+ # them up and then they are also platform
8
+ # dependent
9
+ REF_SIZE = 4 # ?
10
+ OBJ_OVERHEAD = 4 # ?
11
+ FIXNUM_SIZE = 4 # ?
12
+
13
+ # informational output from analysis
14
+ MemoryInfo = Struct.new :roots, :objects, :bytes, :loops
15
+
16
+ def self.analyze(*roots)
17
+ an = Analyzer.new
18
+ an.roots = roots
19
+ an.analyze
20
+ end
21
+
22
+ class Analyzer
23
+ attr_accessor :roots
24
+ attr_reader :result
25
+
26
+ def analyze
27
+ @result = MemoryInfo.new roots, 0, 0, 0
28
+ @objs = {}
29
+
30
+ queue = roots.dup
31
+
32
+ until queue.empty?
33
+ obj = queue.shift
34
+
35
+ case obj
36
+ # special treatment for some types
37
+ # some are certainly missing from this
38
+ when IO
39
+ visit(obj)
40
+ when String
41
+ visit(obj) { @result.bytes += obj.size }
42
+ when Fixnum
43
+ @result.bytes += FIXNUM_SIZE
44
+ when Array
45
+ visit(obj) do
46
+ @result.bytes += obj.size * REF_SIZE
47
+ queue.concat(obj)
48
+ end
49
+ when Hash
50
+ visit(obj) do
51
+ @result.bytes += obj.size * REF_SIZE * 2
52
+ obj.each {|k,v| queue.push(k).push(v)}
53
+ end
54
+ when Enumerable
55
+ visit(obj) do
56
+ obj.each do |o|
57
+ @result.bytes += REF_SIZE
58
+ queue.push(o)
59
+ end
60
+ end
61
+ else
62
+ visit(obj) do
63
+ obj.instance_variables.each do |var|
64
+ @result.bytes += REF_SIZE
65
+ queue.push(obj.instance_variable_get(var))
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ @result
72
+ end
73
+
74
+ private
75
+ def visit(obj)
76
+ id = obj.object_id
77
+
78
+ if @objs.has_key? id
79
+ @result.loops += 1
80
+ false
81
+ else
82
+ @objs[id] = true
83
+ @result.bytes += OBJ_OVERHEAD
84
+ @result.objects += 1
85
+ yield obj if block_given?
86
+ true
87
+ end
88
+ end
89
+ end
90
+ end
data/lib/twinpush.rb CHANGED
@@ -1,13 +1,18 @@
1
1
  require 'faraday'
2
2
  require 'cgi'
3
3
  require 'json'
4
+ require 'measure'
4
5
 
5
6
  class TWINPUSH
7
+ include Memory
8
+
6
9
  DEFAULT_TIMEOUT = 30
7
10
  FORMAT = :json
8
11
  BASE_URI = 'https://subdomain.twinpush.com'
9
12
  API_URL = '/api/v2/apps'
10
13
 
14
+ class PayloadSizeError < StandardError; end
15
+
11
16
  attr_accessor :uri, :app_id, :api_token, :api_token_creator
12
17
 
13
18
  # @param [Hash] authentication_keys
@@ -35,12 +40,22 @@ class TWINPUSH
35
40
 
36
41
  alias show show_notification
37
42
 
43
+ #obtains notification details in OpenStruct
44
+ def show_notification_object(notification_id, device_id = nil)
45
+ OpenStruct.new JSON.parse(show_notification(notification_id, device_id)[:body])
46
+ end
47
+
38
48
  #creates a new notification to be delivered from the platform
39
49
  def create_notification(notification_params)
40
- path = "#{API_URL}/#{app_id}/notifications"
41
- for_uri(uri) do |connection|
42
- response = connection.post(path, notification_params.to_json)
43
- build_response(response)
50
+ memory_size = Memory.analyze(notification_params)
51
+ if memory_size.bytes < 2048 # 2KB limit
52
+ path = "#{API_URL}/#{app_id}/notifications"
53
+ for_uri(uri) do |connection|
54
+ response = connection.post(path, notification_params.to_json)
55
+ build_response(response)
56
+ end
57
+ else
58
+ raise PayloadSizeError.new "The notification payload exceeds the 2KB limit"
44
59
  end
45
60
  end
46
61
 
@@ -115,6 +130,15 @@ class TWINPUSH
115
130
  end
116
131
  end
117
132
 
133
+ # Clears all the custom properties for a given device
134
+ def clear_properties(device_id)
135
+ path = "#{API_URL}/#{app_id}/devices/#{device_id}/clear_custom_properties"
136
+ for_uri(uri) do |connection|
137
+ response = connection.delete(path)
138
+ build_response(response)
139
+ end
140
+ end
141
+
118
142
  private
119
143
 
120
144
  def validate_keys(authentication_keys)
metadata CHANGED
@@ -1,19 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twinpush
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amaury González
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-19 00:00:00.000000000 Z
11
+ date: 2021-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.12'
17
20
  - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 0.12.2
@@ -21,6 +24,9 @@ dependencies:
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.12'
24
30
  - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 0.12.2
@@ -33,6 +39,7 @@ executables: []
33
39
  extensions: []
34
40
  extra_rdoc_files: []
35
41
  files:
42
+ - lib/measure.rb
36
43
  - lib/twinpush.rb
37
44
  - spec/spec_helper.rb
38
45
  - spec/twinpush_spec.rb
@@ -55,8 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
62
  - !ruby/object:Gem::Version
56
63
  version: '0'
57
64
  requirements: []
58
- rubyforge_project: twinpush
59
- rubygems_version: 2.7.6
65
+ rubygems_version: 3.0.8
60
66
  signing_key:
61
67
  specification_version: 4
62
68
  summary: Reliably deliver push notifications through TwinPush API