firebase_dynamic_link 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51b5d7e71fe3c9c0ab7b5bfd80a6bfb23e5dbe61
4
- data.tar.gz: 1857195a47111dcd5e1add4b181ab8cb860be9a4
3
+ metadata.gz: 68097ad4857f4630938203f52a41e070468ca86b
4
+ data.tar.gz: 10e4a64a80f75e0f14e8ea567ccbb08615e41977
5
5
  SHA512:
6
- metadata.gz: f19b37fc7a8272344e7314e7855a73cdeb318d899f974fe78207c9f89402698488f9d5f55c5a78c2c072dd659a10b62bb2e81e56b53fae888d24cd5fe35bbab0
7
- data.tar.gz: 14a2bae7132cac8fc2d20131fcd5be77a633c67b19a0bfd0c6ac2e6e88b6bfd32cce440727ae58a6065d40fae2f3b84f2d8c4c08a8967d4f911908f21e8a57fc
6
+ metadata.gz: 76edc91a6a5c37bbc4e5a6822523cc38c76a57fedc0ca29d0bf51900ba2e392aee4984516d89f5f88071ccb9a9d164be8a6fefbd97bf00a420d8e520e01f49aa
7
+ data.tar.gz: 1e944261c7925b97325f7ab728007f5b74d208d7ddde5c6ad7c30fa8a82af62ebf2d51189c8a925a15eaf4cadcde1b447388c5727ae9bc4a31c0d9175d93d62d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- firebase_dynamic_link (0.1.1)
4
+ firebase_dynamic_link (0.1.2)
5
5
  dry-configurable (~> 0.5, >= 0.5.0)
6
6
  faraday (~> 0.9, >= 0.9.2)
7
7
 
data/README.md CHANGED
@@ -40,12 +40,17 @@ FirebaseDynamicLink.configure do |config|
40
40
  config.default.suffix.option = 'SHORTEN' or 'UNGUESSABLE'
41
41
 
42
42
  config.default.dynamic_link_domain = 'http://xyz.app.goo.gl'
43
+
44
+ config.timeout = 3 # timeout and open timeout for global request
45
+ config.open_timeout = 3
43
46
  end
44
47
 
45
48
  client = FirebaseDynamicLink::Client.new
46
49
  options = {
47
- suffix_option: '' # to override default suffix default config
48
- dynamic_link_domain: '' # to override default dynamic_link_domain default config
50
+ suffix_option: '', # to override default suffix default config
51
+ dynamic_link_domain: '', # to override default dynamic_link_domain default config
52
+ timeout: 10, # timeout and open timeout for each request
53
+ open_timeout: 10
49
54
  }
50
55
  result = client.shorten_link(link, options)
51
56
  # options argument is optional
@@ -56,7 +61,6 @@ if request successful, then the result should be like following hash object
56
61
 
57
62
  ```ruby
58
63
  {
59
- :success=>true,
60
64
  :link=>"https://--.app.goo.gl/ukph",
61
65
  :preview_link=>"https://--.app.goo.gl/ukph?d=1",
62
66
  :warning=>[
@@ -69,25 +73,11 @@ if request successful, then the result should be like following hash object
69
73
  {
70
74
  "warningCode"=>"..."
71
75
  }
72
- ],
73
- :error_message=>nil,
74
- :error_status=>nil,
75
- :error_code=>nil
76
+ ]
76
77
  }
77
78
  ```
78
79
 
79
- otherwise
80
-
81
- ```ruby
82
- { :success=>false,
83
- :link=>nil,
84
- :preview_link=>nil,
85
- :warning=>nil,
86
- :error_message=>"Long link is not parsable: ...",
87
- :error_status=>"INVALID_ARGUMENT",
88
- :error_code=>400
89
- }
90
- ```
80
+ otherwise it raises FirebaseDynamicLink::ConnectionError exception, with message = http error message
91
81
 
92
82
  # NOTE
93
83
 
@@ -1,7 +1,9 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- firebase_dynamic_link (0.1.0)
4
+ firebase_dynamic_link (0.1.2)
5
+ dry-configurable (~> 0.5, >= 0.5.0)
6
+ faraday (~> 0.9, >= 0.9.2)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
@@ -44,7 +46,7 @@ PLATFORMS
44
46
  ruby
45
47
 
46
48
  DEPENDENCIES
47
- appraisal
49
+ appraisal (~> 2.2.0)
48
50
  bundler (~> 1.16)
49
51
  dotenv (~> 2.2, >= 2.2.2)
50
52
  dry-configurable (= 0.5.0)
@@ -1,7 +1,9 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- firebase_dynamic_link (0.1.0)
4
+ firebase_dynamic_link (0.1.2)
5
+ dry-configurable (~> 0.5, >= 0.5.0)
6
+ faraday (~> 0.9, >= 0.9.2)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
@@ -44,7 +46,7 @@ PLATFORMS
44
46
  ruby
45
47
 
46
48
  DEPENDENCIES
47
- appraisal
49
+ appraisal (~> 2.2.0)
48
50
  bundler (~> 1.16)
49
51
  dotenv (~> 2.2, >= 2.2.2)
50
52
  dry-configurable (= 0.6.0)
@@ -1,7 +1,9 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- firebase_dynamic_link (0.1.0)
4
+ firebase_dynamic_link (0.1.2)
5
+ dry-configurable (~> 0.5, >= 0.5.0)
6
+ faraday (~> 0.9, >= 0.9.2)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
@@ -44,7 +46,7 @@ PLATFORMS
44
46
  ruby
45
47
 
46
48
  DEPENDENCIES
47
- appraisal
49
+ appraisal (~> 2.2.0)
48
50
  bundler (~> 1.16)
49
51
  dotenv (~> 2.2, >= 2.2.2)
50
52
  dry-configurable (= 0.6.0)
@@ -1,7 +1,9 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- firebase_dynamic_link (0.1.0)
4
+ firebase_dynamic_link (0.1.2)
5
+ dry-configurable (~> 0.5, >= 0.5.0)
6
+ faraday (~> 0.9, >= 0.9.2)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
@@ -44,7 +46,7 @@ PLATFORMS
44
46
  ruby
45
47
 
46
48
  DEPENDENCIES
47
- appraisal
49
+ appraisal (~> 2.2.0)
48
50
  bundler (~> 1.16)
49
51
  dotenv (~> 2.2, >= 2.2.2)
50
52
  dry-configurable (= 0.7.0)
@@ -7,6 +7,7 @@ module FirebaseDynamicLink
7
7
  extend Dry::Configurable
8
8
 
9
9
  class InvalidConfig < StandardError; end
10
+ class ConnectionError < StandardError; end
10
11
 
11
12
  # You can change it to
12
13
  # FirebaseDynamicLink.adapter = :patron
@@ -19,6 +20,12 @@ module FirebaseDynamicLink
19
20
 
20
21
  setting :api_key
21
22
 
23
+ # Timeout default setting is 3 seconds
24
+ setting :timeout, 3
25
+
26
+ # Open timeout default setting is 3 seconds
27
+ setting :open_timeout, 3
28
+
22
29
  # This domain will be used if dynamic_link_domain setting is nil
23
30
  # it raises error if both of settings are nil
24
31
  setting :default do
@@ -8,28 +8,24 @@ module FirebaseDynamicLink
8
8
  # status >= 400
9
9
  # body {"error"=>{"code"=>400, "message"=>"Long link is not parsable: https://k4mu4.app.goo.gl?link=abcde [https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_parameters]", "status"=>"INVALID_ARGUMENT"}}
10
10
  def shorten_link(link, options = {})
11
+ build_connection_options(connection, options)
12
+
11
13
  suffix_option = options.delete(:suffix_option)
12
14
  suffix_option ||= config.default.suffix.option
13
- dynamic_link_domain = options.delete(:dynamic_link_domain)
14
- dynamic_link_domain ||= config.default.dynamic_link_domain || raise(FirebaseDynamicLink::InvalidConfig, 'Dynamic link domain is empty')
15
- link = "#{dynamic_link_domain}?link=#{link}"
15
+
16
16
  response = connection.post(nil, {
17
- longDynamicLink: link,
17
+ longDynamicLink: build_link(link, options),
18
18
  suffix: {
19
19
  option: suffix_option
20
20
  }
21
21
  }.to_json)
22
- body = JSON.parse(response.body)
23
- has_error = body.key?('error')
24
- {
25
- success: !has_error,
26
- link: has_error ? nil : body['shortLink'],
27
- preview_link: has_error ? nil : body['previewLink'],
28
- warning: has_error ? nil : body['warning'],
29
- error_message: has_error ? body['error']['message'] : nil,
30
- error_status: has_error ? body['error']['status'] : nil,
31
- error_code: has_error ? body['error']['code'] : nil
32
- }
22
+ if response.status.between?(200, 299)
23
+ render_success(response)
24
+ else
25
+ raise_error(response)
26
+ end
27
+ rescue Faraday::ConnectionFailed => e
28
+ raise FirebaseDynamicLink::ConnectionError, e.message
33
29
  end
34
30
 
35
31
  def shorten_object(_json_object)
@@ -38,10 +34,21 @@ module FirebaseDynamicLink
38
34
 
39
35
  private
40
36
 
37
+ def build_link(link, options)
38
+ dynamic_link_domain = options.delete(:dynamic_link_domain)
39
+ dynamic_link_domain ||= config.default.dynamic_link_domain || raise(FirebaseDynamicLink::InvalidConfig, 'Dynamic link domain is empty')
40
+ "#{dynamic_link_domain}?link=#{link}"
41
+ end
42
+
43
+ def build_connection_options(c, options)
44
+ c.options.timeout = 1 if options.key?(:timeout)
45
+ c.options.open_timeout = 1 if options.key?(:open_timeout)
46
+ c
47
+ end
48
+
41
49
  def connection
42
- con = Faraday::Connection.new(url: end_point,
43
- headers: { 'Content-Type' => 'application/json' })
44
- con
50
+ @connection ||= Faraday::Connection.new(url: end_point,
51
+ headers: { 'Content-Type' => 'application/json' })
45
52
  end
46
53
 
47
54
  def config
@@ -51,5 +58,28 @@ module FirebaseDynamicLink
51
58
  def end_point
52
59
  "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=#{config.api_key}"
53
60
  end
61
+
62
+ def raise_error(response)
63
+ message = response.reason_phrase if response.respond_to?(:reason_phrase)
64
+ if message.nil?
65
+ message = begin
66
+ body = JSON.parse(response.body)
67
+ body['error']['message']
68
+ rescue
69
+ response.body
70
+ end
71
+ end
72
+ raise FirebaseDynamicLink::ConnectionError, message
73
+ end
74
+
75
+ def render_success(response)
76
+ body = JSON.parse(response.body)
77
+ has_error = body.key?('error')
78
+ {
79
+ link: has_error ? nil : body['shortLink'],
80
+ preview_link: has_error ? nil : body['previewLink'],
81
+ warning: has_error ? nil : body['warning']
82
+ }
83
+ end
54
84
  end
55
85
  end
@@ -1,3 +1,3 @@
1
1
  module FirebaseDynamicLink
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firebase_dynamic_link
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - M Saiqul Haq