virtengine_api 0.1.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 (151) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE +21 -0
  6. data/README.md +27 -0
  7. data/Rakefile +10 -0
  8. data/lib/megam/api.rb +329 -0
  9. data/lib/megam/api/accounts.rb +96 -0
  10. data/lib/megam/api/addons.rb +27 -0
  11. data/lib/megam/api/assemblies.rb +49 -0
  12. data/lib/megam/api/assembly.rb +56 -0
  13. data/lib/megam/api/backups.rb +57 -0
  14. data/lib/megam/api/balances.rb +45 -0
  15. data/lib/megam/api/billedhistories.rb +35 -0
  16. data/lib/megam/api/billingtransactions.rb +25 -0
  17. data/lib/megam/api/components.rb +24 -0
  18. data/lib/megam/api/credits.rb +34 -0
  19. data/lib/megam/api/disks.rb +44 -0
  20. data/lib/megam/api/domains.rb +25 -0
  21. data/lib/megam/api/errors.rb +29 -0
  22. data/lib/megam/api/eventsall.rb +36 -0
  23. data/lib/megam/api/eventsbilling.rb +35 -0
  24. data/lib/megam/api/eventscontainer.rb +35 -0
  25. data/lib/megam/api/eventsmarketplace.rb +35 -0
  26. data/lib/megam/api/eventsstorage.rb +24 -0
  27. data/lib/megam/api/eventsvm.rb +35 -0
  28. data/lib/megam/api/flavors.rb +56 -0
  29. data/lib/megam/api/license.rb +26 -0
  30. data/lib/megam/api/marketplaces.rb +68 -0
  31. data/lib/megam/api/organizations.rb +50 -0
  32. data/lib/megam/api/promos.rb +18 -0
  33. data/lib/megam/api/quotas.rb +47 -0
  34. data/lib/megam/api/rawimages.rb +36 -0
  35. data/lib/megam/api/reports.rb +24 -0
  36. data/lib/megam/api/requests.rb +36 -0
  37. data/lib/megam/api/sensors.rb +35 -0
  38. data/lib/megam/api/snapshots.rb +46 -0
  39. data/lib/megam/api/sshkeys.rb +35 -0
  40. data/lib/megam/api/subscriptions.rb +26 -0
  41. data/lib/megam/api/version.rb +6 -0
  42. data/lib/megam/core/account.rb +245 -0
  43. data/lib/megam/core/account_collection.rb +122 -0
  44. data/lib/megam/core/addons.rb +169 -0
  45. data/lib/megam/core/addons_collection.rb +121 -0
  46. data/lib/megam/core/assemblies.rb +191 -0
  47. data/lib/megam/core/assemblies_collection.rb +122 -0
  48. data/lib/megam/core/assembly.rb +226 -0
  49. data/lib/megam/core/assembly_collection.rb +122 -0
  50. data/lib/megam/core/backups.rb +259 -0
  51. data/lib/megam/core/backups_collection.rb +122 -0
  52. data/lib/megam/core/balances.rb +187 -0
  53. data/lib/megam/core/balances_collection.rb +121 -0
  54. data/lib/megam/core/billedhistories.rb +190 -0
  55. data/lib/megam/core/billedhistories_collection.rb +121 -0
  56. data/lib/megam/core/billingtransactions.rb +214 -0
  57. data/lib/megam/core/billingtransactions_collection.rb +122 -0
  58. data/lib/megam/core/components.rb +326 -0
  59. data/lib/megam/core/components_collection.rb +123 -0
  60. data/lib/megam/core/credits.rb +146 -0
  61. data/lib/megam/core/credits_collection.rb +121 -0
  62. data/lib/megam/core/disks.rb +199 -0
  63. data/lib/megam/core/disks_collection.rb +122 -0
  64. data/lib/megam/core/domain_collection.rb +122 -0
  65. data/lib/megam/core/domains.rb +118 -0
  66. data/lib/megam/core/error.rb +70 -0
  67. data/lib/megam/core/eventsall.rb +186 -0
  68. data/lib/megam/core/eventsall_collection.rb +122 -0
  69. data/lib/megam/core/eventsbilling.rb +174 -0
  70. data/lib/megam/core/eventsbilling_collection.rb +122 -0
  71. data/lib/megam/core/eventscontainer.rb +187 -0
  72. data/lib/megam/core/eventscontainer_collection.rb +122 -0
  73. data/lib/megam/core/eventsmarketplace.rb +187 -0
  74. data/lib/megam/core/eventsmarketplace_collection.rb +122 -0
  75. data/lib/megam/core/eventsstorage.rb +156 -0
  76. data/lib/megam/core/eventsstorage_collection.rb +122 -0
  77. data/lib/megam/core/eventsvm.rb +187 -0
  78. data/lib/megam/core/eventsvm_collection.rb +122 -0
  79. data/lib/megam/core/flavors.rb +294 -0
  80. data/lib/megam/core/flavors_collection.rb +121 -0
  81. data/lib/megam/core/json_compat.rb +289 -0
  82. data/lib/megam/core/konipai.rb +42 -0
  83. data/lib/megam/core/license.rb +101 -0
  84. data/lib/megam/core/license_collection.rb +122 -0
  85. data/lib/megam/core/log.rb +19 -0
  86. data/lib/megam/core/marketplace.rb +308 -0
  87. data/lib/megam/core/marketplace_collection.rb +123 -0
  88. data/lib/megam/core/monologger.rb +89 -0
  89. data/lib/megam/core/organizations.rb +142 -0
  90. data/lib/megam/core/organizations_collection.rb +121 -0
  91. data/lib/megam/core/promos.rb +145 -0
  92. data/lib/megam/core/quotas.rb +215 -0
  93. data/lib/megam/core/quotas_collection.rb +121 -0
  94. data/lib/megam/core/rawimages.rb +194 -0
  95. data/lib/megam/core/rawimages_collection.rb +121 -0
  96. data/lib/megam/core/reports.rb +176 -0
  97. data/lib/megam/core/reports_collection.rb +121 -0
  98. data/lib/megam/core/request.rb +197 -0
  99. data/lib/megam/core/request_collection.rb +121 -0
  100. data/lib/megam/core/rest_adapter.rb +44 -0
  101. data/lib/megam/core/sensors.rb +141 -0
  102. data/lib/megam/core/sensors_collection.rb +121 -0
  103. data/lib/megam/core/snapshots.rb +268 -0
  104. data/lib/megam/core/snapshots_collection.rb +122 -0
  105. data/lib/megam/core/sshkey.rb +173 -0
  106. data/lib/megam/core/sshkey_collection.rb +122 -0
  107. data/lib/megam/core/stuff.rb +22 -0
  108. data/lib/megam/core/subscriptions.rb +167 -0
  109. data/lib/megam/core/subscriptions_collection.rb +121 -0
  110. data/lib/megam/core/text.rb +87 -0
  111. data/lib/megam/core/text_formatter.rb +64 -0
  112. data/lib/megam/mixins/assemblies.rb +16 -0
  113. data/lib/megam/mixins/assembly.rb +59 -0
  114. data/lib/megam/mixins/common_deployable.rb +78 -0
  115. data/lib/megam/mixins/components.rb +182 -0
  116. data/lib/megam/mixins/megam_attributes.rb +30 -0
  117. data/lib/megam/mixins/outputs.rb +23 -0
  118. data/lib/megam/mixins/policies.rb +27 -0
  119. data/lib/megam_api.rb +1 -0
  120. data/megam_api.gemspec +26 -0
  121. data/test/mixins/test_assemblies.rb +113 -0
  122. data/test/mixins/test_assembly.rb +74 -0
  123. data/test/mixins/test_component.rb +19 -0
  124. data/test/test_accounts.rb +127 -0
  125. data/test/test_addons.rb +26 -0
  126. data/test/test_assemblies.rb +88 -0
  127. data/test/test_assembly.rb +37 -0
  128. data/test/test_backups.rb +29 -0
  129. data/test/test_balances.rb +38 -0
  130. data/test/test_billedhistories.rb +30 -0
  131. data/test/test_billingtranscations.rb +35 -0
  132. data/test/test_components.rb +42 -0
  133. data/test/test_credits.rb +28 -0
  134. data/test/test_disks.rb +25 -0
  135. data/test/test_domains.rb +29 -0
  136. data/test/test_eventsbilling.rb +30 -0
  137. data/test/test_eventscontainer.rb +26 -0
  138. data/test/test_eventsstorage.rb +12 -0
  139. data/test/test_eventsvm.rb +26 -0
  140. data/test/test_helper.rb +77 -0
  141. data/test/test_marketplaces.rb +38 -0
  142. data/test/test_organizations.rb +45 -0
  143. data/test/test_promos.rb +11 -0
  144. data/test/test_quotas.rb +33 -0
  145. data/test/test_reports.rb +32 -0
  146. data/test/test_requests.rb +95 -0
  147. data/test/test_sensors.rb +50 -0
  148. data/test/test_snapshots.rb +30 -0
  149. data/test/test_sshkeys.rb +50 -0
  150. data/test/test_subscriptions.rb +22 -0
  151. metadata +325 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c47461d442d1b7cf2300107ff10bc1ce1b78a77e
4
+ data.tar.gz: 2a20eb725ec3c8672e61f50f059d7d405a268fe2
5
+ SHA512:
6
+ metadata.gz: 2561146139a21ea97dbe03a2ba3a7d2ac0e2bae04b6804058077e50b7692f4f3fd3915611c6db35c0cdefbba5cfb935b733b264e0f308b75ddb58deed69cc2bc
7
+ data.tar.gz: 18a266404ff29c17e812697324d8874c46976db0161f8cf84ccc9b9bbf2a0009baaba24008db2f3bff9bdd8353755d2265bba4bd04cb05955a86cb2352cb0b9e
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .project
5
+ .config
6
+ coverage
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+ .c9/*
17
+
18
+ # YARD artifacts
19
+ .yardoc
20
+ _yardoc
21
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ notifications:
4
+ email:
5
+ recipients:
6
+ - info@megam.io
7
+
8
+ rvm:
9
+ - 2.3.1
10
+
11
+ script: bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ #source "https://rubygems.org"
2
+ source :rubygems
3
+
4
+ # Specify your gem's dependencies in megam_api.gemspec
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2013-2016 Megam Systems. https://www.megam.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ Ruby API for Vertice
2
+ =====================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/megam_api.svg)](http://badge.fury.io/rb/megam_api)
5
+
6
+ This is a Ruby SDK API for Vertice.
7
+
8
+
9
+ Usage
10
+ -----
11
+
12
+ ```shell
13
+
14
+ gem install megam_api
15
+
16
+ ```
17
+
18
+ For more implementation details [see nilavu](http://github.com/megamsys/nilavu.git)
19
+
20
+
21
+ # License
22
+
23
+ MIT
24
+
25
+ # Author
26
+
27
+ Refer [https://rubygems.org/gems/megam_api](https://rubygems.org/gems/megam_api)
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |task|
8
+ task.name = :test
9
+ task.test_files = FileList['test/test*.rb']
10
+ end
data/lib/megam/api.rb ADDED
@@ -0,0 +1,329 @@
1
+ require 'base64'
2
+ require 'time'
3
+ require 'excon'
4
+ require 'uri'
5
+ require 'zlib'
6
+ require 'openssl'
7
+
8
+ __LIB_DIR__ = File.expand_path(File.join(File.dirname(__FILE__), '..'))
9
+ $LOAD_PATH.unshift(__LIB_DIR__) unless $LOAD_PATH.include?(__LIB_DIR__)
10
+
11
+ require 'megam/api/accounts'
12
+ require 'megam/api/assemblies'
13
+ require 'megam/api/assembly'
14
+ require 'megam/api/balances'
15
+ require 'megam/api/credits'
16
+ require 'megam/api/billedhistories'
17
+ require 'megam/api/billingtransactions'
18
+ require 'megam/api/components'
19
+ require 'megam/api/domains'
20
+ require 'megam/api/errors'
21
+ require 'megam/api/marketplaces'
22
+ require 'megam/api/organizations'
23
+ require 'megam/api/promos'
24
+ require 'megam/api/requests'
25
+ require 'megam/api/sensors'
26
+ require 'megam/api/sshkeys'
27
+ require 'megam/api/eventsall'
28
+ require 'megam/api/eventsvm'
29
+ require 'megam/api/eventsmarketplace'
30
+ require 'megam/api/license'
31
+ require 'megam/api/flavors'
32
+ require 'megam/api/eventscontainer'
33
+ require 'megam/api/eventsbilling'
34
+ require 'megam/api/eventsstorage'
35
+ require 'megam/api/snapshots'
36
+ require 'megam/api/backups'
37
+ require 'megam/api/reports'
38
+ require 'megam/api/quotas'
39
+ require 'megam/api/rawimages'
40
+ require 'megam/api/disks'
41
+ require 'megam/api/subscriptions'
42
+ require 'megam/api/addons'
43
+ require 'megam/api/version'
44
+
45
+
46
+ require 'megam/mixins/assemblies'
47
+ require 'megam/mixins/assembly'
48
+ require 'megam/mixins/common_deployable'
49
+ require 'megam/mixins/components'
50
+ require 'megam/mixins/megam_attributes'
51
+ require 'megam/mixins/outputs'
52
+ require 'megam/mixins/policies'
53
+
54
+ require 'megam/core/rest_adapter'
55
+ require 'megam/core/stuff'
56
+ require 'megam/core/text'
57
+ require 'megam/core/log'
58
+ require 'megam/core/json_compat'
59
+ require 'megam/core/error'
60
+ require 'megam/core/account'
61
+ require 'megam/core/account_collection'
62
+ require 'megam/core/request'
63
+ require 'megam/core/request_collection'
64
+ require 'megam/core/license'
65
+ require 'megam/core/license_collection'
66
+ require 'megam/core/flavors'
67
+ require 'megam/core/flavors_collection'
68
+ require 'megam/core/sshkey'
69
+ require 'megam/core/sshkey_collection'
70
+ require 'megam/core/eventsall'
71
+ require 'megam/core/eventsall_collection'
72
+ require 'megam/core/eventsvm'
73
+ require 'megam/core/eventsvm_collection'
74
+ require 'megam/core/eventsmarketplace'
75
+ require 'megam/core/eventsmarketplace_collection'
76
+ require 'megam/core/eventscontainer'
77
+ require 'megam/core/eventscontainer_collection'
78
+ require 'megam/core/eventsbilling'
79
+ require 'megam/core/eventsbilling_collection'
80
+ require 'megam/core/eventsstorage'
81
+ require 'megam/core/eventsstorage_collection'
82
+ require 'megam/core/marketplace'
83
+ require 'megam/core/marketplace_collection'
84
+ require 'megam/core/organizations'
85
+ require 'megam/core/organizations_collection'
86
+ require 'megam/core/domains'
87
+ require 'megam/core/domain_collection'
88
+ require 'megam/core/assemblies'
89
+ require 'megam/core/assemblies_collection'
90
+ require 'megam/core/konipai'
91
+ require 'megam/core/assembly'
92
+ require 'megam/core/assembly_collection'
93
+ require 'megam/core/components'
94
+ require 'megam/core/components_collection'
95
+ require 'megam/core/sensors'
96
+ require 'megam/core/sensors_collection'
97
+ require 'megam/core/snapshots'
98
+ require 'megam/core/snapshots_collection'
99
+ require 'megam/core/backups'
100
+ require 'megam/core/backups_collection'
101
+ require 'megam/core/reports'
102
+ require 'megam/core/reports_collection'
103
+ require 'megam/core/quotas'
104
+ require 'megam/core/quotas_collection'
105
+ require 'megam/core/rawimages'
106
+ require 'megam/core/rawimages_collection'
107
+ require 'megam/core/disks'
108
+ require 'megam/core/disks_collection'
109
+ require 'megam/core/balances_collection'
110
+ require 'megam/core/balances'
111
+ require 'megam/core/credits_collection'
112
+ require 'megam/core/credits'
113
+ require 'megam/core/billedhistories_collection'
114
+ require 'megam/core/billedhistories'
115
+ require 'megam/core/billingtransactions_collection'
116
+ require 'megam/core/billingtransactions'
117
+ require 'megam/core/subscriptions_collection'
118
+ require 'megam/core/subscriptions'
119
+ require 'megam/core/addons_collection'
120
+ require 'megam/core/addons'
121
+ require 'megam/core/promos'
122
+
123
+ module Megam
124
+ class API
125
+ attr_accessor :text
126
+ attr_accessor :email, :api_key, :password_hash, :org_id
127
+ attr_accessor :api_url, :api_version
128
+ attr_accessor :master_key
129
+ attr_reader :last_response
130
+
131
+ API_VERSION2 = '/v2'.freeze
132
+
133
+ X_Megam_DATE = 'X-Megam-DATE'.freeze
134
+ X_Megam_HMAC = 'X-Megam-HMAC'.freeze
135
+ X_Megam_OTTAI = 'X-Megam-OTTAI'.freeze
136
+ X_Megam_ORG = 'X-Megam-ORG'.freeze
137
+ X_Megam_PUTTUSAVI = 'X-Megam-PUTTUSAVI'.freeze
138
+ X_Megam_MASTERKEY = 'X-Megam-MASTERKEY'.freeze
139
+
140
+ HEADERS = {
141
+ 'Accept' => 'application/json',
142
+ 'Accept-Encoding' => 'gzip',
143
+ 'User-Agent' => "megam-api/#{Megam::API::VERSION}",
144
+ 'X-Ruby-Version' => RUBY_VERSION,
145
+ 'X-Ruby-Platform' => RUBY_PLATFORM
146
+ }
147
+
148
+ OPTIONS = {
149
+ headers: {},
150
+ nonblock: false
151
+ }
152
+
153
+ def initialize(options = {})
154
+ @options = OPTIONS.merge(options)
155
+
156
+ assign_credentials
157
+
158
+ ensure_host_is_flattened
159
+
160
+ turn_off_ssl_verify
161
+ end
162
+
163
+
164
+ def text
165
+ @text ||= Megam::Text.new(STDOUT, STDERR, STDIN, {})
166
+ end
167
+
168
+ def request(params, &block)
169
+ just_color_debug("#{@options[:path]}")
170
+ start = Time.now
171
+ Megam::Log.debug('START')
172
+
173
+ ensure_authkeys unless is_passthru?
174
+
175
+ begin
176
+ response = connection.request(params, &block)
177
+ rescue Excon::Errors::HTTPStatusError => error
178
+ klass = case error.response.status
179
+
180
+ when 401 then Megam::API::Errors::Unauthorized
181
+ when 403 then Megam::API::Errors::Forbidden
182
+ when 404 then Megam::API::Errors::NotFound
183
+ when 408 then Megam::API::Errors::Timeout
184
+ when 422 then Megam::API::Errors::RequestFailed
185
+ when 423 then Megam::API::Errors::Locked
186
+ when /50./ then Megam::API::Errors::RequestFailed
187
+ else Megam::API::Errors::ErrorWithResponse
188
+ end
189
+ reerror = klass.new(error.message, error.response)
190
+ reerror.set_backtrace(error.backtrace)
191
+ reerror.response.body = Megam::JSONCompat.from_json(reerror.response.body.chomp)
192
+ raise(reerror)
193
+ end
194
+
195
+ @last_response = response
196
+
197
+ if response.body && !response.body.empty?
198
+ if response.headers['Content-Encoding'] == 'gzip'
199
+ response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
200
+ end
201
+
202
+ begin
203
+ unless response.headers[X_Megam_OTTAI]
204
+ response.body = Megam::JSONCompat.from_json(response.body.chomp)
205
+ else
206
+ response.body = Megam::KoniPai.new.koni(response.body.chomp)
207
+ end
208
+ rescue Exception => jsonerr
209
+ raise(jsonerr)
210
+ end
211
+ end
212
+
213
+ Megam::Log.debug("END(#{(Time.now - start)}s)")
214
+
215
+ @connection.reset
216
+ response
217
+ end
218
+
219
+ private
220
+
221
+ def assign_credentials
222
+ @api_key = @options.delete(:api_key) || ENV['VERTICE_API_KEY']
223
+ @email = @options.delete(:email) || ENV['VERTICE_EMAIL']
224
+ @password_hash = @options.delete(:password_hash)
225
+ @org_id = @options.delete(:org_id)
226
+ @master_key = @options.delete(:master_key) || ENV['VERTICE_MASTER_KEY']
227
+ end
228
+
229
+ def ensure_host_is_flattened
230
+
231
+ uri = URI(@options.delete(:host)) if @options.has_key?(:host)
232
+
233
+ scheme = (uri && uri.scheme) ? uri.scheme : 'http'
234
+
235
+ host = (uri && uri.host) ? uri.host : 'localhost'
236
+
237
+ port = (uri && uri.port) ? uri.port.to_s : '9000'
238
+
239
+ @api_version = (uri && uri.path) ? uri.path : API_VERSION2
240
+
241
+ @api_url = scheme + "://" + host + ":" + port + @api_version
242
+ end
243
+
244
+ def is_passthru?
245
+ @options[:passthru]
246
+ end
247
+
248
+ def ensure_authkeys
249
+ if !api_combo_missing? && !pw_combo_missing? && !masterkey_combo_missing?
250
+ fail Megam::API::Errors::AuthKeysMissing
251
+ end
252
+ end
253
+
254
+ def api_combo_missing?
255
+ (!@email.nil? && !@api_key.nil?)
256
+ end
257
+
258
+ def pw_combo_missing?
259
+ (!@email.nil? && !@password_hash.nil?)
260
+ end
261
+
262
+ def masterkey_combo_missing?
263
+ (!@email.nil? && !@master_key.nil?)
264
+ end
265
+
266
+ def turn_off_ssl_verify
267
+ Excon.defaults[:ssl_verify_peer] = false unless @api_url.include?("https")
268
+ end
269
+
270
+ def just_color_debug(path)
271
+ text.msg "--> #{text.color("(#{path})", :cyan, :bold)}"
272
+ end
273
+
274
+ def connection
275
+ @options[:path] = @api_version + @options[:path]
276
+
277
+ build_headers
278
+
279
+ @connection = Excon.new(@api_url, @options)
280
+ end
281
+
282
+
283
+ def build_headers
284
+ encoded = encode_header
285
+
286
+ @options[:headers] = HEADERS.merge(X_Megam_HMAC => encoded[:hmac],
287
+ X_Megam_DATE => encoded[:date],
288
+ X_Megam_ORG => @org_id).merge(@options[:headers])
289
+
290
+
291
+ build_header_puttusavi
292
+
293
+ build_header_masterkey
294
+ end
295
+
296
+ def encode_header
297
+ body_base64 = Base64.urlsafe_encode64(OpenSSL::Digest::MD5.digest(@options[:body]))
298
+ current_date = Time.now.strftime('%Y-%m-%d %H:%M')
299
+
300
+ movingFactor = "#{current_date}" + "\n" + "#{@options[:path]}" + "\n" + "#{body_base64}"
301
+
302
+ digest = OpenSSL::Digest.new('sha256')
303
+
304
+ if pw_combo_missing? && !is_passthru?
305
+ hash = OpenSSL::HMAC.hexdigest(digest, Base64.strict_decode64(@password_hash), movingFactor)
306
+ elsif api_combo_missing?
307
+ hash = OpenSSL::HMAC.hexdigest(digest, @api_key, movingFactor)
308
+ elsif masterkey_combo_missing?
309
+ hash = OpenSSL::HMAC.hexdigest(digest, @master_key, movingFactor)
310
+ else
311
+ hash = ""
312
+ end
313
+ { hmac: (@email + ':' + hash), date: current_date }
314
+ end
315
+
316
+ def build_header_puttusavi
317
+ if pw_combo_missing? && !is_passthru?
318
+ @options[:headers] = @options[:headers].merge(X_Megam_PUTTUSAVI => "true")
319
+ end
320
+ end
321
+
322
+ def build_header_masterkey
323
+ if masterkey_combo_missing? && !is_passthru?
324
+ @options[:headers] = @options[:headers].merge(X_Megam_MASTERKEY => "true")
325
+ end
326
+ end
327
+
328
+ end
329
+ end
@@ -0,0 +1,96 @@
1
+ module Megam
2
+ class API
3
+ # GET /accounts
4
+ def get_accounts(email)
5
+ @options = {:path => "/accounts/#{email}",
6
+ :body => ''}.merge(@options)
7
+
8
+ request(
9
+ :expects => 200,
10
+ :method => :get,
11
+ :body => @options[:body]
12
+ )
13
+ end
14
+
15
+ # The body content needs to be a json.
16
+ def login(new_account)
17
+ @options = {:path => '/accounts/login',
18
+ :body => Megam::JSONCompat.to_json(new_account)}.merge(@options)
19
+
20
+ request(
21
+ :expects => 302,
22
+ :method => :post,
23
+ :body => @options[:body]
24
+ )
25
+ end
26
+
27
+ # The body content needs to be a json.
28
+ def post_accounts(new_account)
29
+ @options = {path: '/accounts/content',
30
+ body: Megam::JSONCompat.to_json(new_account)}.merge(@options)
31
+
32
+ request(
33
+ :expects => 201,
34
+ :method => :post,
35
+ :body => @options[:body]
36
+ )
37
+ end
38
+
39
+ def update_accounts(update_account)
40
+ @options = {path: '/accounts/update', passthru: true,
41
+ body: Megam::JSONCompat.to_json(update_account)}.merge(@options)
42
+
43
+ request(
44
+ :expects => 201,
45
+ :method => :post,
46
+ :body => @options[:body]
47
+ )
48
+ end
49
+
50
+ def forgot(account)
51
+ @options = { path: "/accounts/forgot/#{account["email"]}", passthru: true,
52
+ body: ''}.merge(@options)
53
+
54
+ request(
55
+ :expects => 201,
56
+ :method => :get,
57
+ :body => @options[:body]
58
+ )
59
+ end
60
+
61
+ def delete_accounts(delete_account)
62
+ @options = {path: "/admin/accounts/#{delete_account["user_email"]}",
63
+ :body => ''}.merge(@options)
64
+
65
+ request(
66
+ :expects => 200,
67
+ :method => :delete,
68
+ :body => @options[:body]
69
+ )
70
+ end
71
+
72
+ def password_reset(account)
73
+ @options = {:path => "/accounts/password_reset", passthru: true,
74
+ body: Megam::JSONCompat.to_json(account)}.merge(@options)
75
+
76
+ request(
77
+ :expects => 201,
78
+ :method => :post,
79
+ :body => @options[:body]
80
+ )
81
+ end
82
+
83
+ # ADMIN: LIST /accounts
84
+ def list_accounts
85
+ @options = {:path => "/admin/accounts",
86
+ :body => ''}.merge(@options)
87
+
88
+ request(
89
+ :expects => 200,
90
+ :method => :get,
91
+ :body => @options[:body]
92
+ )
93
+ end
94
+
95
+ end
96
+ end