Class: Bitly::API::User

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/bitly/api/user.rb

Overview

A User represents the authorized user

Defined Under Namespace

Classes: Email

Instance Attribute Summary collapse

Attributes included from Base

#response

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#assign_attributes

Constructor Details

#initialize(data:, client:, response: nil) ⇒ Bitly::API::User

Creates a Bitly::API::User object.

Examples:

user = Bitly::API::User.new(data: user_data, client: client)

Parameters:

  • data (Hash<String, String | Boolean>)

    The user data from the API

  • client (Bitly::API::Client)

    The authorized API client

  • response (Bitly::HTTP::Response) (defaults to: nil)

    The original HTTP response



58
59
60
61
62
63
64
65
# File 'lib/bitly/api/user.rb', line 58

def initialize(data:, client:, response: nil)
  assign_attributes(data)
  @client = client
  @response = response
  if data["emails"]
    @emails = data["emails"].map { |e| Email.new(e) }
  end
end

Instance Attribute Details

#emailsObject (readonly)

Returns the value of attribute emails.



45
46
47
# File 'lib/bitly/api/user.rb', line 45

def emails
  @emails
end

Class Method Details

.attributesArray<Symbol>

Returns The attributes the API returns for a user.

Returns:

  • (Array<Symbol>)

    The attributes the API returns for a user



35
36
37
# File 'lib/bitly/api/user.rb', line 35

def self.attributes
  [:login, :is_active, :is_2fa_enabled, :name, :is_sso_user, :default_group_guid]
end

.fetch(client:) ⇒ Bitly::API::User

Gets the authorized user from the API. [‘GET /v4/user`](dev.bitly.com/api-reference/#getUser)

Examples:

user = Bitly::API::User.fetch(client: client)

Parameters:

Returns:



29
30
31
32
# File 'lib/bitly/api/user.rb', line 29

def self.fetch(client:)
  response = client.request(path: "/user")
  new(data: response.body, client: client, response: response)
end

.time_attributesArray<Symbol>

converted to ‘Time` objects.

Returns:

  • (Array<Symbol>)

    The attributes the API returns that need to be



40
41
42
# File 'lib/bitly/api/user.rb', line 40

def self.time_attributes
  [:created, :modified]
end

Instance Method Details

#default_groupObject

Returns the default group for the user from the default group guid

Examples:

user.default_group


74
75
76
# File 'lib/bitly/api/user.rb', line 74

def default_group
  @default_group ||= Group.fetch(client: @client, guid: default_group_guid)
end

#update(name: nil, default_group_guid: nil) ⇒ Bitly::API::User

Allows you to update the authorized user’s name or default group guid. If you update the default group ID and have already loaded the default group, it is nilled out so it can be reloaded with the correct ID. [‘PATCH /v4/user`](dev.bitly.com/api-reference/#updateUser).

Examples:

user.update(name: "New Name", default_group_guid: "aaabbb")

Parameters:

  • name (String) (defaults to: nil)

    A new name

  • default_group_guid (String) (defaults to: nil)

    A new default guid

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bitly/api/user.rb', line 91

def update(name: nil, default_group_guid: nil)
  params = { "name" => name }
  if default_group_guid
    params["default_group_guid"] = default_group_guid
    @default_group = nil
  end
  @response = @client.request(
    path: "/user",
    method: "PATCH",
    params: params
  )
  assign_attributes(@response.body)
  self
end