Class: Rerout::Resources::Links
- Inherits:
-
Object
- Object
- Rerout::Resources::Links
- Defined in:
- lib/rerout/links.rb
Overview
Link operations namespace.
Instance Method Summary collapse
-
#create(input) ⇒ Rerout::Models::Link
Create a new short link.
-
#create_batch(inputs) ⇒ Rerout::Models::BatchCreateLinksResult
Create many links in one call via ‘POST /v1/links/batch`.
-
#delete(code) ⇒ Hash
Soft-delete a link.
-
#get(code) ⇒ Rerout::Models::Link
Fetch a single link.
-
#initialize(client) ⇒ Links
constructor
A new instance of Links.
-
#list(cursor: nil, limit: nil) ⇒ Rerout::Models::ListLinksResult
Paginated list of links.
-
#stats(code, days: 30) ⇒ Rerout::Models::LinkStats
Per-link click stats.
-
#update(code, input) ⇒ Rerout::Models::Link
Update a link.
Constructor Details
#initialize(client) ⇒ Links
Returns a new instance of Links.
10 11 12 |
# File 'lib/rerout/links.rb', line 10 def initialize(client) @client = client end |
Instance Method Details
#create(input) ⇒ Rerout::Models::Link
Create a new short link.
18 19 20 21 22 |
# File 'lib/rerout/links.rb', line 18 def create(input) body = coerce_input(input) response = @client.request(method: :post, path: '/v1/links', body: body) Models::Link.from_hash(response) end |
#create_batch(inputs) ⇒ Rerout::Models::BatchCreateLinksResult
Create many links in one call via ‘POST /v1/links/batch`.
Each item may be a CreateLinkInput or a plain Hash. Only the batch-supported fields (‘target_url`, `code`, `expires_at`, `domain_hostname`) are forwarded — richer Smart Link fields are not accepted by the batch endpoint. A failed item does not raise; inspect `result.results.ok`.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rerout/links.rb', line 34 def create_batch(inputs) links = Array(inputs).map { |item| batch_link_hash(item) } if links.empty? raise Error.new( code: 'bad_request', message: 'create_batch requires at least one link.', status: 0 ) end response = @client.request(method: :post, path: '/v1/links/batch', body: { 'links' => links }) Models::BatchCreateLinksResult.from_hash(response) end |
#delete(code) ⇒ Hash
Soft-delete a link.
93 94 95 |
# File 'lib/rerout/links.rb', line 93 def delete(code) @client.request(method: :delete, path: link_path(code)) end |
#get(code) ⇒ Rerout::Models::Link
Fetch a single link.
65 66 67 68 |
# File 'lib/rerout/links.rb', line 65 def get(code) response = @client.request(method: :get, path: link_path(code)) Models::Link.from_hash(response) end |
#list(cursor: nil, limit: nil) ⇒ Rerout::Models::ListLinksResult
Paginated list of links.
53 54 55 56 57 58 59 |
# File 'lib/rerout/links.rb', line 53 def list(cursor: nil, limit: nil) query = {} query['cursor'] = cursor unless cursor.nil? query['limit'] = limit unless limit.nil? response = @client.request(method: :get, path: '/v1/links', query: query) Models::ListLinksResult.from_hash(response) end |
#stats(code, days: 30) ⇒ Rerout::Models::LinkStats
Per-link click stats. Defaults to 30 days.
102 103 104 105 106 107 108 109 |
# File 'lib/rerout/links.rb', line 102 def stats(code, days: 30) response = @client.request( method: :get, path: "#{link_path(code)}/stats", query: { 'days' => days } ) Models::LinkStats.from_hash(response) end |
#update(code, input) ⇒ Rerout::Models::Link
Update a link. Only fields set on ‘input` are sent.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rerout/links.rb', line 75 def update(code, input) raise ArgumentError, 'input must be a Rerout::UpdateLinkInput' unless input.is_a?(UpdateLinkInput) if input.empty? raise Error.new( code: 'empty_update', message: 'UpdateLinkInput has no fields set; refusing to send empty PATCH.', status: 0 ) end response = @client.request(method: :patch, path: link_path(code), body: input.to_h) Models::Link.from_hash(response) end |