Class: MxHero::API::Groups

Inherits:
Object
  • Object
show all
Includes:
Communication, Urls
Defined in:
lib/groups.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Urls

#domain_by_id_url, #domains_url, #service_url

Methods included from Communication

#call, #headers, #json_parse

Constructor Details

#initialize(domain, config = {}) ⇒ Groups

Returns a new instance of Groups.



26
27
28
29
30
31
32
33
# File 'lib/groups.rb', line 26

def initialize(domain, config = {})
  @domain      = domain
  @service_url = config[:api_url]
  @username    = config[:username]
  @password    = config[:password]
  @verbose     = config[:verbose] || false
  @as_user     = config[:as_user]
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



24
25
26
# File 'lib/groups.rb', line 24

def domain
  @domain
end

Instance Method Details

#accounts(group_name, options = { per_page: nil, page: nil }) ⇒ MxHero::API::PaginatedElements

Retrieve all the accounts for one group

Parameters:

  • group_name (String)
  • options (Hash) (defaults to: { per_page: nil, page: nil })

    pagination options (page and/or per_page)

Returns:



84
85
86
87
88
89
# File 'lib/groups.rb', line 84

def accounts(group_name, options = { per_page: nil, page: nil })
  response = call(:get, group_accounts_url(group_name, options))
  paginate_wrap response do |hash|
    hash[:elements].map { |e| Account.new(e) }
  end
end

#add_account(group_name, account_name) ⇒ MxHero::API::Response

Add an account to a group

Parameters:

  • group_name (String)
  • account_name (String)

Returns:

  • (MxHero::API::Response)

    with content empty. In case on error, may be one of the following:

    + domain.account.not.found                : Inexistent account
    + domain.group.account.already.has.group  : Try to add an account to a group when already is in that
    


100
101
102
103
# File 'lib/groups.rb', line 100

def (group_name, )
  response = call(:post, group_add_accounts_url(group_name, ), nil, throw_exception: false)
  wrap_response_from response
end

#all(pagination = { page: nil, per_page: nil }) ⇒ PaginatedElement

Retrieve all the groups

Returns:

  • (PaginatedElement)

    that contains an array of Group elements Basically its an Array with instances of [MxHero::API::Group] with the methods total_elements, total_pages and actual_page



42
43
44
45
46
47
# File 'lib/groups.rb', line 42

def all(pagination = { page: nil, per_page: nil })
  response = call(:get, groups_url(pagination))
  paginate_wrap(response) do |hash|
    hash[:elements].map { |e| Group.new(e) }
  end
end

#delete(group_name) ⇒ MxHero::API::Response

Delete the group

Parameters:

  • group_name (String)

Returns:

  • (MxHero::API::Response)

    with content empty. In case on error, may be one of the following:

    + domain.group.not.found : Inexistent group
    


72
73
74
75
# File 'lib/groups.rb', line 72

def delete(group_name)
  response = call(:delete, group_url(group_name), nil, throw_exception: false)
  wrap_response_from response
end

#remove_account(group_name, account_name) ⇒ MxHero::API::Response

Returns with content empty. In case on error, may be one of the following:

+ domain.account.not.found            : Inexistent account
+ domain.group.account.not.in.group   : Try to remove an account that is not in the group.

Parameters:

  • group_name (String)
  • account_name (String)

Returns:

  • (MxHero::API::Response)

    with content empty. In case on error, may be one of the following:

    + domain.account.not.found            : Inexistent account
    + domain.group.account.not.in.group   : Try to remove an account that is not in the group
    


113
114
115
116
# File 'lib/groups.rb', line 113

def (group_name, )
  response = call(:delete, group_remove_accounts_url(group_name, ), nil, throw_exception: false)
  wrap_response_from response
end

#save(group) ⇒ MxHero::API::Response

Save a new group

Parameters:

Returns:



60
61
62
63
# File 'lib/groups.rb', line 60

def save(group)
  group.domain = domain
  wrap_response_from call(:post, groups_url, group.to_json)
end

#search_accounts(term) ⇒ Object

Search available accounts



50
51
52
53
# File 'lib/groups.rb', line 50

def search_accounts(term)
  response = call(:get, search_accounts_url(term) ,nil, throw_exception: false)
  Response.new(response.status, json_parse(response.content))
end