class Rebrandly::Api

Constants

API_VERSION
BASE_URL

Public Instance Methods

account() click to toggle source

GET /v1/account

# File lib/rebrandly/api.rb, line 60
def account
  Creator.new(rebrandly_request(:get, 'account'))
end
delete(id, options={}) click to toggle source

DELETE /v1/links/:id

# File lib/rebrandly/api.rb, line 39
def delete(id, options={})
  Link.new(rebrandly_request(:delete, "links/#{id}", options))
end
domain(id) click to toggle source

GET /v1/domains/:id

# File lib/rebrandly/api.rb, line 50
def domain(id)
  Domain.new(rebrandly_request(:get, "domains/#{id.to_s}"))
end
domain_count(options={}) click to toggle source

GET /v1/domains/count

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

GET /v1/domains

# File lib/rebrandly/api.rb, line 44
def domains(options={})
  all_domains = rebrandly_request(:get, 'domains', options)
  all_domains.map { Domain.new(all_domains.first) }
end
shorten(destination, options={}) click to toggle source

POST /v1/links

# File lib/rebrandly/api.rb, line 28
def shorten(destination, options={})
  options[:destination] = destination
  Link.new(rebrandly_request(:post, 'links', options))
end

Private Instance Methods

headers() click to toggle source
# File lib/rebrandly/api.rb, line 92
def headers
  {
    'Content-type' => 'application/json',
    'apikey' => Rebrandly.api_key
  }
end
rebrandly_request(method, url, options={}) click to toggle source
# File lib/rebrandly/api.rb, line 66
def rebrandly_request(method, url, options={})
  url = "#{BASE_URL}/#{url}"
  # Convert all hash keys into camel case for Rebrandly
  options = Hash[options.map { |k,v| [k.to_s.rebrandly_lower_camelize.to_sym, v] }]

  http_attrs = { headers: headers }
  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
    rebrandly_error = res.parsed_response
    if rebrandly_error['domain'] == 'usageLimits' && rebrandly_error['reason'] == 'rateLimitExceeded'
      raise RateLimitExceeded
    else
      raise RebrandlyError, rebrandly_error['message']
    end
  end
end