class MxHero::API::Groups

Attributes

domain[R]

Public Class Methods

new(domain, config = {}) click to toggle source
# 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

Public Instance Methods

accounts(group_name, options = { per_page: nil, page: nil }) click to toggle source

Retrieve all the accounts for one group

@param group_name [String] @param options [Hash] pagination options (page and/or per_page)

@return [MxHero::API::PaginatedElements] the list of accounts [MxHero::API::Account]

# 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) click to toggle source

Add an account to a group

@param group_name [String] @param account_name [String]

@return [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
# File lib/groups.rb, line 100
def add_account(group_name, account_name)
  response = call(:post, group_add_accounts_url(group_name, account_name), nil, throw_exception: false)
  wrap_response_from response
end
all(pagination = { page: nil, per_page: nil }) click to toggle source

Retrieve all the groups

@params pagination info. Ex.: page: 2, per_page: 10 or simple page: 2

@return [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
# 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) click to toggle source

Delete the group

@param group_name [String]

@return [MxHero::API::Response] with content empty.

In case on error, may be one of the following:
  + domain.group.not.found : Inexistent group
# 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) click to toggle source

@param group_name [String] @param account_name [String]

@return [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
# File lib/groups.rb, line 113
def remove_account(group_name, account_name)
  response = call(:delete, group_remove_accounts_url(group_name, account_name), nil, throw_exception: false)
  wrap_response_from response
end
save(group) click to toggle source

Save a new group

@param group [MxHero::API::Group]

@return [MxHero::API::Response]

# 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) click to toggle source

Search available accounts

# 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

Private Instance Methods

group_accounts_url(group_name, pagination = {}) click to toggle source
# File lib/groups.rb, line 157
def group_accounts_url(group_name, pagination = {})
  group_url(group_name) + "/accounts" + pagination_query(pagination)
end
group_add_accounts_url(group_name, account_name) click to toggle source
# File lib/groups.rb, line 161
def group_add_accounts_url(group_name, account_name)
  group_accounts_url(group_name) + "/#{account_name}/add"
end
group_from(response) click to toggle source
# File lib/groups.rb, line 131
def group_from(response)
  return nil if response.content.nil? || response.content.empty?
  hash = json_parse response.content
  Group.new hash
end
group_remove_accounts_url(group_name, account_name) click to toggle source
# File lib/groups.rb, line 165
def group_remove_accounts_url(group_name, account_name)
  group_accounts_url(group_name) + "/#{account_name}/remove"
end
group_url(group_name) click to toggle source
# File lib/groups.rb, line 153
def group_url(group_name)
  groups_url + "/#{URI::encode(group_name)}"
end
groups_url(pagination = {}) click to toggle source
# File lib/groups.rb, line 141
def groups_url(pagination = {})
  domain_by_id_url(domain) + '/groups' + pagination_query(pagination)
end
paginate_wrap(response, &block) click to toggle source
# File lib/groups.rb, line 120
def paginate_wrap(response, &block)
  raise 'an error ocurred when try to communicate with the API' if response.status != 200
  hash = json_parse(response.content)
  PaginatedElements.new(block ? block.call(hash) : hash[:elements], hash[:totalElements], hash[:totalPages], hash[:actualPage])
end
pagination_query(params = { page: nil, per_page: nil }) click to toggle source
# File lib/groups.rb, line 145
def pagination_query(params = { page: nil, per_page: nil })
  return '' unless params[:page] || params[:per_page]
  '?' + [].tap do |section|
    section << "limit=#{params[:per_page]}" if params[:per_page]
    section << "offset=#{params[:page]}"    if params[:page]
  end.join('&')
end
parse_elements(hash) click to toggle source
# File lib/groups.rb, line 137
def parse_elements(hash)
  hash[:elements].map { |e| Group.new(e) }
end
search_accounts_url(term) click to toggle source
# File lib/groups.rb, line 169
def search_accounts_url(term)
  "#{groups_url}/accounts/available?account=#{term}"
end
wrap_response_from(response) click to toggle source
# File lib/groups.rb, line 126
def wrap_response_from(response)
  content = (200..299).include?(response.code) ? group_from(response) : json_parse(response.content)
  Response.new(response.status, content)
end