rebrandly 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: caf24ed175471cbc2765aadb9b261a055d931126
4
+ data.tar.gz: 8b98d6fbb0ede8aafd49b1b43e6799b03f2f7184
5
+ SHA512:
6
+ metadata.gz: 52c14cb4085f2931cc820df68bd10af61ad4bfa25c8ec5fbee0a5e321ab47cb4e4ea58350060b2c690acd48d0c54540a711fbd33734dffcff1d28a8259c8e838
7
+ data.tar.gz: 916ae46f6e1b47be7cf6e57ac2811ad748414fa23b0255844c78d07dc905e402e67a5823a325c1e1d469d4e563628b8585d80fc9b46c963f6e4e4d71b10e95f3
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.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
+ - 1.9.3
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rebrandly.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,70 @@
1
+ # Rebrandly
2
+
3
+ This is very much a work in progress. I've created this so I could use Ruby to
4
+ shorten URL's with Rebrandly. I hope to continue to work on this gem in the near
5
+ future to do testing, error checking, etc. This is a very early version.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rebrandly'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rebrandly
22
+
23
+ ## Usage
24
+
25
+ If using Rails, put the following into your application.rb. If just Ruby,
26
+ run this before trying to use the API.
27
+
28
+ ```ruby
29
+ Rebrandly.configure do |config|
30
+ config.api_key = 'YOUR_KEY_HERE'
31
+ end
32
+ ```
33
+
34
+ ### Instantiate an API object.
35
+
36
+ ```ruby
37
+ api = Rebrandly::Api.new
38
+ ```
39
+
40
+ ### API Requests
41
+
42
+ ```ruby
43
+ api.links # GET /v1/links
44
+ api.links(id) # GET /v1/links/:id
45
+ api.link_count(options) # GET /v1/links/count
46
+ api.new_link(options) # GET /v1/links/new
47
+ api.shorten(destination, options) # POST /v1/links
48
+ api.update_link(id, options) # POST /v1/links/:id
49
+ api.delete(id, options) # DELETE /v1/links/:id
50
+ api.domains # GET /v1/domains
51
+ api.domain(id) # GET /v1/domains/:id
52
+ api.domain_count(options) # GET /v1/domains/count
53
+ api.account # GET /v1/account
54
+ ```
55
+
56
+ ### Make a new short link
57
+
58
+ ```ruby
59
+ my_domain = api.domains.first
60
+ link = api.shorten('https://google.com', domain: my_domain.to_h, title: 'Google', description: 'The Googles!!!', favourite: true)
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ardavis/rebrandly.
66
+
67
+
68
+ ## License
69
+
70
+ 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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rebrandly"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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,55 @@
1
+ require 'rebrandly/version'
2
+ require 'rebrandly/configuration'
3
+ require 'rebrandly/api'
4
+ require 'rebrandly/element'
5
+ require 'rebrandly/link'
6
+ require 'rebrandly/creator'
7
+ require 'rebrandly/domain'
8
+ require 'rebrandly/integration'
9
+
10
+ module Rebrandly
11
+ class << self
12
+ attr_accessor :configuration
13
+ end
14
+
15
+ def self.api_key
16
+ configuration.api_key
17
+ end
18
+
19
+ def self.configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def self.configure
24
+ yield(configuration)
25
+ end
26
+ end
27
+
28
+ class String
29
+ def underscore
30
+ self.gsub(/::/, '/').
31
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
32
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
33
+ tr("-", "_").
34
+ downcase
35
+ end
36
+
37
+ def camelize
38
+ self.split('_').collect(&:capitalize).join
39
+ end
40
+
41
+ def lower_camelize
42
+ res = self.camelize
43
+ res[0].downcase + res[1..-1]
44
+ end
45
+ end
46
+
47
+ class Hash
48
+ def self.stringify_keys(hash)
49
+ stringified_hash = {}
50
+ hash.each do |k, v|
51
+ stringified_hash[k.to_s] = v.is_a?(Hash) ? stringify_keys(v) : v
52
+ end
53
+ stringified_hash
54
+ end
55
+ end
@@ -0,0 +1,97 @@
1
+ require 'httparty'
2
+
3
+ module Rebrandly
4
+ class Api
5
+ API_VERSION = 'v1'
6
+ BASE_URL = "https://api.rebrandly.com/#{API_VERSION}"
7
+
8
+ # GET /v1/links
9
+ def links(options={})
10
+ all_links = rebrandly_request(:get, 'links', options)
11
+ all_links.map { Link.new(all_links.first) }
12
+ end
13
+
14
+ # GET /v1/links/:id
15
+ def link(id)
16
+ Link.new(rebrandly_request(:get, "links/#{id.to_s}"))
17
+ end
18
+
19
+ # GET /v1/links/count
20
+ def link_count(options={})
21
+ rebrandly_request(:get, 'links/count')['count']
22
+ end
23
+
24
+ # GET /v1/links/new
25
+ def new_link(options={})
26
+ Link.new(rebrandly_request(:get, 'links/new', options))
27
+ end
28
+
29
+ # POST /v1/links
30
+ def shorten(destination, options={})
31
+ options[:destination] = destination
32
+ Link.new(rebrandly_request(:post, 'links', options))
33
+ end
34
+
35
+ # POST /v1/links/:id
36
+ def update_link(id, options={})
37
+ Link.new(rebrandly_request(:post, "links/#{id}", options))
38
+ end
39
+
40
+ # DELETE /v1/links/:id
41
+ def delete(id, options={})
42
+ Link.new(rebrandly_request(:delete, "links/#{id}", options))
43
+ end
44
+
45
+ # GET /v1/domains
46
+ def domains(options={})
47
+ all_domains = rebrandly_request(:get, 'domains', options)
48
+ all_domains.map { Domain.new(all_domains.first) }
49
+ end
50
+
51
+ # GET /v1/domains/:id
52
+ def domain(id)
53
+ Domain.new(rebrandly_request(:get, "domains/#{id.to_s}"))
54
+ end
55
+
56
+ # GET /v1/domains/count
57
+ def domain_count(options={})
58
+ rebrandly_request(:get, 'domains/count')['count']
59
+ end
60
+
61
+ # GET /v1/account
62
+ def account
63
+ Creator.new(rebrandly_request(:get, 'account'))
64
+ end
65
+
66
+ private
67
+
68
+ def rebrandly_request(method, url, options={})
69
+ url = "#{BASE_URL}/#{url}"
70
+ # Convert all hash keys into camel case for Rebrandly
71
+ options = Hash[options.map { |k,v| [k.to_s.lower_camelize.to_sym, v] }].to_json
72
+
73
+ http_attrs = { headers: headers }
74
+ case method
75
+ when :get
76
+ http_attrs.merge!(query: options)
77
+ when :post
78
+ http_attrs.merge!(body: options)
79
+ end
80
+
81
+ res = HTTParty.send(method, url, http_attrs)
82
+ if res.code == 200
83
+ JSON.parse(res.body)
84
+ else
85
+ raise res.parsed_response['message']
86
+ end
87
+ end
88
+
89
+ def headers
90
+ {
91
+ 'Content-type' => 'application/json',
92
+ 'apikey' => Rebrandly.api_key
93
+ }
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,9 @@
1
+ module Rebrandly
2
+ class Configuration
3
+ attr_accessor :api_key
4
+
5
+ def initialize
6
+ @api_key = nil
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Rebrandly
2
+ class Creator < Element
3
+ attr_accessor :id, :full_name, :avatar_url, :username, :email, :created_at
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Rebrandly
2
+ class Domain < Element
3
+ attr_accessor :id, :ref, :full_name, :top_level_domain, :level,
4
+ :created_at, :updated_at, :custom_homepage, :owner_id, :type,
5
+ :subdomains, :managed, :status, :https, :active
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Rebrandly
2
+ # General class to map Rebrandly objects to Ruby objects
3
+ class Element
4
+ def initialize(attrs={})
5
+ # Only set the attributes if the method exists, this way we can ignore deprecated attributes
6
+ attrs.each { |k,v| send("#{k.underscore}=", v) if respond_to?("#{k.underscore}=") }
7
+ end
8
+
9
+ def to_h
10
+ Hash[
11
+ *instance_variables.map do |variable|
12
+ name = variable[1..-1].to_sym # Drop the @ sign at the front
13
+ [name.to_sym, instance_variable_get(variable)]
14
+ end.flatten
15
+ ]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module Rebrandly
2
+ class Integration < Element
3
+ attr_accessor :link, :name, :link_id
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ module Rebrandly
2
+ class Link < Element
3
+ attr_accessor :id, :link_id, :title, :slashtag, :destination, :created_at,
4
+ :updated_at, :status, :tags, :scripts, :forward_parameters,
5
+ :clicks, :last_click_date, :last_click_at, :is_public,
6
+ :short_url, :domain_id, :domain_name,
7
+ :https, :favourite
8
+
9
+
10
+ # Associations
11
+ %i(domain creator integration).each do |association|
12
+ # Creates the getter methods, such as "@instance.domain"
13
+ attr_reader association
14
+
15
+ # Creates the setter methods, such as "@instance.domain = ..."
16
+ define_method("#{association}=") do |attrs|
17
+ attrs ||= {}
18
+
19
+ # Retrieve the class
20
+ klass = Object.const_get("Rebrandly::#{association.to_s.camelize}")
21
+
22
+ # Ex:
23
+ # @domain = Domain.new(attrs)
24
+ instance_variable_set("@#{association}", klass.new(attrs))
25
+ end
26
+ end
27
+
28
+
29
+ alias :favorite :favourite
30
+ alias :favorite= :favourite=
31
+
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Rebrandly
2
+ VERSION = "0.1.0"
3
+ 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 'rebrandly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rebrandly"
8
+ spec.version = Rebrandly::VERSION
9
+ spec.authors = ["Andrew Davis"]
10
+ spec.email = ["andrew@andrewrdavis.net"]
11
+
12
+ spec.summary = "A Ruby Rebrandly Wrapper"
13
+ spec.description = "Easily use Rebrandly's HTTP API using this gem."
14
+ spec.homepage = "https://github.com/ardavis/rebrandly"
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", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_dependency 'httparty', '~> 0.14'
28
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rebrandly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Davis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-10 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: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
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.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.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.14'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.14'
69
+ description: Easily use Rebrandly's HTTP API using this gem.
70
+ email:
71
+ - andrew@andrewrdavis.net
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/rebrandly.rb
86
+ - lib/rebrandly/api.rb
87
+ - lib/rebrandly/configuration.rb
88
+ - lib/rebrandly/creator.rb
89
+ - lib/rebrandly/domain.rb
90
+ - lib/rebrandly/element.rb
91
+ - lib/rebrandly/integration.rb
92
+ - lib/rebrandly/link.rb
93
+ - lib/rebrandly/version.rb
94
+ - rebrandly.gemspec
95
+ homepage: https://github.com/ardavis/rebrandly
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
+ rubyforge_project:
115
+ rubygems_version: 2.6.8
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: A Ruby Rebrandly Wrapper
119
+ test_files: []