Class: ShortIo::ShortUrl

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  @host_name          ||= host_name
  @api_key            ||= api_key
  @short_io_base_url  ||= short_io_base_url
  @options            ||= options
  unless options.key?(:domain_id)
    options_default_value
    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 options
  @options
end

Instance Method Details

#add_domain ⇒ JSON

Add new domain

Examples:

ShortIo::ShortUrl.new('example.com', 'YOUR_API_KEY').add_domain
  {
    linkType: 'random',
    state: 'configured',
    cloaking: false,
    setupType: 'dns',
    httpsLinks: false,
    id: 91576,
    hostname: 'yourdomain.com',
    UserId: 9346,
    updatedAt: '2022-02-03T10:22:47.010Z',
    createdAt: '2022-02-03T10:22:46.649Z',
    provider: null,
    unicodeHostname: 'urdomain.com',
    clientStorage: null
  }

Returns:

  • (JSON) —

    return in JSON format.

See Also:



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

Raises:



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

Examples:

ShortIo::ShortUrl.new('example.com', 'YOUR_API_KEY', {domain_id: YOUR_DOMAIN_ID}).delete_domain
  response:
  {
    success: true
  }

Returns:

  • (JSON) —

    return in JSON format.

See Also:



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

Examples:

ShortIo::ShortUrl.new('example.com', 'YOUR_API_KEY').domain_list
  response:
  {
    id: 7252,
   hostname: 'yrbrand.co',
   title: null,
   segmentKey: null,
   linkType: 'increment',
   state: 'not_configured',
   provider: 'cloudflare',
   redirect404: 'https://short.cm',
   hideReferer: 1,
   caseSensitive: true,
   exportEnabled: true,
   cloaking: false,
   jsRedir: true,
   incrementCounter: 'A',
   setupType: 'js',
   autodeletePeriod: 1,
   httpsLinks: true,
   clientStorage: '{"configurationHidden":false}',
   integrationGA: null,
   integrationFB: null,
   integrationAdroll: null,
   integrationGTM: null,
   createdAt: '2017-12-07T08:24:41.000Z',
   updatedAt: '2019-12-24T13:08:30.000Z',
   TeamId: 1381,
   unicodeHostname: 'yrbrand.co'
 }

Returns:

  • (JSON) —

    return domain list in JSON format.

See Also:



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

Returns:

  • (Hash) —

    return default options in Hash if no value provided.



31
32
33
34
35
36
37
# File 'lib/short_io/short_url.rb', line 31

def options_default_value
  @options = {
    hide_referer: options.key?(:hide_referer) ? options[:hide_referer] : false,
    https_link: options.key?(:https_link) ? options[:https_link] : false,
    link_type: options.key?(:link_type) ? options[: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

Examples:

ShortIo::ShortUrl.new('example.com', 'YOUR_API_KEY', {domain_id: YOUR_DOMAIN_ID, root_redirect_url: 'YOUR_LINK'}).update_domain
  response:
  {
    success: true
  }

Returns:

  • (JSON) —

    return in JSON format.

See Also:



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