Class: Sink::Client

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

Constant Summary collapse

REQUEST_CLASSES =
{
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  put: Net::HTTP::Put
}.freeze
%i[
  url slug comment expiration expires_at title description image apple google
  cloaking redirect_with_query password unsafe geo tags
].freeze
TIMEOUT_ERRORS =
[Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, Timeout::Error].freeze
CONNECTION_ERRORS =
[
  IOError, SocketError, SystemCallError, Net::ProtocolError, OpenSSL::SSL::SSLError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, token:, open_timeout: 5, read_timeout: 30) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/sink/client.rb', line 26

def initialize(base_url:, token:, open_timeout: 5, read_timeout: 30)
  @token = token.to_s
  raise ArgumentError, "token is required" if @token.empty?

  @base_url = validate_base_url(base_url)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details



89
90
91
92
93
94
95
# File 'lib/sink/client.rb', line 89

def check_links(cursor: nil, limit: 6, timeout: 6)
  body = request(:post, "/api/link/check", body: {
    "cursor" => cursor, "limit" => limit, "timeout" => timeout
  }.compact)

  page(body, "results") { |result| Keys.underscore_keys(result) }
end


37
38
39
# File 'lib/sink/client.rb', line 37

def create_link(url:, **attributes)
  build_link(request(:post, "/api/link/create", body: link_payload(attributes.merge(url: url))))
end


49
50
51
52
# File 'lib/sink/client.rb', line 49

def delete_link(slug)
  request(:post, "/api/link/delete", body: { "slug" => slug })
  true
end


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

def each_link(**options)
  return enum_for(:each_link, **options) unless block_given?

  cursor = nil
  loop do
    page = links(**options, cursor: cursor)
    page.each { |link| yield link }
    break unless page.more?

    cursor = page.cursor
  end
end


41
42
43
# File 'lib/sink/client.rb', line 41

def edit_link(url:, slug:, **attributes)
  build_link(request(:put, "/api/link/edit", body: link_payload(attributes.merge(url: url, slug: slug))))
end


54
# File 'lib/sink/client.rb', line 54

def link(slug) = build_link(request(:get, "/api/link/query", query: { slug: slug }))


56
57
58
59
60
61
62
# File 'lib/sink/client.rb', line 56

def links(limit: nil, cursor: nil, sort: nil, tag: nil, status: nil)
  body = request(:get, "/api/link/list", query: {
    limit: limit, cursor: cursor, sort: sort, tag: tag, status: status
  })

  link_page(body)
end


77
78
79
80
81
82
83
# File 'lib/sink/client.rb', line 77

def search_links(query: nil, url: nil, limit: nil, tag: nil, status: nil)
  body = request(:get, "/api/link/search", query: {
    q: query, url: url, limit: limit, tag: tag, status: status
  })

  link_page(body)
end

#tagsObject



85
86
87
# File 'lib/sink/client.rb', line 85

def tags
  Array(request(:get, "/api/link/tags")).map { |tag| Tag.from_response(tag) }
end


45
46
47
# File 'lib/sink/client.rb', line 45

def upsert_link(url:, **attributes)
  build_link(request(:post, "/api/link/upsert", body: link_payload(attributes.merge(url: url))))
end

#verifyObject



35
# File 'lib/sink/client.rb', line 35

def verify = Keys.underscore_keys(request(:get, "/api/verify") || {})