mxhero-api 0.1.11 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/communication.rb +35 -0
- data/lib/groups.rb +178 -0
- data/lib/mxhero-api.rb +288 -312
- data/lib/urls.rb +19 -0
- data/mxhero-api.gemspec +4 -4
- data/test/fixtures/api/account_properties.yml +12 -13
- data/test/fixtures/api/account_properties_not_found.yml +9 -9
- data/test/fixtures/api/accounts.yml +39 -0
- data/test/fixtures/api/accounts_filtered.yml +57 -42
- data/test/fixtures/api/accounts_from_domain.yml +10 -10
- data/test/fixtures/api/accounts_from_domain_paginated.yml +10 -10
- data/test/fixtures/api/accounts_without_group.yml +75 -0
- data/test/fixtures/api/add_an_inexistent_account_to_group.yml +39 -0
- data/test/fixtures/api/add_and_remove_account.yml +219 -0
- data/test/fixtures/api/all_groups.yml +39 -0
- data/test/fixtures/api/create_rule_for_domain.yml +95 -21
- data/test/fixtures/api/delete_group.yml +75 -0
- data/test/fixtures/api/delete_rule.yml +21 -61
- data/test/fixtures/api/domain_by_id.yml +10 -10
- data/test/fixtures/api/domain_by_id_not_found.yml +9 -9
- data/test/fixtures/api/domain_rule.yml +62 -76
- data/test/fixtures/api/domains.yml +10 -10
- data/test/fixtures/api/remove_account_from_group_twice.yml +150 -0
- data/test/fixtures/api/remove_inexistente_account_from_group.yml +39 -0
- data/test/fixtures/api/rules_for_domain.yml +11 -57
- data/test/fixtures/api/rules_for_domain_by_component.yml +6 -8
- data/test/fixtures/api/save_group.yml +119 -0
- data/test/fixtures/api/update_account_properties.yml +35 -33
- data/test/fixtures/api/update_rule.yml +35 -76
- data/test/helper.rb +1 -1
- data/test/test_groups.rb +104 -0
- data/test/test_mxhero_api.rb +49 -27
- metadata +26 -7
- data/test/fixtures/api/create_rule_alredy_exist.yml +0 -81
- data/test/fixtures/api/create_rule_success.yml +0 -41
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.12
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module MxHero
|
2
|
+
module API
|
3
|
+
module Communication
|
4
|
+
|
5
|
+
# Make a HTTP call
|
6
|
+
# @param method [Symbol] indicate the HTTP verb (:get, :post, :put, etc)
|
7
|
+
# @param url [String]
|
8
|
+
# @param body [String] (default: nil)
|
9
|
+
# @param [Hash] more_options
|
10
|
+
# @option more_options [Boolean] :throw_exception (default: true) throw exception if the response status are between 500 and 600
|
11
|
+
def call(method, url, body = nil, more_options = {})
|
12
|
+
unless @client
|
13
|
+
@client ||= HTTPClient.new
|
14
|
+
end
|
15
|
+
@client.set_auth(url, @username, @password)
|
16
|
+
response = @client.request(method, url, nil, body, headers)
|
17
|
+
unless more_options[:throw_exception] == false
|
18
|
+
raise "An error ocurred when try to communicate with the API\nError: #{response.inspect}" if (500..600).include?(response.status)
|
19
|
+
end
|
20
|
+
response
|
21
|
+
end
|
22
|
+
|
23
|
+
# Default headers
|
24
|
+
def headers
|
25
|
+
@headers ||= { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [Hash]
|
29
|
+
def json_parse(json)
|
30
|
+
JSON.parse(json, symbolize_names: true)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/groups.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative 'communication'
|
3
|
+
require_relative 'urls'
|
4
|
+
|
5
|
+
require 'delegate'
|
6
|
+
|
7
|
+
module MxHero::API
|
8
|
+
|
9
|
+
# A group instance
|
10
|
+
#
|
11
|
+
class Group < Struct.new(:name, :domain, :description)
|
12
|
+
def initialize(elements)
|
13
|
+
elements.each { |prop, value| send("#{prop}=", value) if respond_to?("#{prop}") }
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_json
|
17
|
+
{ name: name, domain: domain, description: description }.to_json
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
class Account < Struct.new(:account, :domain, :created_date, :updated_date, :group)
|
23
|
+
def initialize(elements)
|
24
|
+
elements.each { |prop, value| send("#{prop}=", value) if respond_to?("#{prop}") }
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_json
|
28
|
+
{ account: account, domain: domain, createdDate: created_date, updatedDate: updated_date, group: group }.to_json
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
"account: #{account}, domain: #{domain}, group: #{group}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
class PaginatedElements < SimpleDelegator
|
38
|
+
attr_reader :total_elements, :total_pages, :actual_page
|
39
|
+
def initialize(paginable, total_elements, total_pages, actual_page)
|
40
|
+
super(paginable)
|
41
|
+
@total_elements = total_elements
|
42
|
+
@total_pages = total_pages
|
43
|
+
@actual_page = actual_page
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Groups
|
48
|
+
include Communication
|
49
|
+
include Urls
|
50
|
+
|
51
|
+
attr_reader :domain
|
52
|
+
|
53
|
+
def initialize(domain, config = {})
|
54
|
+
@domain = domain
|
55
|
+
@service_url = config[:api_url]
|
56
|
+
@username = config[:username]
|
57
|
+
@password = config[:password]
|
58
|
+
@verbose = config[:verbose] || false
|
59
|
+
end
|
60
|
+
|
61
|
+
# Retrieve all the groups
|
62
|
+
#
|
63
|
+
# @return [PaginatedElement] that contains an array of Group elements
|
64
|
+
# Basically its an Array with instances of [MxHero::API::Group] with the methods total_elements, total_pages and actual_page
|
65
|
+
def all(limit = nil, offset = nil)
|
66
|
+
response = call(:get, groups_url)
|
67
|
+
paginate_wrap(response) do |hash|
|
68
|
+
hash[:elements].map { |e| Group.new(e) }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Save a new group
|
73
|
+
#
|
74
|
+
# @param group [MxHero::API::Group]
|
75
|
+
#
|
76
|
+
# @return [MxHero::API::Response]
|
77
|
+
def save(group)
|
78
|
+
group.domain = domain
|
79
|
+
wrap_response_from call(:post, groups_url, group.to_json)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Delete the group
|
83
|
+
#
|
84
|
+
# @param group_name [String]
|
85
|
+
#
|
86
|
+
# @return [MxHero::API::Response] with content empty.
|
87
|
+
# In case on error, may be one of the following:
|
88
|
+
# + domain.group.not.found : Inexistent group
|
89
|
+
def delete(group_name)
|
90
|
+
response = call(:delete, group_url(group_name), nil, throw_exception: false)
|
91
|
+
wrap_response_from response
|
92
|
+
end
|
93
|
+
|
94
|
+
# Retrieve all the accounts for one group
|
95
|
+
#
|
96
|
+
# @param group_name [String]
|
97
|
+
#
|
98
|
+
# @return [MxHero::API::PaginatedElements] the list of accounts [MxHero::API::Account]
|
99
|
+
def accounts(group_name)
|
100
|
+
response = call(:get, group_accounts_url(group_name))
|
101
|
+
paginate_wrap response do |hash|
|
102
|
+
hash[:elements].map { |e| Account.new(e) }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Add an account to a group
|
107
|
+
#
|
108
|
+
# @param group_name [String]
|
109
|
+
# @param account_name [String]
|
110
|
+
#
|
111
|
+
# @return [MxHero::API::Response] with content empty.
|
112
|
+
# In case on error, may be one of the following:
|
113
|
+
# + domain.account.not.found : Inexistent account
|
114
|
+
# + domain.group.account.already.has.group : Try to add an account to a group when already is in that
|
115
|
+
def add_account(group_name, account_name)
|
116
|
+
response = call(:post, group_add_accounts_url(group_name, account_name), nil, throw_exception: false)
|
117
|
+
wrap_response_from response
|
118
|
+
end
|
119
|
+
|
120
|
+
# @param group_name [String]
|
121
|
+
# @param account_name [String]
|
122
|
+
#
|
123
|
+
# @return [MxHero::API::Response] with content empty.
|
124
|
+
# In case on error, may be one of the following:
|
125
|
+
# + domain.account.not.found : Inexistent account
|
126
|
+
# + domain.group.account.not.in.group : Try to remove an account that is not in the group
|
127
|
+
#
|
128
|
+
def remove_account(group_name, account_name)
|
129
|
+
response = call(:delete, group_remove_accounts_url(group_name, account_name), nil, throw_exception: false)
|
130
|
+
wrap_response_from response
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def paginate_wrap(response, &block)
|
136
|
+
raise 'an error ocurred when try to communicate with the API' if response.status != 200
|
137
|
+
hash = json_parse(response.content)
|
138
|
+
PaginatedElements.new(block ? block.call(hash) : hash[:elements], hash[:totalElements], hash[:totalPages], hash[:actualPage])
|
139
|
+
end
|
140
|
+
|
141
|
+
def wrap_response_from(response)
|
142
|
+
content = (200..299).include?(response.code) ? group_from(response) : json_parse(response.content)
|
143
|
+
Response.new(response.status, content)
|
144
|
+
end
|
145
|
+
|
146
|
+
def group_from(response)
|
147
|
+
return nil if response.content.nil? || response.content.empty?
|
148
|
+
hash = json_parse response.content
|
149
|
+
Group.new hash
|
150
|
+
end
|
151
|
+
|
152
|
+
def parse_elements(hash)
|
153
|
+
hash[:elements].map { |e| Group.new(e) }
|
154
|
+
end
|
155
|
+
|
156
|
+
def groups_url
|
157
|
+
domain_by_id_url(domain) + 'groups'
|
158
|
+
end
|
159
|
+
|
160
|
+
def group_url(group_name)
|
161
|
+
groups_url + "/#{group_name}"
|
162
|
+
end
|
163
|
+
|
164
|
+
def group_accounts_url(group_name)
|
165
|
+
group_url(group_name) + "/accounts"
|
166
|
+
end
|
167
|
+
|
168
|
+
def group_add_accounts_url(group_name, account_name)
|
169
|
+
group_accounts_url(group_name) + "/#{account_name}/add"
|
170
|
+
end
|
171
|
+
|
172
|
+
def group_remove_accounts_url(group_name, account_name)
|
173
|
+
group_accounts_url(group_name) + "/#{account_name}/remove"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
end
|
data/lib/mxhero-api.rb
CHANGED
@@ -1,347 +1,323 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'httpclient'
|
2
3
|
require 'json'
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
5
|
+
require_relative 'communication'
|
6
|
+
require_relative 'urls'
|
7
|
+
|
8
|
+
module MxHero::API
|
9
|
+
# The class that contains the response information
|
10
|
+
#
|
11
|
+
# The *code* represent the HTTP code of the response.
|
12
|
+
#
|
13
|
+
# The *msg* represent a hash with the response information. Example:
|
14
|
+
#
|
15
|
+
# { status: 500, code: 500,
|
16
|
+
# developerMessage: "rules.already.exists.for.component",
|
17
|
+
# moreInfoUrl: "mailto:support@mxhero.com" }
|
18
|
+
#
|
19
|
+
class Response < Struct.new(:code, :msg)
|
20
|
+
|
21
|
+
# Response is successful? Based in HTTP status code
|
22
|
+
def success?
|
23
|
+
code.to_i == 200 || code.to_i == 201
|
22
24
|
end
|
23
|
-
|
24
|
-
|
25
|
-
# A client to interact with mxhero engine API
|
26
|
-
class Client
|
27
|
-
|
28
|
-
# @param [Hash] config the options of configuration
|
29
|
-
# @option config [String] :api_url The URL to consume the API
|
30
|
-
# @option config [String] :username The username for access the API
|
31
|
-
# @option config [String] :password The password for the user that access the API
|
32
|
-
# @option config [Boolean] :verbose (false) If true puts information about http operations
|
33
|
-
def initialize(config = {})
|
34
|
-
@service_url = config[:api_url]
|
35
|
-
@username = config[:username]
|
36
|
-
@password = config[:password]
|
37
|
-
@verbose = config[:verbose] || false
|
38
|
-
end
|
39
25
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
# @param id [Integer] the rule id
|
44
|
-
#
|
45
|
-
# @return [Hash, nil] the Rule information or nil if not exist
|
46
|
-
#
|
47
|
-
# @raise an exception when the status code isn't 200
|
48
|
-
def domain_rule(domain, id)
|
49
|
-
url = domain_rule_url(domain, id)
|
50
|
-
response = call(:get, url)
|
51
|
-
raise 'an error ocurred when try to communicate with the API' if response.status != 200
|
52
|
-
json_parse(response.content)
|
53
|
-
end
|
54
|
-
|
55
|
-
# Update a rule
|
56
|
-
# @return [MxHero::API::Response] When the rule is update correctly then return MxHero::API::Response object without msg (msg nil)
|
57
|
-
# In case of error, the message is completed with the cause of error
|
58
|
-
def update_rule(rule)
|
59
|
-
url = domain_rule_url(rule[:domain], rule[:id])
|
60
|
-
response = call(:put, url, rule.to_json)
|
61
|
-
parse_response(response)
|
62
|
-
end
|
63
|
-
|
64
|
-
# Retrive all the domains
|
65
|
-
#
|
66
|
-
# TODO: Improve the response. We really need manage pagination here?
|
67
|
-
#
|
68
|
-
# @return [Hash] with the list of domains
|
69
|
-
# * :elements [Array<Hash>] the list of domains as a Hash, when any element contains:
|
70
|
-
# * :domain [String]
|
71
|
-
# * :server [String]
|
72
|
-
# * :creationDate [Fixnum]
|
73
|
-
# * :updateDate [Fixnum]
|
74
|
-
# * :aliases
|
75
|
-
# * :ldap
|
76
|
-
# * :totalElements
|
77
|
-
# * :totalPages
|
78
|
-
# * :actualPage
|
79
|
-
#
|
80
|
-
# @raise an exception when the status code isn't 200
|
81
|
-
def domains
|
82
|
-
response = call(:get, domains_url)
|
83
|
-
raise 'an error ocurred when try to communicate with the API' if response.status != 200
|
84
|
-
response_as_a_hash = json_parse(response.content)
|
85
|
-
response_as_a_hash
|
86
|
-
end
|
87
|
-
|
88
|
-
# Retrive the domain by id according to the parameter if exist
|
89
|
-
#
|
90
|
-
# @param id The domain name or id. For example: 'mydomain.com'
|
91
|
-
# @return [Hash] with the domain attributes if exist
|
92
|
-
# * :domain [String]
|
93
|
-
# * :server [String]
|
94
|
-
# * :creationDate [Fixnum]
|
95
|
-
# * :updateDate [Fixnum]
|
96
|
-
# * :aliases
|
97
|
-
# * :ldap
|
98
|
-
# @raise an exception when the status code is one of the 5xx status error
|
99
|
-
# @raise an exception if @param id is nil
|
100
|
-
def domain_by_id(id = nil)
|
101
|
-
raise 'Id domain could not be nil' if id.nil?
|
102
|
-
response = call(:get, domain_by_id_url(id))
|
103
|
-
response_as_a_hash = json_parse(response.content)
|
104
|
-
response_as_a_hash
|
105
|
-
end
|
26
|
+
alias_method :content, :msg
|
27
|
+
alias_method :content=, :msg=
|
28
|
+
end
|
106
29
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
30
|
+
# A client to interact with mxhero engine API
|
31
|
+
class Client
|
32
|
+
include Communication
|
33
|
+
include Urls
|
34
|
+
|
35
|
+
# @param [Hash] config the options of configuration
|
36
|
+
# @option config [String] :api_url The URL to consume the API
|
37
|
+
# @option config [String] :username The username for access the API
|
38
|
+
# @option config [String] :password The password for the user that access the API
|
39
|
+
# @option config [Boolean] :verbose (false) If true puts information about http operations
|
40
|
+
def initialize(config = {})
|
41
|
+
@service_url = config[:api_url]
|
42
|
+
@username = config[:username]
|
43
|
+
@password = config[:password]
|
44
|
+
@verbose = config[:verbose] || false
|
45
|
+
end
|
111
46
|
|
112
|
-
|
113
|
-
|
114
|
-
|
47
|
+
# Find a rule by domain and ID
|
48
|
+
#
|
49
|
+
# @param domain [String]
|
50
|
+
# @param id [Integer] the rule id
|
51
|
+
#
|
52
|
+
# @return [Hash, nil] the Rule information or nil if not exist
|
53
|
+
#
|
54
|
+
# @raise an exception when the status code isn't 200
|
55
|
+
def domain_rule(domain, id)
|
56
|
+
url = domain_rule_url(domain, id)
|
57
|
+
response = call(:get, url)
|
58
|
+
raise 'an error ocurred when try to communicate with the API' if response.status != 200
|
59
|
+
json_parse(response.content)
|
60
|
+
end
|
115
61
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
# @option msg [Hash] :toDirection
|
125
|
-
# @option msg [Array<Hash>] :properties
|
126
|
-
# @option msg [String] :component
|
127
|
-
#
|
128
|
-
# @return [MxHero::API::Response]
|
129
|
-
def create_rule_for_domain(domain, msg)
|
130
|
-
url = rules_for_domain_url(domain)
|
131
|
-
response = call(:post, url, msg.to_json, throw_exception: false)
|
132
|
-
parse_response(response)
|
133
|
-
end
|
134
|
-
|
135
|
-
# Create a rule
|
136
|
-
#
|
137
|
-
# @param [Hash] msg
|
138
|
-
# @option msg [String] :domain
|
139
|
-
# @option msg [Boolean] :twoWays
|
140
|
-
# @option msg [Boolean] :enabled
|
141
|
-
# @option msg [String] :name
|
142
|
-
# @option msg [Integer] :created in epoch format
|
143
|
-
# @option msg [Hash] :fromDirection
|
144
|
-
# @option msg [Hash] :toDirection
|
145
|
-
# @option msg [Array<Hash>] :properties
|
146
|
-
# @option msg [String] :component
|
147
|
-
#
|
148
|
-
# @return [MxHero::API::Response]
|
149
|
-
def create_rule(msg)
|
150
|
-
response = call(:post, rules_url, msg.to_json, throw_exception: false)
|
151
|
-
parse_response(response)
|
152
|
-
end
|
62
|
+
# Update a rule
|
63
|
+
# @return [MxHero::API::Response] When the rule is update correctly then return MxHero::API::Response object without msg (msg nil)
|
64
|
+
# In case of error, the message is completed with the cause of error
|
65
|
+
def update_rule(rule)
|
66
|
+
url = domain_rule_url(rule[:domain], rule[:id])
|
67
|
+
response = call(:put, url, rule.to_json)
|
68
|
+
parse_response(response)
|
69
|
+
end
|
153
70
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
71
|
+
# Retrive all the domains
|
72
|
+
#
|
73
|
+
# TODO: Improve the response. We really need manage pagination here?
|
74
|
+
#
|
75
|
+
# @return [Hash] with the list of domains
|
76
|
+
# * :elements [Array<Hash>] the list of domains as a Hash, when any element contains:
|
77
|
+
# * :domain [String]
|
78
|
+
# * :server [String]
|
79
|
+
# * :creationDate [Fixnum]
|
80
|
+
# * :updateDate [Fixnum]
|
81
|
+
# * :aliases
|
82
|
+
# * :ldap
|
83
|
+
# * :totalElements
|
84
|
+
# * :totalPages
|
85
|
+
# * :actualPage
|
86
|
+
#
|
87
|
+
# @raise an exception when the status code isn't 200
|
88
|
+
def domains
|
89
|
+
response = call(:get, domains_url)
|
90
|
+
raise 'an error ocurred when try to communicate with the API' if response.status != 200
|
91
|
+
response_as_a_hash = json_parse(response.content)
|
92
|
+
response_as_a_hash
|
93
|
+
end
|
167
94
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
# * :aliases [Array<Hash>] the list of aliases of one account
|
187
|
-
# * :name [String]
|
188
|
-
# * :domain [String]
|
189
|
-
# * :dataSource [String]
|
190
|
-
# * :dataSource [String]
|
191
|
-
# * :totalElements [Fixnum]
|
192
|
-
# * :totalPages [Fixnum]
|
193
|
-
# * :actualPage [Fixnum]
|
194
|
-
#
|
195
|
-
# @raise an exception when the status code isn't 200
|
196
|
-
def accounts_by_domain(domain, refinement = {}) #filter_account = nil, pagination = {})
|
197
|
-
params = refinement.dup
|
198
|
-
filter_account = params.delete(:account)
|
199
|
-
limit, offset = params.values_at(:limit, :offset)
|
95
|
+
# Retrive the domain by id according to the parameter if exist
|
96
|
+
#
|
97
|
+
# @param id The domain name or id. For example: 'mydomain.com'
|
98
|
+
# @return [Hash] with the domain attributes if exist
|
99
|
+
# * :domain [String]
|
100
|
+
# * :server [String]
|
101
|
+
# * :creationDate [Fixnum]
|
102
|
+
# * :updateDate [Fixnum]
|
103
|
+
# * :aliases
|
104
|
+
# * :ldap
|
105
|
+
# @raise an exception when the status code is one of the 5xx status error
|
106
|
+
# @raise an exception if @param id is nil
|
107
|
+
def domain_by_id(id = nil)
|
108
|
+
raise 'Id domain could not be nil' if id.nil?
|
109
|
+
response = call(:get, domain_by_id_url(id))
|
110
|
+
response_as_a_hash = json_parse(response.content)
|
111
|
+
response_as_a_hash
|
112
|
+
end
|
200
113
|
|
114
|
+
#
|
115
|
+
# Retrive all the account from one domain
|
116
|
+
#
|
117
|
+
# @params [String] domain
|
118
|
+
# @params [String] filter_account the account to filter in the list. This can be a part of an account.
|
119
|
+
# @param [Hash] refinement
|
120
|
+
# @option refinement [Fixnum] :limit of elements per page
|
121
|
+
# @option refinement [Fixnum] :offset number of page (start in 1)
|
122
|
+
# @option refinement [String] :account filter accounts that start with this value
|
123
|
+
# @option refinement [Boolean] :without_group filter accounts without group
|
124
|
+
#
|
125
|
+
#
|
126
|
+
# @return [Hash] with the following elements:
|
127
|
+
# * :elements [Array<Hash>] the list of accounts as a Hash, when any element contains:
|
128
|
+
# * :account [String] example: alex
|
129
|
+
# * :domain [String] example: mxhero.com
|
130
|
+
# * :createdDate [Fixnum] example: 1375909565000 (epoch format)
|
131
|
+
# * :updatedDate [Fixnum]
|
132
|
+
# * :group [String]
|
133
|
+
# * :aliases [Array<Hash>] the list of aliases of one account
|
134
|
+
# * :name [String]
|
135
|
+
# * :domain [String]
|
136
|
+
# * :dataSource [String]
|
137
|
+
# * :dataSource [String]
|
138
|
+
# * :totalElements [Fixnum]
|
139
|
+
# * :totalPages [Fixnum]
|
140
|
+
# * :actualPage [Fixnum]
|
141
|
+
#
|
142
|
+
# @raise an exception when the status code isn't 200
|
143
|
+
def accounts_by_domain(domain, refinement = {}) #filter_account = nil, pagination = {})
|
144
|
+
params = refinement.dup
|
145
|
+
filter_account = params.delete(:account)
|
146
|
+
without_group = params.delete(:without_group) || false
|
147
|
+
limit, offset = params.values_at(:limit, :offset)
|
148
|
+
|
149
|
+
if without_group
|
150
|
+
url = accounts_without_group_url(domain, filter_account)
|
151
|
+
else
|
201
152
|
url = paginate accounts_by_domain_url(domain, filter_account), { limit: limit, offset: offset }
|
202
|
-
response = call(:get, url)
|
203
|
-
json_parse(response.content)
|
204
|
-
end
|
205
|
-
|
206
|
-
|
207
|
-
# @return [Boolean] true when operation it's ok
|
208
|
-
def delete_rule(domain, id)
|
209
|
-
url = domain_rule_url(domain, id)
|
210
|
-
response = call(:delete, url)
|
211
|
-
return true if response.status == 200
|
212
|
-
return false
|
213
153
|
end
|
154
|
+
response = call(:get, url)
|
155
|
+
json_parse(response.content)
|
156
|
+
end
|
214
157
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
#
|
223
|
-
def account_properties(domain, account)
|
224
|
-
url = account_properties_url(domain, account)
|
225
|
-
response = call(:get, url)
|
226
|
-
if response.status == 200
|
227
|
-
props = {}
|
228
|
-
json_parse(response.content).each { |property| props[property[:name]] = property[:value] }
|
229
|
-
return Response.new(response.code, props)
|
230
|
-
end
|
231
|
-
parse_response(response)
|
232
|
-
end
|
158
|
+
def accounts_without_group_url(domain, filter_account)
|
159
|
+
domain_by_id_url(domain) + "/groups/accounts/available?account=#{filter_account}"
|
160
|
+
end
|
161
|
+
|
162
|
+
def verbose?
|
163
|
+
@verbose
|
164
|
+
end
|
233
165
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
#
|
238
|
-
# @return [MxHero::API::Response]. On success not return msg.
|
239
|
-
def update_account_properties(domain, account, properties)
|
240
|
-
url = account_properties_url(domain, account)
|
241
|
-
response = call(:put, url, account_properties_to_json(properties))
|
242
|
-
parse_response(response)
|
243
|
-
end
|
166
|
+
def verbose=(verbose)
|
167
|
+
@verbose = verbose
|
168
|
+
end
|
244
169
|
|
245
|
-
|
246
|
-
|
170
|
+
# @param domain [String]
|
171
|
+
# @param [Hash] msg
|
172
|
+
# @option msg [String] :domain
|
173
|
+
# @option msg [Boolean] :twoWays
|
174
|
+
# @option msg [Boolean] :enabled
|
175
|
+
# @option msg [String] :name
|
176
|
+
# @option msg [Integer] :created in epoch format
|
177
|
+
# @option msg [Hash] :fromDirection
|
178
|
+
# @option msg [Hash] :toDirection
|
179
|
+
# @option msg [Array<Hash>] :properties
|
180
|
+
# @option msg [String] :component
|
181
|
+
#
|
182
|
+
# @return [MxHero::API::Response]
|
183
|
+
def create_rule_for_domain(domain, msg)
|
184
|
+
url = rules_for_domain_url(domain)
|
185
|
+
response = call(:post, url, msg.to_json, throw_exception: false)
|
186
|
+
parse_response(response)
|
187
|
+
end
|
188
|
+
|
189
|
+
## Create a rule
|
190
|
+
##
|
191
|
+
## @param [Hash] msg
|
192
|
+
## @option msg [String] :domain
|
193
|
+
## @option msg [Boolean] :twoWays
|
194
|
+
## @option msg [Boolean] :enabled
|
195
|
+
## @option msg [String] :name
|
196
|
+
## @option msg [Integer] :created in epoch format
|
197
|
+
## @option msg [Hash] :fromDirection
|
198
|
+
## @option msg [Hash] :toDirection
|
199
|
+
## @option msg [Array<Hash>] :properties
|
200
|
+
## @option msg [String] :component
|
201
|
+
##
|
202
|
+
## [MxHero::API::Response]
|
203
|
+
#def create_rule(msg)
|
204
|
+
# response = call(:post, rules_url, msg.to_json, throw_exception: false)
|
205
|
+
# parse_response(response)
|
206
|
+
#end
|
207
|
+
|
208
|
+
# Retrieve all the rules for one domain
|
209
|
+
#
|
210
|
+
# @param [String] domain
|
211
|
+
# @param [String] component Filter the list of rules by this component [optional]
|
212
|
+
#
|
213
|
+
# @return [MxHero::API::Response] reponse
|
214
|
+
# the key :msg contains an array of rules for the domain.
|
215
|
+
def rules_for_domain(domain, component = nil)
|
216
|
+
url = rules_for_domain_url(domain)
|
217
|
+
url << "?component=#{component}" if component
|
218
|
+
response = call(:get, url)
|
219
|
+
parse_response(response, on_empty: [])
|
220
|
+
end
|
247
221
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
# Ex.: url: http://www.example.com/api/accounts
|
256
|
-
# pagination: { limit: 4, offset: 2 }
|
257
|
-
# return > http://www.example.com/api/accounts?limit=4&offset=2
|
258
|
-
def paginate(url, pagination)
|
259
|
-
paginated = url.dup
|
260
|
-
connector = url.include?('?') ? '&' : '?'
|
261
|
-
[ :limit, :offset ].map do |elem|
|
262
|
-
if pagination[elem]
|
263
|
-
paginated << "#{connector}#{elem.to_s}=#{pagination[elem]}"
|
264
|
-
connector = '&'
|
265
|
-
end
|
266
|
-
end
|
267
|
-
paginated
|
268
|
-
end
|
222
|
+
# @return [Boolean] true when operation it's ok
|
223
|
+
def delete_rule(domain, id)
|
224
|
+
url = domain_rule_url(domain, id)
|
225
|
+
response = call(:delete, url)
|
226
|
+
return true if response.status == 200
|
227
|
+
return false
|
228
|
+
end
|
269
229
|
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
230
|
+
# @param [String] domain
|
231
|
+
# @param [String] account Ex.: test or mxhero (the user name)
|
232
|
+
# @return [MxHero::API::Response] reponse
|
233
|
+
# the key :msg contains a Hash with the key, value of any property.
|
234
|
+
# Example:
|
235
|
+
#
|
236
|
+
# { 'email': 'test@mxhero.com', 'name': 'John', 'lastname': 'Doe', ... }
|
237
|
+
#
|
238
|
+
def account_properties(domain, account)
|
239
|
+
url = account_properties_url(domain, account)
|
240
|
+
response = call(:get, url)
|
241
|
+
if response.status == 200
|
242
|
+
props = {}
|
243
|
+
json_parse(response.content).each { |property| props[property[:name]] = property[:value] }
|
244
|
+
return Response.new(response.code, props)
|
276
245
|
end
|
246
|
+
parse_response(response)
|
247
|
+
end
|
277
248
|
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
249
|
+
# @param [String] domain
|
250
|
+
# @param [String] account
|
251
|
+
# @param [Hash] properties The properties of the account. Ex.: { 'email': 'test@mxhero.com', 'name': 'John', 'lastname': 'Doe', ... }
|
252
|
+
#
|
253
|
+
# @return [MxHero::API::Response]. On success not return msg.
|
254
|
+
def update_account_properties(domain, account, properties)
|
255
|
+
url = account_properties_url(domain, account)
|
256
|
+
response = call(:put, url, account_properties_to_json(properties))
|
257
|
+
parse_response(response)
|
258
|
+
end
|
284
259
|
|
285
|
-
|
286
|
-
|
287
|
-
JSON.parse(json, symbolize_names: true)
|
288
|
-
end
|
260
|
+
private
|
261
|
+
|
289
262
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
263
|
+
# Complete the URL with the pagination info (:limit and :offset)
|
264
|
+
# @param [String] original URL. Ex.: http://www.example.com/api/accounts
|
265
|
+
# @param [Hash] pagination
|
266
|
+
# @option pagination [Fixnum] :limit of elements per page
|
267
|
+
# @option pagination [Fixnum] :offset number of page (start in 1)
|
268
|
+
#
|
269
|
+
# @return [String] the URL with the pagination parameters.
|
270
|
+
# Ex.: url: http://www.example.com/api/accounts
|
271
|
+
# pagination: { limit: 4, offset: 2 }
|
272
|
+
# return > http://www.example.com/api/accounts?limit=4&offset=2
|
273
|
+
def paginate(url, pagination)
|
274
|
+
paginated = url.dup
|
275
|
+
connector = url.include?('?') ? '&' : '?'
|
276
|
+
[ :limit, :offset ].map do |elem|
|
277
|
+
if pagination.key? elem
|
278
|
+
paginated << "#{connector}#{elem.to_s}=#{pagination[elem]}"
|
279
|
+
connector = '&'
|
302
280
|
end
|
303
|
-
|
304
|
-
response
|
305
|
-
end
|
306
|
-
|
307
|
-
# Default headers
|
308
|
-
def headers
|
309
|
-
@headers ||= { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
|
310
|
-
end
|
311
|
-
|
312
|
-
def rules_for_domain_url(domain)
|
313
|
-
service_url + "/domains/#{domain}/rules"
|
314
|
-
end
|
315
|
-
|
316
|
-
def rules_url
|
317
|
-
service_url + '/rules'
|
318
281
|
end
|
282
|
+
paginated
|
283
|
+
end
|
319
284
|
|
320
|
-
|
321
|
-
|
285
|
+
def account_properties_to_json(properties)
|
286
|
+
out = []
|
287
|
+
properties.each do |key, value|
|
288
|
+
out << { name: key, value: value }
|
322
289
|
end
|
290
|
+
out.to_json
|
291
|
+
end
|
323
292
|
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
293
|
+
# @return [MxHero::API::Response] a response object
|
294
|
+
def parse_response(response, opts = { on_empty: nil })
|
295
|
+
json = response.content
|
296
|
+
hash = json.nil? || json.empty? ? opts[:on_empty] : json_parse(json)
|
297
|
+
Response.new(response.code, hash)
|
298
|
+
end
|
299
|
+
|
300
|
+
def rules_for_domain_url(domain)
|
301
|
+
service_url + "/domains/#{domain}/rules"
|
302
|
+
end
|
328
303
|
|
329
|
-
|
330
|
-
|
331
|
-
|
304
|
+
def rules_url
|
305
|
+
service_url + '/rules'
|
306
|
+
end
|
332
307
|
|
333
|
-
|
334
|
-
|
335
|
-
|
308
|
+
def accounts_by_domain_url(domain, filter_account = nil)
|
309
|
+
filter = filter_account ? "?account=#{filter_account}" : ''
|
310
|
+
domain_by_id_url(domain) + 'accounts' + filter
|
311
|
+
end
|
336
312
|
|
337
|
-
|
338
|
-
|
339
|
-
|
313
|
+
def domain_rule_url(domain, id)
|
314
|
+
rules_for_domain_url(domain) + "/#{id}"
|
315
|
+
end
|
340
316
|
|
341
|
-
|
342
|
-
|
343
|
-
end
|
317
|
+
def account_properties_url(domain, account)
|
318
|
+
accounts_by_domain_url(domain) + "/#{account}/properties"
|
344
319
|
end
|
345
320
|
|
346
321
|
end
|
322
|
+
|
347
323
|
end
|