relinkly 0.1.0

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
+ SHA256:
3
+ metadata.gz: efe951a2ac0f57738160775af7e0ae5d7233527c057eb65fe6b93a96d764fe0b
4
+ data.tar.gz: 8676c1129862ca4e14cf50a19ce3020b586298778c058fe7dfe95c6c6f5ffb57
5
+ SHA512:
6
+ metadata.gz: 13322191a160fcc770f2d827b01ef63621676e018216204ebba6dbdc3c8ecb2b8925a9d9f15cda10bc8c8605d24bf617def43ac25e215c28319a348344da5df1
7
+ data.tar.gz: 798c9c83492adbf68ee0c33c8e6e50e8805df4ec7c11e5d0e428b15e84a07586ba60746e2f5f47386bb7ae06a95831b02ad388f6ecda2f9e900310e66357fbac
@@ -0,0 +1,11 @@
1
+
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.6
5
+ before_install: gem install bundler -v 2.2.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in relinkly.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Andrew Davis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # relinkly
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'relinkly'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install relinkly
18
+
19
+ ## Usage
20
+
21
+ If using Rails, put the following into your application.rb. If just Ruby,
22
+ run this before trying to use the API.
23
+
24
+ ```ruby
25
+ Relinkly.configure do |config|
26
+ config.api_key = 'YOUR_KEY_HERE'
27
+ end
28
+ ```
29
+
30
+ ### Instantiate an API object.
31
+
32
+ ```ruby
33
+ api = Relinkly::API.new
34
+ ```
35
+
36
+ ### API Requests
37
+
38
+ ```ruby
39
+ api.links # GET /v1/links
40
+ api.links(id) # GET /v1/links/:id
41
+ api.link_count(options) # GET /v1/links/count
42
+ api.new_link(options) # GET /v1/links/new
43
+ api.shorten(destination, options) # POST /v1/links
44
+ api.update_link(id, options) # POST /v1/links/:id
45
+ api.delete(id, options) # DELETE /v1/links/:id
46
+ api.domains # GET /v1/domains
47
+ api.domain(id) # GET /v1/domains/:id
48
+ api.domain_count(options) # GET /v1/domains/count
49
+ api.account # GET /v1/account
50
+ ```
51
+
52
+ ### Make a new short link
53
+
54
+ ```ruby
55
+ my_domain = api.domains.first
56
+ link = api.shorten('https://google.com', domain: my_domain.to_h, title: 'Google', description: 'Google Homepage', favourite: true)
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cdrrazan/relinkly.
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'relinkly'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
@@ -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,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'relinkly/version'
4
+ require 'relinkly/configuration'
5
+ require 'relinkly/api'
6
+ require 'relinkly/element'
7
+ require 'relinkly/domain'
8
+ require 'relinkly/creator'
9
+ require 'relinkly/integration'
10
+ require 'relinkly/link'
11
+
12
+ module Relinkly
13
+ class << self
14
+ attr_accessor :configuration
15
+ end
16
+
17
+ def self.api_key
18
+ configuration.api_key
19
+ end
20
+
21
+ def self.configuration
22
+ @configuration ||= Configuration.new
23
+ end
24
+
25
+ def self.configure
26
+ yield(configuration)
27
+ end
28
+ end
29
+
30
+ class String
31
+ def relinkly_underscore
32
+ gsub(/::/, '/')
33
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
34
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
35
+ .tr('-', '_')
36
+ .downcase
37
+ end
38
+
39
+ def relinkly_camelize
40
+ split('_').collect(&:capitalize).join
41
+ end
42
+
43
+ def relinkly_lower_camelize
44
+ res = relinkly_camelize
45
+ res[0].downcase + res[1..-1]
46
+ end
47
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+
5
+ module Relinkly
6
+ class RelinklyError < StandardError; end
7
+
8
+ class RateLimitExceeded < RelinklyError; end
9
+
10
+ class API
11
+ API_VERSION = 'v1'
12
+ BASE_URL = "https://api.rebrandly.com/#{API_VERSION}"
13
+
14
+ # GET /v1/links
15
+ def links(options = {})
16
+ all_links = relinkly_request(:get, 'links', options)
17
+ all_links.map { |link| Link.new(link) }
18
+ end
19
+
20
+ # GET /v1/links/:id
21
+ def link(id)
22
+ Link.new(relinkly_request(:get, "links/#{id}"))
23
+ end
24
+
25
+ # GET /v1/links/count
26
+ def link_count(_options = {})
27
+ relinkly_request(:get, 'links/count')['count']
28
+ end
29
+
30
+ # POST /v1/links
31
+ def shorten(destination, options = {})
32
+ options[:destination] = destination
33
+ Link.new(relinkly_request(:post, 'links', options))
34
+ end
35
+
36
+ # POST /v1/links/:id
37
+ def update_link(id, options = {})
38
+ Link.new(relinkly_request(:post, "links/#{id}", options))
39
+ end
40
+
41
+ # DELETE /v1/links/:id
42
+ def delete(id, options = {})
43
+ Link.new(relinkly_request(:delete, "links/#{id}", options))
44
+ end
45
+
46
+ # GET /v1/domains
47
+ def domains(options = {})
48
+ all_domains = relinkly_request(:get, 'domains', options)
49
+ all_domains.map { |domain| Domain.new(domain) }
50
+ end
51
+
52
+ # GET /v1/domains/:id
53
+ def domain(id)
54
+ Domain.new(relinkly_request(:get, "domains/#{id}"))
55
+ end
56
+
57
+ # GET /v1/domains/count
58
+ def domain_count(_options = {})
59
+ relinkly_request(:get, 'domains/count')['count']
60
+ end
61
+
62
+ # GET /v1/account
63
+ def account
64
+ Creator.new(relinkly_request(:get, 'account'))
65
+ end
66
+
67
+ private
68
+
69
+ def relinkly_request(method, url, options = {})
70
+ url = "#{BASE_URL}/#{url}"
71
+ # Convert all hash keys into camel case for Relinkly
72
+ options = Hash[options.map { |k, v| [k.to_s.relinkly_lower_camelize.to_sym, v] }]
73
+
74
+ http_attrs = { headers: headers }
75
+ case method
76
+ when :get
77
+ http_attrs.merge!(query: options)
78
+ when :post
79
+ http_attrs.merge!(body: options.to_json)
80
+ end
81
+
82
+ res = HTTParty.send(method, url, http_attrs)
83
+ if res.code == 200
84
+ JSON.parse(res.body)
85
+ else
86
+ relinkly_error = res.parsed_response
87
+ if relinkly_error['domain'] == 'usageLimits' && relinkly_error['reason'] == 'rateLimitExceeded'
88
+ raise RateLimitExceeded
89
+ else
90
+ raise RelinklyError, relinkly_error['message']
91
+ end
92
+ end
93
+ end
94
+
95
+ def headers
96
+ {
97
+ 'Content-type' => 'application/json',
98
+ 'apikey' => Relinkly.api_key
99
+ }
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relinkly
4
+ class Configuration
5
+ attr_accessor :api_key
6
+
7
+ def initialize
8
+ @api_key = nil
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relinkly
4
+ class Creator < Element
5
+ attr_accessor :id, :full_name, :avatar_url, :username, :email, :created_at
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relinkly
4
+ class Domain < Element
5
+ attr_accessor :id, :ref, :full_name, :top_level_domain, :level,
6
+ :created_at, :updated_at, :custom_homepage, :owner_id, :type,
7
+ :subdomains, :managed, :status, :https, :active
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relinkly
4
+ # General class to map Relinkly objects to Ruby objects
5
+ class Element
6
+ def initialize(attrs = {})
7
+ # Only set the attributes if the method exists, this way we can ignore deprecated attributes
8
+ attrs.each { |k, v| send("#{k.relinkly_underscore}=", v) if respond_to?("#{k.relinkly_underscore}=") }
9
+ end
10
+
11
+ def to_h
12
+ Hash[
13
+ *instance_variables.map do |variable|
14
+ name = variable[1..-1].to_sym # Drop the @ sign at the front
15
+ [name.to_sym, instance_variable_get(variable)]
16
+ end.flatten
17
+ ]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relinkly
4
+ class Integration < Element
5
+ attr_accessor :link, :name, :link_id
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relinkly
4
+ class Link < Element
5
+ attr_accessor :id, :link_id, :title, :slashtag, :destination, :created_at,
6
+ :updated_at, :status, :tags, :scripts, :forward_parameters,
7
+ :clicks, :last_click_date, :last_click_at, :is_public,
8
+ :short_url, :domain_id, :domain_name,
9
+ :https, :favourite
10
+
11
+ # Associations
12
+ %i[domain creator integration].each do |association|
13
+ # Creates the getter methods, such as "@instance.domain"
14
+ attr_reader association
15
+
16
+ # Creates the setter methods, such as "@instance.domain = ..."
17
+ define_method("#{association}=") do |attrs|
18
+ attrs ||= {}
19
+
20
+ # Retrieve the class
21
+ klass = Relinkly.const_get(association.to_s.relinkly_camelize)
22
+
23
+ # Ex:
24
+ # @domain = Domain.new(attrs)
25
+ instance_variable_set("@#{association}", klass.new(attrs))
26
+ end
27
+ end
28
+
29
+ alias favorite favourite
30
+ alias favorite= favourite=
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relinkly
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'relinkly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "relinkly"
8
+ spec.version = Relinkly::VERSION
9
+ spec.authors = ["Rajan Bhattarai"]
10
+ spec.email = ["hey@rajanbhattarai.com"]
11
+
12
+ spec.summary = "A Ruby wrapper for the Rebrandly API "
13
+ spec.description = "Easily create branded short links in your ruby apps using Rebrandly."
14
+ spec.homepage = "https://github.com/cdrrazan/relinkly"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.2.3"
25
+ spec.add_development_dependency "rake", "~> 13.0.3"
26
+ spec.add_development_dependency "rspec", "~> 3.10.0"
27
+ spec.add_dependency 'httparty', '~> 0.18.1'
28
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relinkly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rajan Bhattarai
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 13.0.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 13.0.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.10.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.10.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.18.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.18.1
69
+ description: Easily create branded short links in your ruby apps using Rebrandly.
70
+ email:
71
+ - hey@rajanbhattarai.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/relinkly.rb
86
+ - lib/relinkly/api.rb
87
+ - lib/relinkly/configuration.rb
88
+ - lib/relinkly/creator.rb
89
+ - lib/relinkly/domain.rb
90
+ - lib/relinkly/element.rb
91
+ - lib/relinkly/integration.rb
92
+ - lib/relinkly/link.rb
93
+ - lib/relinkly/version.rb
94
+ - relinkly.gemspec
95
+ homepage: https://github.com/cdrrazan/relinkly
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.1.4
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A Ruby wrapper for the Rebrandly API
118
+ test_files: []