Class: Sink::Client
- Inherits:
-
Object
- Object
- Sink::Client
- Defined in:
- lib/sink/client.rb
Constant Summary collapse
- REQUEST_CLASSES =
{ get: Net::HTTP::Get, post: Net::HTTP::Post, put: Net::HTTP::Put }.freeze
- LINK_ATTRIBUTES =
%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
- #check_links(cursor: nil, limit: 6, timeout: 6) ⇒ Object
- #create_link(url:, **attributes) ⇒ Object
- #delete_link(slug) ⇒ Object
- #each_link(**options) ⇒ Object
- #edit_link(url:, slug:, **attributes) ⇒ Object
-
#initialize(base_url:, token:, open_timeout: 5, read_timeout: 30) ⇒ Client
constructor
A new instance of Client.
- #link(slug) ⇒ Object
- #links(limit: nil, cursor: nil, sort: nil, tag: nil, status: nil) ⇒ Object
- #search_links(query: nil, url: nil, limit: nil, tag: nil, status: nil) ⇒ Object
- #tags ⇒ Object
- #upsert_link(url:, **attributes) ⇒ Object
- #verify ⇒ Object
Constructor Details
#initialize(base_url:, token:, open_timeout: 5, read_timeout: 30) ⇒ Client
Returns a new instance of Client.
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
#check_links(cursor: nil, limit: 6, timeout: 6) ⇒ Object
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 |
#create_link(url:, **attributes) ⇒ Object
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 |
#delete_link(slug) ⇒ Object
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 |
#each_link(**options) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sink/client.rb', line 64 def each_link(**) return enum_for(:each_link, **) unless block_given? cursor = nil loop do page = links(**, cursor: cursor) page.each { |link| yield link } break unless page.more? cursor = page.cursor end end |
#edit_link(url:, slug:, **attributes) ⇒ Object
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 |
#link(slug) ⇒ Object
54 |
# File 'lib/sink/client.rb', line 54 def link(slug) = build_link(request(:get, "/api/link/query", query: { slug: slug })) |
#links(limit: nil, cursor: nil, sort: nil, tag: nil, status: nil) ⇒ Object
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 |
#search_links(query: nil, url: nil, limit: nil, tag: nil, status: nil) ⇒ Object
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 |
#tags ⇒ Object
85 86 87 |
# File 'lib/sink/client.rb', line 85 def Array(request(:get, "/api/link/tags")).map { |tag| Tag.from_response(tag) } end |
#upsert_link(url:, **attributes) ⇒ Object
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 |
#verify ⇒ Object
35 |
# File 'lib/sink/client.rb', line 35 def verify = Keys.underscore_keys(request(:get, "/api/verify") || {}) |