Sha256: 86e85440833d7d06c0ffe85e574d3664f74756f7e3b25ddf6e2fad9d63e7326d

Contents?: true

Size: 848 Bytes

Versions: 1

Compression:

Stored size: 848 Bytes

Contents

# frozen_string_literal: true

module Bitly
  module V4
    # Bitly Client.
    class Client
      BASE_URI = 'https://api-ssl.bitly.com/'
      private_constant :BASE_URI

      def initialize(**args)
        @conn = Faraday.new(BASE_URI)
        @conn.authorization(:Bearer, args[:access_token])
        @conn.headers[:content_type] = 'application/json'
      end

      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

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bitly-client-0.1.0 lib/bitly/v4/client.rb