mautic_api 1.0.0

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +32 -0
  4. data/lib/mautic_api.rb +41 -0
  5. data/lib/mautic_api/api/api.rb +248 -0
  6. data/lib/mautic_api/api/companies.rb +11 -0
  7. data/lib/mautic_api/api/contacts.rb +96 -0
  8. data/lib/mautic_api/api/segments.rb +33 -0
  9. data/lib/mautic_api/auth/abstract_auth.rb +258 -0
  10. data/lib/mautic_api/auth/api_auth.rb +57 -0
  11. data/lib/mautic_api/auth/auth.rb +6 -0
  12. data/lib/mautic_api/auth/auth_interface.rb +29 -0
  13. data/lib/mautic_api/auth/oauth2.rb +62 -0
  14. data/lib/mautic_api/engine.rb +10 -0
  15. data/lib/mautic_api/exception/context_not_found_exception.rb +14 -0
  16. data/lib/mautic_api/version.rb +3 -0
  17. data/lib/tasks/mautic_api_tasks.rake +4 -0
  18. data/test/dummy/README.rdoc +28 -0
  19. data/test/dummy/Rakefile +6 -0
  20. data/test/dummy/app/assets/javascripts/application.js +13 -0
  21. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  22. data/test/dummy/app/controllers/application_controller.rb +5 -0
  23. data/test/dummy/app/helpers/application_helper.rb +2 -0
  24. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/test/dummy/bin/bundle +3 -0
  26. data/test/dummy/bin/rails +4 -0
  27. data/test/dummy/bin/rake +4 -0
  28. data/test/dummy/config.ru +4 -0
  29. data/test/dummy/config/application.rb +23 -0
  30. data/test/dummy/config/boot.rb +5 -0
  31. data/test/dummy/config/database.yml +25 -0
  32. data/test/dummy/config/environment.rb +5 -0
  33. data/test/dummy/config/environments/development.rb +37 -0
  34. data/test/dummy/config/environments/production.rb +78 -0
  35. data/test/dummy/config/environments/test.rb +39 -0
  36. data/test/dummy/config/initializers/assets.rb +8 -0
  37. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  38. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  39. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  40. data/test/dummy/config/initializers/inflections.rb +16 -0
  41. data/test/dummy/config/initializers/mime_types.rb +4 -0
  42. data/test/dummy/config/initializers/session_store.rb +3 -0
  43. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  44. data/test/dummy/config/locales/en.yml +23 -0
  45. data/test/dummy/config/routes.rb +56 -0
  46. data/test/dummy/config/secrets.yml +22 -0
  47. data/test/dummy/public/404.html +67 -0
  48. data/test/dummy/public/422.html +67 -0
  49. data/test/dummy/public/500.html +66 -0
  50. data/test/dummy/public/favicon.ico +0 -0
  51. data/test/mautic_api_test.rb +7 -0
  52. data/test/test_helper.rb +18 -0
  53. metadata +130 -0
@@ -0,0 +1,33 @@
1
+ module MauticApi
2
+
3
+ class Segments < Api
4
+
5
+ ENDPOINT = "segments"
6
+ LIST_NAME = "segments"
7
+ ITEM_NAME = "segment"
8
+
9
+ # Add a contact to the segment
10
+ #
11
+ # @param int $id Segment ID
12
+ # @param int $contactId Contact ID
13
+ #
14
+ # @return array|mixed
15
+
16
+ def add_contact id, contact_id
17
+ return make_request("#{self.endpoint}/#{id}/contact/add/#{contact_id}", {}, :post)
18
+ end
19
+
20
+ # Remove a contact from the segment
21
+ #
22
+ # @param int $id Segment ID
23
+ # @param int $contactId Contact ID
24
+ #
25
+ # @return array|mixed
26
+
27
+ def remove_contact id, contact_id
28
+ return make_request("#{self.endpoint}/#{id}/contact/remove/#{contact_id}", {}, :post)
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,258 @@
1
+
2
+ # @copyright 2017 Mautic Contributors. All rights reserved
3
+ # @author Mautic, Inc.
4
+ #
5
+ # @link https://mautic.org
6
+ #
7
+ # @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
8
+
9
+ module MauticApi::Auth
10
+
11
+ # use Mautic\Exception\UnexpectedResponseFormatException
12
+ # use Mautic\Response
13
+
14
+ class AbstractAuth < AuthInterface
15
+
16
+ # # Holds string of HTTP response headers
17
+ # #
18
+ # # @var string
19
+
20
+ # attr_protected :_http_response_headers
21
+
22
+ # # Holds array of HTTP response CURL info
23
+ # #
24
+ # # @var array
25
+
26
+ # attr_protected :_http_response_info
27
+
28
+ # # Timeout for a cURL request
29
+ # #
30
+ # # @var int
31
+
32
+ # attr_protected :_curl_timeout = null
33
+
34
+
35
+ # # Returns array of HTTP response headers
36
+ # #
37
+ # # @return array
38
+
39
+ # def get_response_headers()
40
+ # {
41
+ # return self.parse_headers(self._http_response_headers)
42
+ # }
43
+
44
+ # # Returns array of HTTP response headers
45
+ # #
46
+ # # @return array
47
+
48
+ # def get_response_info()
49
+ # {
50
+ # return self._http_response_info
51
+ # }
52
+
53
+ # # {@inheritdoc}
54
+ # #
55
+ # # @throws UnexpectedResponseFormatException|Exception
56
+
57
+ # def makeRequest url, parameters = [], method = :get, settings = []
58
+
59
+ # # self.log('makeRequest('.$url.', '.http_build_query($parameters).', '.$method.',...)')
60
+ # url, parameters = self.separate_url_params(url, parameters)
61
+
62
+ # # make sure method is capitalized for congruency
63
+ # method = method.underscore
64
+
65
+ # headers = settings['headers'] || {}
66
+ # headers, parameters = self.prepare_request url, headers, parameters, method, settings
67
+
68
+ # # Set default CURL options
69
+ # options = {
70
+ # CURLOPT_RETURNTRANSFER: true,
71
+ # CURLOPT_SSL_VERIFYPEER: false,
72
+ # CURLOPT_HEADER: true
73
+ # }
74
+
75
+
76
+ # if self._curl_timeout != nil
77
+ # $options[CURLOPT_TIMEOUT] = self._curlTimeout
78
+
79
+ # // CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set
80
+ # $options[CURLOPT_FOLLOWLOCATION] = (ini_get('open_basedir')) ? false : true
81
+ # //Set custom REST method if not GET or POST
82
+ # if (!in_array($method, array('GET', 'POST'))) {
83
+ # $options[CURLOPT_CUSTOMREQUEST] = $method
84
+ # }
85
+ # //Set post fields for POST/PUT/PATCH requests
86
+ # $isPost = false
87
+ # if (in_array($method, array('POST', 'PUT', 'PATCH'))) {
88
+ # $isPost = true
89
+ # // Set file to upload
90
+ # // Sending file data requires an array to set
91
+ # // the Content-Type header to multipart/form-data
92
+ # if (!empty($parameters['file']) && file_exists($parameters['file'])) {
93
+ # $options[CURLOPT_INFILESIZE] = filesize($parameters['file'])
94
+ # $parameters['file'] = self.createCurlFile($parameters['file'])
95
+ # $headers[] = "Content-Type: multipart/form-data"
96
+ # } else {
97
+ # $parameters = http_build_query($parameters, '', '&')
98
+ # }
99
+ # $options[CURLOPT_POST] = true
100
+ # $options[CURLOPT_POSTFIELDS] = $parameters
101
+ # self.log('Posted parameters = '.print_r($parameters, true))
102
+ # }
103
+ # $query = self.getQueryParameters($isPost, $parameters)
104
+ # self.log('Query parameters = '.print_r($query, true))
105
+ # //Create a query string for GET/DELETE requests
106
+ # if (count($query) > 0) {
107
+ # $queryGlue = strpos($url, '?') === false ? '?' : '&'
108
+ # $url = $url.$queryGlue.http_build_query($query, '', '&')
109
+ # self.log('URL updated to '.$url)
110
+ # }
111
+ # // Set the URL
112
+ # $options[CURLOPT_URL] = $url
113
+ # $headers[] = 'Accept: application/json'
114
+ # $options[CURLOPT_HTTPHEADER] = $headers
115
+ # //Make CURL request
116
+ # $curl = curl_init()
117
+ # curl_setopt_array($curl, $options)
118
+ # $response = new Response(curl_exec($curl), curl_getinfo($curl))
119
+ # self._httpResponseHeaders = $response->getHeaders()
120
+ # self._httpResponseInfo = $response->getInfo()
121
+ # curl_close($curl)
122
+ # if (self._debug) {
123
+ # $_SESSION['oauth']['debug']['info'] = $response->getInfo()
124
+ # $_SESSION['oauth']['debug']['returnedHeaders'] = $response->getHeaders()
125
+ # $_SESSION['oauth']['debug']['returnedBody'] = $response->getBody()
126
+ # }
127
+ # // Handle zip file response
128
+ # if ($response->isZip()) {
129
+ # $temporaryFilePath = isset($settings['temporaryFilePath']) ? $settings['temporaryFilePath'] : sys_get_temp_dir()
130
+ # return $response->saveToFile($temporaryFilePath)
131
+ # } else {
132
+ # return $response->getDecodedBody()
133
+ # }
134
+ # }
135
+
136
+
137
+ # protected
138
+
139
+ # # @param $url
140
+ # # @param array $headers
141
+ # # @param array $parameters
142
+ # # @param $method
143
+ # # @param array $settings
144
+ # #
145
+ # # @return mixed
146
+
147
+ # def prepare_request url, headers, parameters, method, settings
148
+
149
+ # end
150
+
151
+
152
+ # # @deprecated 2.6.0 to be removed in 3.0; use createCurlFile instead
153
+ # #
154
+ # # @param $filename
155
+ # # @param string $mimetype
156
+ # # @param string $postname
157
+ # #
158
+ # # @return \CURLFile|string
159
+
160
+ # protected function crateCurlFile($filename, $mimetype = '', $postname = '')
161
+ # {
162
+ # return self.createCurlFile($filename, $mimetype, $postname)
163
+ # }
164
+
165
+ # # Build the CURL file based on PHP version
166
+ # #
167
+ # # @param string $filename
168
+ # # @param string $mimetype
169
+ # # @param string $postname
170
+ # #
171
+ # # @return string|\CURLFile
172
+
173
+ # protected function createCurlFile($filename, $mimetype = '', $postname = '')
174
+ # {
175
+ # if (!function_exists('curl_file_create')) {
176
+ # // For PHP < 5.5
177
+ # return "@$filename;filename="
178
+ # .($postname ?: basename($filename))
179
+ # .($mimetype ? ";type=$mimetype" : '')
180
+ # }
181
+ # // For PHP >= 5.5
182
+ # return curl_file_create($filename, $mimetype, $postname)
183
+ # }
184
+
185
+ # # @param $isPost
186
+ # # @param $parameters
187
+ # #
188
+ # # @return array
189
+
190
+ # protected function getQueryParameters($isPost, $parameters)
191
+ # {
192
+ # return ($isPost) ? array() : $parameters
193
+ # }
194
+
195
+ # # @param string $message
196
+
197
+ # protected function log($message)
198
+ # {
199
+ # if (self._debug) {
200
+ # $_SESSION['oauth']['debug']['flow'][date('m-d H:i:s')][] = $message
201
+ # }
202
+ # }
203
+
204
+ # # Build the HTTP response array out of the headers string
205
+ # #
206
+ # # @param string $headersStr
207
+ # #
208
+ # # @return array
209
+
210
+ # protected function parseHeaders($headersStr)
211
+ # {
212
+ # $headersArr = array()
213
+ # $headersHlpr = explode("\r\n", $headersStr)
214
+ # foreach ($headersHlpr as $header) {
215
+ # $pos = strpos($header, ':')
216
+ # if ($pos === false) {
217
+ # $headersArr[] = trim($header)
218
+ # } else {
219
+ # $headersArr[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1)))
220
+ # }
221
+ # }
222
+ # return $headersArr
223
+ # }
224
+
225
+ # # Separates parameters from base URL
226
+ # #
227
+ # # @param $url
228
+ # # @param $params
229
+ # #
230
+ # # @return array
231
+
232
+ # protected function separateUrlParams($url, $params)
233
+ # {
234
+ # $a = parse_url($url)
235
+ # if (!empty($a['query'])) {
236
+ # parse_str($a['query'], $qparts)
237
+ # $cleanParams = array()
238
+ # foreach ($qparts as $k => $v) {
239
+ # $cleanParams[$k] = $v ? $v : ''
240
+ # }
241
+ # $params = array_merge($params, $cleanParams)
242
+ # $urlParts = explode('?', $url, 2)
243
+ # $url = $urlParts[0]
244
+ # }
245
+ # return array($url, $params)
246
+ # }
247
+
248
+ # # Set the timeout for a cURL request
249
+ # #
250
+ # # @param int $timeout
251
+
252
+ # public function setCurlTimeout($timeout)
253
+ # {
254
+ # self._curlTimeout = $timeout
255
+ # }
256
+
257
+ end
258
+ end
@@ -0,0 +1,57 @@
1
+
2
+
3
+ # @copyright 2014 Mautic Contributors. All rights reserved
4
+ # @author Mautic, Inc.
5
+ #
6
+ # @link https://mautic.org
7
+ #
8
+ # @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
9
+
10
+ module MauticApi::Auth
11
+
12
+ # # OAuth Client modified from https://code.google.com/p/simple-php-oauth/
13
+
14
+ class ApiAuth
15
+
16
+ # # Get an API Auth object
17
+ # #
18
+ # # @param array $parameters
19
+ # # @param string auth_method
20
+ # #
21
+ # # @return AuthInterface
22
+ # #
23
+ # # @deprecated
24
+
25
+ # def self.initialize parameters = [], auth_method = :o_auth
26
+ # object = self.new
27
+ # return object.new_auth parameters, auth_method
28
+ # end
29
+
30
+ # # Get an API Auth object
31
+ # #
32
+ # # @param array $parameters
33
+ # # @param string auth_method
34
+ # #
35
+ # # @return AuthInterface
36
+
37
+ # def new_auth parameters = {}, auth_method = :o_auth
38
+
39
+ # klass = "MauticApi::Auth::#{auth_method.camelize}".constantize
40
+ # auth_object = klass.new
41
+ # auth_setup = auth_object.method(:setup)
42
+ # pass = []
43
+
44
+ # auth_setup.parameters.each do |method, param|
45
+ # if parameters[param.to_sym]
46
+ # pass << parameters[param.to_sym]
47
+ # else
48
+ # pass << nil
49
+ # end
50
+ # end
51
+
52
+ # auth_setup.call(*pass)
53
+
54
+ # return auth_object
55
+ # end
56
+ end
57
+ end
@@ -0,0 +1,6 @@
1
+ module MauticApi
2
+ module Auth
3
+ autoload :AuthInterface, 'mautic_api/auth/auth_interface'
4
+ autoload :OAuth2, 'mautic_api/auth/oauth2'
5
+ end
6
+ end
@@ -0,0 +1,29 @@
1
+
2
+ module MauticApi::Auth
3
+
4
+ # OAuth Client modified from https://code.google.com/p/simple-php-oauth/
5
+
6
+ class AuthInterface
7
+
8
+ # Check if current authorization is still valid
9
+ #
10
+ # @return bool
11
+
12
+ def is_authorized
13
+ raise 'not implemented'
14
+ end
15
+
16
+ # Make a request to server using the supported auth method
17
+ #
18
+ # @param string $url
19
+ # @param array $parameters
20
+ # @param string $method
21
+ # @param array $settings
22
+ #
23
+ # @return array
24
+
25
+ def make_request url, parameters={}, method=:get, settings={}
26
+ raise 'not implemented'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,62 @@
1
+
2
+ # @copyright 2014 Mautic Contributors. All rights reserved
3
+ # @author Mautic, Inc.
4
+ #
5
+ # @link https://mautic.org
6
+ #
7
+ # @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
8
+
9
+ module MauticApi::Auth
10
+
11
+ class OAuth2 < AuthInterface
12
+
13
+ attr_accessor :access_token, :response_info
14
+
15
+ def initialize access_token, parameters={}
16
+ self.access_token = access_token
17
+ end
18
+
19
+ def is_authorized
20
+ #Check for existing access token
21
+ return self.access_token && !self.access_token.expired?
22
+ end
23
+
24
+ def get_response_info
25
+ self.response_info
26
+ end
27
+
28
+ # Make a request to server using oauth2
29
+ #
30
+ # @param string $url
31
+ # @param array $parameters
32
+ # @param string $method
33
+ # @param array $settings
34
+ #
35
+ # @return array
36
+
37
+ def make_request url, parameters = {}, method = :get, settings = {}, &block
38
+
39
+ case method
40
+ when :get
41
+ self.response_info = self.access_token.get(url, :params => parameters, &block)
42
+ when :post
43
+ self.response_info = self.access_token.post(url, :body => parameters, &block)
44
+ when :put
45
+ self.response_info = self.access_token.put(url, :body => parameters, &block)
46
+ when :patch
47
+ self.response_info = self.access_token.patch(url, :body => parameters, &block)
48
+ when :delete
49
+ self.response_info = self.access_token.delete(url, :body => parameters, &block)
50
+ end
51
+
52
+ return {
53
+ url: url,
54
+ method: method,
55
+ parameters: parameters,
56
+ settings: settings,
57
+ code: self.response_info.status,
58
+ body: self.response_info.parsed
59
+ }
60
+ end
61
+ end
62
+ end