ruby_branch 0.1.0 → 0.2.0

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: 1a0edc2530f33ea70d9b6acfd46503d17232df8b
4
- data.tar.gz: 6d740197b830aefa47f231c255d0ece0067be0f0
3
+ metadata.gz: b84f50702fdfb01cb7a33b4ebb33ce549dac9ba9
4
+ data.tar.gz: e7774df26bfc7714928729b4edf44f603947d7da
5
5
  SHA512:
6
- metadata.gz: 6d9705fd4e50cf6eadb031601599870bcb0230b7d99197ad11fe48fe015bf01b6b41b035720a53d3a6853ab03452b76fcb46166341f0b67c6d76547a0acbfcfb
7
- data.tar.gz: 75de6a1698b15625dc5b43024f7a81b5e6181438f4a5eaa951d1668a839fa5839ced9109a399728fcabb8e82476c876c1bde2300fb00012e23e96a50f6998d14
6
+ metadata.gz: b11727d831a98553dfd119634ab49c787736c4047d553fbc20c8505501f8e47e17b6e536a8f833f0e228cc304f0d655429cbb0e1f46009d06d647fb9d9cb54fe
7
+ data.tar.gz: b197e4ce4b2ef3815cffbcc3e1570dc60a9d7aadcf80afe1c8eb83bb8d49d55d0bbd2958426822590096b8fb6317baa61db605dcb530ea2edc7fdf031990482f
data/README.md CHANGED
@@ -26,6 +26,7 @@ Configuration:
26
26
 
27
27
  RubyBranch.configure do |config|
28
28
  config.api_key = 'YOUR_API_KEY'
29
+ config.secret_key = 'YOUR_SECRET_KEY'
29
30
  config.branch_domain = 'YOUR_BRANCH_DOMAIN'
30
31
  # for fallback in case link creation has been failed
31
32
  config.link_to_homepage = 'https://yourdomain.com' # optional
@@ -43,6 +44,12 @@ Methods:
43
44
  # Create link through branch api
44
45
  link.create(analytics: {}, data: {}, settings: {})
45
46
 
47
+ # Update existing link without api forcing
48
+ link.update_safely(url: url, analytics: {}, data: {}, settings: {})
49
+
50
+ # Update existing link through branch api
51
+ link.update(url: url, analytics: {}, data: {}, settings: {})
52
+
46
53
  ## Development
47
54
 
48
55
  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.
@@ -5,6 +5,7 @@ require 'ruby_branch/errors/link_length_exceed_error'
5
5
  require 'ruby_branch/errors/api_response_error'
6
6
  require 'addressable'
7
7
  require 'faraday'
8
+ require 'json'
8
9
  require 'ruby_branch/api/response'
9
10
  require 'ruby_branch/api/request'
10
11
  require 'ruby_branch/api/resources/link'
@@ -11,6 +11,15 @@ module RubyBranch
11
11
  Response.new(response)
12
12
  end
13
13
 
14
+ def put(resource, body = '')
15
+ response = connection.put do |request|
16
+ request.url resource
17
+ request.headers['Content-Type'] = 'application/json'
18
+ request.body = body unless body.empty?
19
+ end
20
+ Response.new(response)
21
+ end
22
+
14
23
  def connection
15
24
  @connection ||=
16
25
  Faraday.new(url: BRANCH_API_ENDPOINT) do |connection|
@@ -11,20 +11,33 @@ module RubyBranch
11
11
  create(analytics: analytics, data: data, settings: settings)
12
12
  end
13
13
 
14
+ def update_safely(url:, analytics: {}, data: {}, settings: {})
15
+ build(analytics: analytics, data: data, settings: settings)
16
+ rescue Errors::LinkLengthExceedError
17
+ update(url: url, analytics: analytics, data: data, settings: settings)
18
+ end
19
+
14
20
  def create(analytics: {}, data: {}, settings: {})
15
21
  request_body = build_request_body(analytics, settings, data)
16
22
  response = do_create_request(request_body)
17
23
 
18
24
  return response.json[:url] if response.success?
19
25
 
20
- error_attrs = { status: response.status, body: response.body }
21
- if defined?(Bugsnag) && defined?(Rails)
22
- Bugsnag.notify(Errors::ApiResponseError.new, error_attrs) if Rails.env.production?
23
- end
26
+ process_error(response)
24
27
 
25
28
  RubyBranch.config.link_to_homepage if RubyBranch.config.link_to_homepage
26
29
  end
27
30
 
31
+ def update(url:, analytics: {}, data: {}, settings: {})
32
+ request_body = build_request_body(analytics, settings, data)
33
+ response = do_update_request(url, request_body)
34
+
35
+ return true if response.success?
36
+
37
+ process_error(response)
38
+ false
39
+ end
40
+
28
41
  def build(analytics: {}, data: {}, settings: {})
29
42
  params = {}
30
43
  params.merge!(prepare_analytics(analytics))
@@ -48,8 +61,13 @@ module RubyBranch
48
61
  request.post('v1/url', request_body.to_json)
49
62
  end
50
63
 
64
+ def do_update_request(url, request_body)
65
+ request = Request.new
66
+ request.put("v1/url?url=#{url}", request_body.to_json)
67
+ end
68
+
51
69
  def build_request_body(analytics, settings, data)
52
- body = { branch_key: RubyBranch.config.api_key }
70
+ body = { branch_key: RubyBranch.config.api_key, branch_secret: RubyBranch.config.secret_key }
53
71
  body.merge! prepare_analytics(analytics)
54
72
  body.merge! prepare_settings(settings)
55
73
  body.merge! prepare_data(data)
@@ -73,6 +91,13 @@ module RubyBranch
73
91
  raise Errors::LinkLengthExceedError if link.size > LINK_LENGTH_LIMIT
74
92
  end
75
93
 
94
+ def process_error(response)
95
+ error_attrs = { status: response.status, body: response.body }
96
+ if defined?(Bugsnag) && defined?(Rails) && Rails.env.production?
97
+ Bugsnag.notify(Errors::ApiResponseError.new, error_attrs)
98
+ end
99
+ end
100
+
76
101
  end
77
102
  end
78
103
  end
@@ -1,7 +1,7 @@
1
1
  module RubyBranch
2
2
  class Config
3
3
 
4
- attr_accessor :api_key, :branch_domain, :link_to_homepage
4
+ attr_accessor :api_key, :secret_key, :branch_domain, :link_to_homepage
5
5
 
6
6
  end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module RubyBranch
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -13,6 +13,15 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
16
25
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
26
  f.match(%r{^(test|spec|features)/})
18
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_branch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Morskov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-16 00:00:00.000000000 Z
11
+ date: 2018-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -153,7 +153,8 @@ files:
153
153
  homepage: ''
154
154
  licenses:
155
155
  - MIT
156
- metadata: {}
156
+ metadata:
157
+ allowed_push_host: https://rubygems.org
157
158
  post_install_message:
158
159
  rdoc_options: []
159
160
  require_paths:
@@ -170,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
171
  version: '0'
171
172
  requirements: []
172
173
  rubyforge_project:
173
- rubygems_version: 2.5.1
174
+ rubygems_version: 2.6.13
174
175
  signing_key:
175
176
  specification_version: 4
176
177
  summary: Ruby client for https://branch.io API