Class: Kutt

Inherits:
Object
  • Object
show all
Defined in:
lib/kutt.rb

Instance Method Summary collapse

Constructor Details

#initialize(apikey, base_url = 'https://kutt.it') ⇒ Kutt

Returns a new instance of Kutt.



5
6
7
8
9
10
# File 'lib/kutt.rb', line 5

def initialize(apikey, base_url = 'https://kutt.it')
  @base_url = base_url
  @headers = { 'Content-Type': 'application/json', 'Accept': 'application/json',
     'X-API-Key': apikey}
  return true
end

Instance Method Details

#countObject



51
52
53
54
55
# File 'lib/kutt.rb', line 51

def count
  r = HTTParty.get(@base_url + '/api/url/geturls?count=0',
    headers: @headers)['countAll']
    return r
end

#delete(target) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kutt.rb', line 26

def delete(target)
  url_array = target.split('/')
  id = url_array.last

  payload = { 'id': id }

  r = HTTParty.post(@base_url + '/api/url/deleteurl', headers: @headers,
    body: payload.to_json)

  return r.code, r.to_hash
end

#list(count = 5, page = 1) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/kutt.rb', line 57

def list(count = 5, page = 1)
  r = HTTParty.get(@base_url + "/api/url/geturls?count=#{count}&page=
    #{page}",
    headers: @headers)

  return r.code, r.to_hash
end

#list_allObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kutt.rb', line 65

def list_all
  num_pages = (count.to_f / 50).ceil
  return ''.to_hash if num_pages.zero?

  full_list = []
  (1..num_pages).each do |page|
    r = HTTParty.get(@base_url + "/api/url/geturls?count=50&page=#{page}",
      :headers => @headers)['list']
    (full_list << r).flatten!
  end
  return r.code, full_list
end

#stats(target) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kutt.rb', line 38

def stats(target)
  url_array = target.split('/')
  id = url_array.last

  payload = {'id': id }

  r = HTTParty.get(@base_url + '/api/url/stats',
    headers: @headers,
    query: payload)

  return r.code, r.to_hash
end

#submit(url, **args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kutt.rb', line 12

def submit(url, **args)
  payload = {}
  payload['target'] =    url
  payload['customurl'] = args[:customurl] if args[:customurl]
  payload['password'] =  args[:password]  if args[:password]
  payload['reuse'] =     'true'           if args[:reuse] == true

  r = HTTParty.post(@base_url + '/api/url/submit',
    headers: @headers,
    body: payload.to_json)

  return r.code, r.to_hash
end