megam_api 0.75 → 0.77

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 71c2300fe07469f9620a2a8aa85416daca5ec6a7
4
- data.tar.gz: 625d47cb3a4f19f7c4c4597e4a9bc5c304f4fdd2
3
+ metadata.gz: 8425f7c1d458f71a142ce97114a8530e4fed4336
4
+ data.tar.gz: ff54ea8456f042cf79616c70ef34307135463c78
5
5
  SHA512:
6
- metadata.gz: 607188d7cae267b3418ee76b2ade88323a87ab161862bb8677f21eb18142d138f7ef688ffd31c6413868e47f2c996ba548f3a50e857d69973de953471d275b90
7
- data.tar.gz: 7eb58498ba9585db3401e347d9ef9bf84b230ad0100485a3406d8ccfb5a0c24d8b11f70ab4086677bd258b909c9c0a2e2540dd13350e8c42e9ad8901db4eded2
6
+ metadata.gz: 755d8b9d59fa9ff9a085c3aaf0b570027d9e858efe69b939f7c7235391d84694ea76b0601f37e84fe65a01eb5bba8a95632d321034b6651d6be83db8fc43ec6d
7
+ data.tar.gz: 69483f9bf6ac8a348f03e6a5897cc5a0926864f75ab1be1d27c183fd5bab38fe2d11edfa2ad5a083a5736fd2838a29ed98615ce8db431b61d6bd9213b600d4d9
data/lib/megam/api.rb CHANGED
@@ -24,7 +24,7 @@ require 'megam/api/csars'
24
24
  require 'megam/api/assemblies'
25
25
  require 'megam/api/assembly'
26
26
  require 'megam/api/components'
27
- require 'megam/api/event'
27
+ require 'megam/api/sensors'
28
28
  require 'megam/api/availableunits'
29
29
  require 'megam/api/balances'
30
30
  require 'megam/api/billedhistories'
@@ -66,8 +66,8 @@ require 'megam/core/assembly'
66
66
  require 'megam/core/assembly_collection'
67
67
  require 'megam/core/components'
68
68
  require 'megam/core/components_collection'
69
- require 'megam/core/event'
70
-
69
+ require 'megam/core/sensors'
70
+ require 'megam/core/sensors_collection'
71
71
  require 'megam/core/availableunits_collection'
72
72
  require 'megam/core/availableunits'
73
73
  require 'megam/core/balances_collection'
@@ -91,7 +91,6 @@ module Megam
91
91
  # text is used to print stuff in the terminal (message, log, info, warn etc.)
92
92
  attr_accessor :text
93
93
 
94
- API_MEGAM_IO = 'api.megam.io'.freeze
95
94
  API_VERSION2 = '/v2'.freeze
96
95
 
97
96
  X_Megam_DATE = 'X-Megam-DATE'.freeze
@@ -128,13 +127,13 @@ module Megam
128
127
  # 3. Upon merge of the options, the api_key, email as available in the @options is deleted.
129
128
  def initialize(options = {})
130
129
  @options = OPTIONS.merge(options)
131
- puts @options
132
130
  @api_key = @options.delete(:api_key) || ENV['MEGAM_API_KEY']
133
131
  @email = @options.delete(:email)
134
- fail ArgumentError, 'You must specify [:email, :api_key]' if @email.nil? || @api_key.nil?
132
+ fail Megam::API::Errors::AuthKeysMissing if @email.nil? || @api_key.nil?
135
133
  end
136
134
 
137
135
  def request(params, &block)
136
+
138
137
  just_color_debug("#{@options[:path]}")
139
138
  start = Time.now
140
139
  Megam::Log.debug('START')
@@ -213,7 +212,7 @@ module Megam
213
212
  @options[:path] = API_VERSION2 + @options[:path]
214
213
  encoded_api_header = encode_header(@options)
215
214
  @options[:headers] = HEADERS.merge(X_Megam_HMAC => encoded_api_header[:hmac],
216
- X_Megam_DATE => encoded_api_header[:date]).merge(@options[:headers])
215
+ X_Megam_DATE => encoded_api_header[:date]).merge(@options[:headers])
217
216
 
218
217
  Megam::Log.debug('HTTP Request Data:')
219
218
  Megam::Log.debug("> HTTP #{@options[:scheme]}://#{@options[:host]}")
@@ -252,5 +251,5 @@ module Megam
252
251
  final_hmac = @email + ':' + hash
253
252
  header_params = { hmac: final_hmac, date: current_date }
254
253
  end
255
- end
254
+ end
256
255
  end
@@ -1,9 +1,7 @@
1
1
  module Megam
2
2
  class API
3
3
  def get_discounts
4
- puts "Entered get discount--weehaa!"
5
4
  @options = {:path => '/discounts',:body => ""}.merge(@options)
6
- puts @options
7
5
  request(
8
6
  :expects => 200,
9
7
  :method => :get,
@@ -11,10 +9,9 @@ module Megam
11
9
  )
12
10
  end
13
11
 
14
-
15
12
  def post_discounts(new_discount)
16
13
  @options = {:path => '/discounts/content',
17
- :body => Megam::JSONCompat.to_json(new_discount)}.merge(@options)
14
+ :body => Megam::JSONCompat.to_json(new_discount)}.merge(@options)
18
15
 
19
16
  request(
20
17
  :expects => 201,
@@ -22,13 +19,11 @@ module Megam
22
19
  :body => @options[:body]
23
20
  )
24
21
  end
25
-
26
-
27
22
  def update_discounts(update_discount)
28
23
  @options = {:path => '/discounts/update',
29
24
  :body => Megam::JSONCompat.to_json(update_discount)}.merge(@options)
30
25
 
31
- request(
26
+ request(
32
27
  :expects => 201,
33
28
  :method => :post,
34
29
  :body => @options[:body]
@@ -10,8 +10,8 @@ module Megam
10
10
  super message
11
11
  @response = response
12
12
  end
13
-
14
-
13
+
14
+
15
15
  end
16
16
 
17
17
  class Unauthorized < ErrorWithResponse; end
@@ -22,6 +22,8 @@ module Megam
22
22
  class Socket < ErrorWithResponse; end
23
23
  class RequestFailed < ErrorWithResponse; end
24
24
 
25
+ class AuthKeysMissing < ArgumentError; end
26
+
25
27
  end
26
28
  end
27
29
  end
@@ -0,0 +1,35 @@
1
+ module Megam
2
+ class API
3
+ def get_sensors
4
+ @options = {:path => '/sensors',:body => ""}.merge(@options)
5
+
6
+ request(
7
+ :expects => 200,
8
+ :method => :get,
9
+ :body => @options[:body]
10
+ )
11
+ end
12
+
13
+ def get_sensor(id)
14
+ @options = {:path => "/sensors/#{id}",:body => ""}.merge(@options)
15
+
16
+ request(
17
+ :expects => 200,
18
+ :method => :get,
19
+ :body => @options[:body]
20
+ )
21
+ end
22
+
23
+ def post_sensors(new_sensors)
24
+ @options = {:path => '/sensors/content',
25
+ :body => Megam::JSONCompat.to_json(new_sensors)}.merge(@options)
26
+
27
+ request(
28
+ :expects => 201,
29
+ :method => :post,
30
+ :body => @options[:body]
31
+ )
32
+ end
33
+
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  module Megam
2
2
  class API
3
- VERSION = "0.75"
3
+ VERSION = "0.77"
4
4
  end
5
5
  end
@@ -49,7 +49,8 @@ module Megam
49
49
  MEGAM_DISCOUNTS = "Megam::Discounts".freeze
50
50
  MEGAM_DISCOUNTSCOLLECTION = "Megam::DiscountsCollection".freeze
51
51
  MEGAM_ERROR = "Megam::Error".freeze
52
- MEGAM_EVENT = "Megam::Event".freeze
52
+ MEGAM_SENSORS = "Megam::Sensors".freeze
53
+ MEGAM_SENSORSCOLLECTION = "Megam::SensorsCollection".freeze
53
54
  MEGAM_MARKETPLACE = "Megam::MarketPlace".freeze
54
55
  MEGAM_MARKETPLACECOLLECTION = "Megam::MarketPlaceCollection".freeze
55
56
  MEGAM_MARKETPLACEADDON = "Megam::MarketPlaceAddons".freeze
@@ -65,13 +66,14 @@ module Megam
65
66
  MEGAM_PROMOS = "Megam::Promos".freeze
66
67
 
67
68
 
69
+
68
70
  class <<self
69
71
  # Increase the max nesting for JSON, which defaults
70
72
  # to 19, and isn't enough for some (for example, a Node within a Node)
71
73
  # structures.
72
74
  def opts_add_max_nesting(opts)
73
- if opts.nil? || !opts.has_key?(:max_nesting)
74
- opts = opts.nil? ? Hash.new : opts.clone
75
+ if opts.nil? || !opts.key?(:max_nesting)
76
+ opts = opts.nil? ? {} : opts.clone
75
77
  opts[:max_nesting] = JSON_MAX_NESTING
76
78
  end
77
79
  opts
@@ -84,8 +86,8 @@ module Megam
84
86
  # JSON gem requires top level object to be a Hash or Array (otherwise
85
87
  # you get the "must contain two octets" error). Yajl doesn't impose the
86
88
  # same limitation. For compatibility, we re-impose this condition.
87
- unless obj.kind_of?(Hash) or obj.kind_of?(Array)
88
- raise JSON::ParserError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
89
+ unless obj.is_a?(Hash) || obj.is_a?(Array)
90
+ fail JSON::ParserError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
89
91
  end
90
92
 
91
93
  # The old default in the json gem (which we are mimicing because we
@@ -95,28 +97,27 @@ module Megam
95
97
  if opts[:create_additions].nil? || opts[:create_additions]
96
98
  map_to_rb_obj(obj)
97
99
  else
98
- obj
100
+ obj
99
101
  end
100
102
  end
101
103
 
102
104
  # Look at an object that's a basic type (from json parse) and convert it
103
105
  # to an instance of Megam classes if desired.
104
106
  def map_to_rb_obj(json_obj)
105
-
106
107
  case json_obj
107
108
  when Hash
108
109
  mapped_hash = map_hash_to_rb_obj(json_obj)
109
110
 
110
- if json_obj.has_key?(JSON_CLAZ) && (class_to_inflate = class_for_json_class(json_obj[JSON_CLAZ]))
111
- class_to_inflate.json_create(mapped_hash)
111
+ if json_obj.key?(JSON_CLAZ) && (class_to_inflate = class_for_json_class(json_obj[JSON_CLAZ]))
112
+ class_to_inflate.json_create(mapped_hash)
112
113
 
113
114
  else
114
- mapped_hash
115
+ mapped_hash
115
116
  end
116
117
  when Array
117
- json_obj.map {|e| map_to_rb_obj(e)}
118
+ json_obj.map { |e| map_to_rb_obj(e) }
118
119
  else
119
- json_obj
120
+ json_obj
120
121
  end
121
122
  end
122
123
 
@@ -185,8 +186,10 @@ module Megam
185
186
  Megam::CSARCollection
186
187
  when MEGAM_DOMAIN
187
188
  Megam::Domains
188
- when MEGAM_EVENT
189
- Megam::Event
189
+ when MEGAM_SENSORS
190
+ Megam::Sensors
191
+ when MEGAM_SENSORSCOLLECTION
192
+ Megam::SensorsCollection
190
193
  when MEGAM_AVAILABLEUNITS
191
194
  Megam::Availableunits
192
195
  when MEGAM_AVAILABLEUNITSCOLLECTION
@@ -200,9 +203,9 @@ module Megam
200
203
  when MEGAM_BILLEDHISTORIESCOLLECTION
201
204
  Megam::BilledhistoriesCollection
202
205
  when MEGAM_INVOICES
203
- Megam::Invoices
204
- when MEGAM_INVOICESCOLLECTION
205
- Megam::InvoicesCollection
206
+ Megam::Invoices
207
+ when MEGAM_INVOICESCOLLECTION
208
+ Megam::InvoicesCollection
206
209
  when MEGAM_BILLINGS
207
210
  Megam::Billings
208
211
  when MEGAM_BILLINGSCOLLECTION
@@ -222,10 +225,9 @@ module Megam
222
225
  when MEGAM_PROMOS
223
226
  Megam::Promos
224
227
  else
225
- raise JSON::ParserError, "Unsupported `json_class` type '#{json_class}'"
228
+ fail JSON::ParserError, "Unsupported `json_class` type '#{json_class}'"
226
229
  end
227
230
  end
228
-
229
231
  end
230
232
  end
231
233
  end
@@ -32,7 +32,7 @@ module Megam
32
32
  if arg != nil
33
33
  @id = arg
34
34
  else
35
- @id
35
+ @id
36
36
  end
37
37
  end
38
38
 
@@ -40,7 +40,7 @@ module Megam
40
40
  if arg != nil
41
41
  @code = arg
42
42
  else
43
- @code
43
+ @code
44
44
  end
45
45
  end
46
46
 
@@ -48,7 +48,7 @@ module Megam
48
48
  if arg != nil
49
49
  @amount = arg
50
50
  else
51
- @amount
51
+ @amount
52
52
  end
53
53
  end
54
54
 
@@ -56,7 +56,7 @@ module Megam
56
56
  if arg != nil
57
57
  @created_at = arg
58
58
  else
59
- @created_at
59
+ @created_at
60
60
  end
61
61
  end
62
62
 
@@ -66,7 +66,7 @@ module Megam
66
66
  if arg != nil
67
67
  @some_msg = arg
68
68
  else
69
- @some_msg
69
+ @some_msg
70
70
  end
71
71
  end
72
72
 
@@ -146,14 +146,12 @@ module Megam
146
146
 
147
147
  # Show a particular promo by name,
148
148
  # Megam::Promos
149
- def self.show(params)
150
- puts "[x] Called show"
149
+ def self.show(params)
151
150
  pre = self.new(params["email"], params["api_key"], params["host"])
152
151
  pre.megam_rest.get_promos(params["code"])
153
152
  end
154
153
 
155
154
 
156
-
157
155
  def to_s
158
156
  Megam::Stuff.styled_hash(to_hash)
159
157
  end
@@ -190,7 +190,6 @@ module Megam
190
190
 
191
191
  # Create the node via the REST API
192
192
  def create
193
- puts "entering megam_api"
194
193
  megam_rest.post_request(to_hash)
195
194
  end
196
195
 
@@ -0,0 +1,157 @@
1
+ # Copyright:: Copyright (c) 2012, 2014 Megam Systems
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module Megam
18
+ class Sensors < Megam::ServerAPI
19
+ def initialize(email = nil, api_key = nil, host = nil)
20
+ @id = nil
21
+ @sensor_type = nil
22
+ @payload = {}
23
+ @created_at = nil
24
+ super(email, api_key, host)
25
+ end
26
+
27
+ def sensors
28
+ self
29
+ end
30
+
31
+ def id(arg = nil)
32
+ if !arg.nil?
33
+ @id = arg
34
+ else
35
+ @id
36
+ end
37
+ end
38
+
39
+ def sensor_type(arg = nil)
40
+ if !arg.nil?
41
+ @sensor_type = arg
42
+ else
43
+ @sensor_type
44
+ end
45
+ end
46
+
47
+ def payload(arg = nil)
48
+ if !arg.nil?
49
+ @payload = arg
50
+ else
51
+ @payload
52
+ end
53
+ end
54
+
55
+ def created_at(arg = nil)
56
+ if !arg.nil?
57
+ @created_at = arg
58
+ else
59
+ @created_at
60
+ end
61
+ end
62
+
63
+ def error?
64
+ crocked = true if some_msg.key?(:msg_type) && some_msg[:msg_type] == 'error'
65
+ end
66
+
67
+ # Transform the ruby obj -> to a Hash
68
+ def to_hash
69
+ index_hash = {}
70
+ index_hash['json_claz'] = self.class.name
71
+ index_hash['id'] = id
72
+
73
+ index_hash['sensor_type'] = sensor_type
74
+ index_hash['payload'] = payload
75
+ index_hash['created_at'] = created_at
76
+ index_hash
77
+ end
78
+
79
+ # Serialize this object as a hash: called from JsonCompat.
80
+ # Verify if this called from JsonCompat during testing.
81
+ def to_json(*a)
82
+ for_json.to_json(*a)
83
+ end
84
+
85
+ def for_json
86
+ result = {
87
+ 'id' => id,
88
+ 'sensor_type' => sensor_type,
89
+ 'payload' => payload,
90
+ 'created_at' => created_at
91
+ }
92
+ result
93
+ end
94
+
95
+ def self.json_create(o)
96
+ asm = new
97
+ asm.id(o['id']) if o.key?('id')
98
+ asm.sensor_type(o['sensor_type']) if o.key?('sensor_type')
99
+ asm.payload(o['payload']) if o.key?('payload')
100
+ asm.created_at(o['created_at']) if o.key?('created_at')
101
+ asm
102
+ end
103
+
104
+ def self.from_hash(o, tmp_email = nil, tmp_api_key = nil, tmp_host = nil)
105
+ asm = new(tmp_email, tmp_api_key, tmp_host)
106
+ asm.from_hash(o)
107
+ asm
108
+ end
109
+
110
+ def from_hash(o)
111
+ @id = o['id'] if o.key?('id')
112
+ @sensor_type = o['sensor_type'] if o.key?('sensor_type')
113
+ @payload = o['payload'] if o.key?('payload')
114
+ @created_at = o['created_at'] if o.key?('created_at')
115
+ self
116
+ end
117
+
118
+ def self.create(params)
119
+ sensors = from_hash(params, params['email'], params['api_key'], params['host'])
120
+ sensors.create
121
+ end
122
+
123
+ # Load a account by email_p
124
+ def self.show(params)
125
+ sensors = new(params['email'], params['api_key'], params['host'])
126
+ sensors.megam_rest.get_sensor(params['id'])
127
+ end
128
+
129
+ def self.list(params)
130
+ sensors = self.new(params["email"], params["api_key"], params["host"])
131
+ sensors.megam_rest.get_sensors
132
+ end
133
+
134
+ def self.update(params)
135
+ sensors = from_hash(params, params['email'] || params[:email], params['api_key'] || params[:api_key], params['host'] || params[:host])
136
+ sensors.update
137
+ end
138
+
139
+ def self.list(o)
140
+ app = self.new(o["email"], o["api_key"], o["host"])
141
+ app.megam_rest.get_sensors
142
+ end
143
+
144
+ # Create the node via the REST API
145
+ def update
146
+ megam_rest.update_(to_hash)
147
+ end
148
+
149
+ def create
150
+ megam_rest.post_sensors(to_hash)
151
+ end
152
+
153
+ def to_s
154
+ Megam::Stuff.styled_hash(to_hash)
155
+ end
156
+ end
157
+ end