Class: Shortify::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/shortify/client.rb

Constant Summary collapse

CREATE_ENDPOINT =
"/redirects".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, username, password) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
# File 'lib/shortify/client.rb', line 6

def initialize(base_uri, username, password)
  @base_uri = HTTParty.normalize_base_uri(base_uri)
  @username = username
  @password = password
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



4
5
6
# File 'lib/shortify/client.rb', line 4

def base_uri
  @base_uri
end

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'lib/shortify/client.rb', line 4

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



4
5
6
# File 'lib/shortify/client.rb', line 4

def username
  @username
end

Instance Method Details

#short_url_for(target_url) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/shortify/client.rb', line 12

def short_url_for(target_url)
  post_options = options.merge(body: { url: target_url }.to_json)
  response = HTTParty.post(CREATE_ENDPOINT, post_options)
  fail error_for_response(response) unless response.key?("token")

  URI.parse("#{base_uri}/#{response['token']}") if response.key?("token")
end