class MxHero::API::Groups
Attributes
Public Class Methods
# 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
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 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
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 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
@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 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 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
# File lib/groups.rb, line 157 def group_accounts_url(group_name, pagination = {}) group_url(group_name) + "/accounts" + pagination_query(pagination) end
# File lib/groups.rb, line 161 def group_add_accounts_url(group_name, account_name) group_accounts_url(group_name) + "/#{account_name}/add" end
# 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
# File lib/groups.rb, line 165 def group_remove_accounts_url(group_name, account_name) group_accounts_url(group_name) + "/#{account_name}/remove" end
# File lib/groups.rb, line 153 def group_url(group_name) groups_url + "/#{URI::encode(group_name)}" end
# File lib/groups.rb, line 141 def groups_url(pagination = {}) domain_by_id_url(domain) + '/groups' + pagination_query(pagination) end
# 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
# 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
# File lib/groups.rb, line 137 def parse_elements(hash) hash[:elements].map { |e| Group.new(e) } end
# File lib/groups.rb, line 169 def search_accounts_url(term) "#{groups_url}/accounts/available?account=#{term}" end
# 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