class Bitly::V4::Client

Bitly Client.

Constants

BASE_URI

Public Class Methods

new(**args) click to toggle source
# File lib/bitly/v4/client.rb, line 10
def initialize(**args)
  @conn = Faraday.new(BASE_URI)
  @conn.authorization(:Bearer, args[:access_token])
  @conn.headers[:content_type] = 'application/json'
end

Public Instance Methods

shorten(long_url, options = {}) click to toggle source
# File lib/bitly/v4/client.rb, line 16
def shorten(long_url, options = {})
  body = { long_url: long_url }.merge(options)
  response = post('/v4/shorten', body.to_json)
  Bitly::V4::Bitlink.new(JSON.parse(response.body, symbolize_names: true))
end

Private Instance Methods

post(path, body) click to toggle source
# File lib/bitly/v4/client.rb, line 24
def post(path, body)
  response = @conn.post(path, body)
  raise Bitly::V4::Error.new(response.status, response.body) unless [200, 201].include?(response.status)

  response
end