Class: ShortIo::ShortUrl
- Inherits:
-
Object
- Object
- ShortIo::ShortUrl
- Defined in:
- lib/short_io/short_url.rb
Overview
Add domain and list domains registerd to Short.io
Constant Summary collapse
- REQUEST_TYPE =
'application/json'- SHORT_IO_BASE_URL =
'https://api.short.io/domains/'- SHORT_IO_API_BASE_URL =
'https://api.short.io/api/domains'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#host_name ⇒ Object
readonly
Returns the value of attribute host_name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#add_domain ⇒ JSON
Add new domain.
-
#check_variables ⇒ Object
Check Variables.
-
#delete_domain ⇒ JSON
Delete a domain.
-
#domain_list ⇒ JSON
Domain List.
-
#initialize(host_name, api_key, short_io_base_url = SHORT_IO_BASE_URL, options = {}) ⇒ ShortUrl
constructor
A new instance of ShortUrl.
-
#options_default_value ⇒ Hash
Options Default Value.
-
#setup(action = nil, base_url = SHORT_IO_BASE_URL) ⇒ Object
Prepare request.
-
#update_domain ⇒ JSON
Udpadating a domain.
Constructor Details
#initialize(host_name, api_key, short_io_base_url = SHORT_IO_BASE_URL, options = {}) ⇒ ShortUrl
Returns a new instance of ShortUrl.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/short_io/short_url.rb', line 16 def initialize(host_name, api_key, short_io_base_url=SHORT_IO_BASE_URL, ={}) @host_name ||= host_name @api_key ||= api_key @short_io_base_url ||= short_io_base_url ||= unless .key?(:domain_id) check_variables end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/short_io/short_url.rb', line 14 def api_key @api_key end |
#host_name ⇒ Object (readonly)
Returns the value of attribute host_name.
14 15 16 |
# File 'lib/short_io/short_url.rb', line 14 def host_name @host_name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/short_io/short_url.rb', line 14 def end |
Instance Method Details
#add_domain ⇒ JSON
Add new domain
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/short_io/short_url.rb', line 82 def add_domain setup request = Net::HTTP::Post.new(@url) request["accept"] = REQUEST_TYPE request["content-type"] = REQUEST_TYPE request["authorization"] = @api_key request.body = JSON.generate( { "hideReferer":"#{@options[:hide_referer]}", "httpsLinks":"#{@options[:https_link]}", "hostname":"#{@host_name}", "linkType":"#{@options[:link_type]}" } ) response = @http.request(request) return response.read_body end |
#check_variables ⇒ Object
Check Variables
44 45 46 47 |
# File 'lib/short_io/short_url.rb', line 44 def check_variables raise ShortIo::HostNameError.new(host_name: 'Please provide a host name') if (@host_name.nil? || @host_name.empty?) raise ShortIo::ApiKeyError.new(api_key: 'Please provide an API key') if (@api_key.nil? || @api_key.empty?) end |
#delete_domain ⇒ JSON
Delete a domain
114 115 116 117 118 119 120 121 122 |
# File 'lib/short_io/short_url.rb', line 114 def delete_domain setup("delete/#{@options[:domain_id]}") request = Net::HTTP::Post.new(@url) request["authorization"] = @api_key response = @http.request(request) return response.read_body end |
#domain_list ⇒ JSON
Domain List
188 189 190 191 192 193 194 195 196 |
# File 'lib/short_io/short_url.rb', line 188 def domain_list setup(nil, SHORT_IO_API_BASE_URL) request = Net::HTTP::Get.new(@url) request["accept"] = REQUEST_TYPE request["authorization"] = @api_key response = @http.request(request) return response.read_body end |
#options_default_value ⇒ Hash
Options Default Value
31 32 33 34 35 36 37 |
# File 'lib/short_io/short_url.rb', line 31 def = { hide_referer: .key?(:hide_referer) ? [:hide_referer] : false, https_link: .key?(:https_link) ? [:https_link] : false, link_type: .key?(:link_type) ? [:link_type] : 'random' } end |
#setup(action = nil, base_url = SHORT_IO_BASE_URL) ⇒ Object
Prepare request
51 52 53 54 55 56 |
# File 'lib/short_io/short_url.rb', line 51 def setup(action=nil, base_url=SHORT_IO_BASE_URL) @url = action.nil? ? URI(base_url) : URI(base_url + action) @http = Net::HTTP.new(@url.host, @url.port) @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE end |
#update_domain ⇒ JSON
Udpadating a domain
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/short_io/short_url.rb', line 137 def update_domain setup("settings/#{@options[:domain_id]}") request = Net::HTTP::Post.new(@url) request["accept"] = REQUEST_TYPE request["content-type"] = REQUEST_TYPE request["authorization"] = @api_key request.body = JSON.generate({ 'rootRedirect': "#{@options[:root_redirect_url]}" }) response = @http.request(request) return response.read_body end |