rebrandly_client 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10c23b1c684b63698c9f9b2525cc508c4ba98cff
4
+ data.tar.gz: 1a5bbb25ebcd4836661a2b9108b790424351eeed
5
+ SHA512:
6
+ metadata.gz: 8de7e1258bb87423e61244000ca17ab7853621f32adb9420b1528dbaa1c3af363371de628772607d81fb96623090411bb11292e0d663d67e37618d2c19ee14b9
7
+ data.tar.gz: 655cd6d90cd740b05dbf659d6319b68077378ad97907ddb70b6834f0afcc79b5395894fb5b6ab31a5ffb5104ab55a12487ac83b7e3a1f283740737a6c00e4beb
@@ -0,0 +1,27 @@
1
+ module Rebrandly
2
+ class << self
3
+ attr_accessor :configuration
4
+
5
+ def configuration
6
+ @configuration ||= Configuration.new
7
+ end
8
+
9
+ def api_key
10
+ configuration.api_key
11
+ end
12
+
13
+ def workspace
14
+ configuration.workspace
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+
21
+ private
22
+
23
+ class Configuration
24
+ attr_accessor :api_key, :workspace
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module Rebrandly
2
+ class Error < StandardError
3
+ def new(message)
4
+ super("Rebrandly >> #{message}")
5
+ end
6
+ end
7
+
8
+ class MissingApiKey < Error
9
+ def new
10
+ super('Missing API key')
11
+ end
12
+ end
13
+
14
+ class RateLimitExceeded < Error
15
+ def new(message)
16
+ super("RateLimit >> #{message}")
17
+ end
18
+ end
19
+
20
+ class UsageLimitExceeded < Error
21
+ def new(source)
22
+ super("Usage limit reach on #{source}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,113 @@
1
+ require 'httparty'
2
+ require 'rebrandly/configuration'
3
+ require 'rebrandly/error'
4
+
5
+ module Rebrandly
6
+ module Client
7
+ class << self
8
+ API_VERSION = 'v1'.freeze
9
+ BASE_URL = "https://api.rebrandly.com/#{API_VERSION}".freeze
10
+
11
+ def get(end_point, options = {})
12
+ wrap_request(end_point, options) do |url, http_options, formatted_options|
13
+ HTTParty.get(url, http_options.merge(query: formatted_options))
14
+ end
15
+ end
16
+
17
+ def post(end_point, options = {})
18
+ wrap_request(end_point, options) do |url, http_options, formatted_options|
19
+ HTTParty.post(url, http_options.merge(body: formatted_options.to_json))
20
+ end
21
+ end
22
+
23
+ def delete(end_point, options = {})
24
+ wrap_request(end_point, options) do |url, http_options, _formatted_options|
25
+ HTTParty.delete(url, http_options)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def lower_camelize(string)
32
+ s = string.split('_').collect(&:capitalize).join
33
+ s[0].downcase + s[1..-1]
34
+ end
35
+
36
+ def underscore(string)
37
+ string.gsub(/::/, '/').
38
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
39
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
40
+ tr('-', '_').
41
+ downcase
42
+ end
43
+
44
+ def lower_camelize_keys(object)
45
+ case object
46
+ when Hash
47
+ object.inject({}) do |hash, (key, value)|
48
+ hash[lower_camelize(key.to_s).to_sym] = lower_camelize_keys(value)
49
+ hash
50
+ end
51
+ when Array
52
+ object.map do |value|
53
+ lower_camelize_keys(value)
54
+ end
55
+ else
56
+ object
57
+ end
58
+ end
59
+
60
+ def underscore_keys(object)
61
+ case object
62
+ when Hash
63
+ object.inject({}) do |hash, (key, value)|
64
+ hash[underscore(key.to_s).to_sym] = underscore_keys(value)
65
+ hash
66
+ end
67
+ when Array
68
+ object.map do |value|
69
+ underscore_keys(value)
70
+ end
71
+ else
72
+ object
73
+ end
74
+ end
75
+
76
+ def handle_error(response)
77
+ parsed_response = response.parsed_response
78
+
79
+ case response.code
80
+ when 429
81
+ raise RateLimitExceeded
82
+ when 403
83
+ raise UsageLimitExceeded.new(parsed_response['source'])
84
+ else
85
+ raise Error.new(parsed_response['message'])
86
+ end
87
+ end
88
+
89
+ def wrap_request(end_point, options = {})
90
+ url = "#{BASE_URL}/#{end_point.sub(/^\//, '')}"
91
+ http_options = {headers: headers}
92
+ formatted_options = lower_camelize_keys(options)
93
+
94
+ response = yield(url, http_options, formatted_options)
95
+
96
+ return handle_error(response) unless response.code == 200
97
+
98
+ underscore_keys(JSON.parse(response.body))
99
+ end
100
+
101
+ def headers
102
+ raise MissingApiKey unless Rebrandly.api_key
103
+
104
+ h = Rebrandly.workspace ? {'workspace-type' => Rebrandly.workspace} : {}
105
+
106
+ h.merge({
107
+ 'Content-type' => 'application/json',
108
+ 'apikey' => Rebrandly.api_key
109
+ })
110
+ end
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rebrandly_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Dt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.16.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.16.2
27
+ description: A simple ruby client to connect to the Rebrandly API
28
+ email: Deuteu@users.noreply.github.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/rebrandly/configuration.rb
34
+ - lib/rebrandly/error.rb
35
+ - lib/rebrandly_client.rb
36
+ homepage: https://github.com/Deuteu/rebrandly_client
37
+ licenses:
38
+ - Apache License 2.0
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.4.5.1
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Rebrandly API client
60
+ test_files: []