class Relinkly::API

Constants

API_VERSION
BASE_URL

Public Instance Methods

account() click to toggle source

GET /v1/account

# File lib/relinkly/api.rb, line 17
def account
  Creator.new(relinkly_request(:get, 'account'))
end
delete_tag(id, options = {}) click to toggle source

DELETE /v1/tags/:id

# File lib/relinkly/api.rb, line 77
def delete_tag(id, options = {})
  Tag.new(relinkly_request(:delete, "tags/#{id}", options))
end
domain(id) click to toggle source

GET /v1/domains/:id

# File lib/relinkly/api.rb, line 38
def domain(id)
  Domain.new(relinkly_request(:get, "domains/#{id}"))
end
domain_count(_options = {}) click to toggle source

GET /v1/domains/count

# File lib/relinkly/api.rb, line 43
def domain_count(_options = {})
  relinkly_request(:get, 'domains/count')['count']
end
domains(options = {}) click to toggle source

GET /v1/domains

# File lib/relinkly/api.rb, line 32
def domains(options = {})
  all_domains = relinkly_request(:get, 'domains', options)
  all_domains.map { |domain| Domain.new(domain) }
end
new_tag(destination, options = {}) click to toggle source

POST /v1/tags

# File lib/relinkly/api.rb, line 66
def new_tag(destination, options = {})
  options[:destination] = destination
  Tag.new(relinkly_request(:post, 'tags', options))
end
shorten(destination, options = {}) click to toggle source

POST /v1/links

# File lib/relinkly/api.rb, line 100
def shorten(destination, options = {})
  options[:destination] = destination
  Link.new(relinkly_request(:post, 'links', options))
end
tag(id) click to toggle source

GET /v1/tags/:id

# File lib/relinkly/api.rb, line 56
def tag(id)
  Tag.new(relinkly_request(:get, "tags/#{id}"))
end
tag_count(_options = {}) click to toggle source

GET /v1/tags/count

# File lib/relinkly/api.rb, line 61
def tag_count(_options = {})
  relinkly_request(:get, 'tags/count')['count']
end
tags(options = {}) click to toggle source

GET /v1/tags

# File lib/relinkly/api.rb, line 50
def tags(options = {})
  all_tags = relinkly_request(:get, 'tags', options)
  all_tags.map { |tag| Tag.new(tag) }
end
update_tag(id, options = {}) click to toggle source

POST /v1/tags/:id

# File lib/relinkly/api.rb, line 72
def update_tag(id, options = {})
  Tag.new(relinkly_request(:post, "tags/#{id}", options))
end
workspaces(options = {}) click to toggle source

GET /v1/account/workspaces

# File lib/relinkly/api.rb, line 24
def workspaces(options = {})
  all_workspaces = relinkly_request(:get, 'account/workspaces', options)
  all_workspaces.map { |workspace| Workspace.new(workspace) }
end

Private Instance Methods

headers() click to toggle source
# File lib/relinkly/api.rb, line 150
def headers
  {
    'Content-type' => 'application/json',
    'apikey' => Relinkly.api_key
  }
end
relinkly_request(method, url, options = {}) click to toggle source
# File lib/relinkly/api.rb, line 121
def relinkly_request(method, url, options = {})
  url = "#{BASE_URL}/#{url}"
  # Convert all hash keys into camel case for Relinkly
  options = Hash[options.map { |k, v| [k.to_s.relinkly_lower_camelize.to_sym, v] }]
  workspace_id = options[:workspace]

  # Passes the workspace_id into header if the operation requires workspace_id to be included.
  header_with_workspace = workspace_id.nil? ? headers : headers.merge!('workspace' => workspace_id)
  http_attrs = { headers: header_with_workspace }
  case method
  when :get
    http_attrs.merge!(query: options)
  when :post
    http_attrs.merge!(body: options.to_json)
  end

  res = HTTParty.send(method, url, http_attrs)
  if res.code == 200
    JSON.parse(res.body)
  else
    relinkly_error = res.parsed_response
    if relinkly_error['domain'] == 'usageLimits' && relinkly_error['reason'] == 'rateLimitExceeded'
      raise RateLimitExceeded
    else
      raise RelinklyError, relinkly_error['message']
    end
  end
end