oneapi-ruby 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.
@@ -0,0 +1,205 @@
1
+ require_relative 'objects'
2
+
3
+ # ----------------------------------------------------------------------------------------------------
4
+ # Generic:
5
+ # ----------------------------------------------------------------------------------------------------
6
+
7
+ module OneApi
8
+
9
+ class OneApiAuthentication < OneApiModel
10
+
11
+ oneapi_attr_accessor :username, FieldConversionRule.new()
12
+ oneapi_attr_accessor :password, FieldConversionRule.new()
13
+ oneapi_attr_accessor :ibsso_token, FieldConversionRule.new('login.ibAuthCookie | TODO')
14
+ oneapi_attr_accessor :authenticated, FieldConversionRule.new()
15
+ oneapi_attr_accessor :verified, FieldConversionRule.new('login.verified | TODO')
16
+
17
+ end
18
+
19
+ class OneApiError < OneApiModel
20
+
21
+ oneapi_attr_accessor :message_id, FieldConversionRule.new('requestError.serviceException.messageId | requestError.policyException.messageId')
22
+ oneapi_attr_accessor :text, FieldConversionRule.new('requestError.serviceException.text | requestError.policyException.text')
23
+ oneapi_attr_accessor :variables, FieldConversionRule.new('requestError.serviceException.variables | requestError.policyException.variables')
24
+
25
+ end
26
+
27
+ class GenericObject < OneApiModel
28
+
29
+ # FIXME: WIthout this it is not a valid model
30
+ oneapi_attr_accessor :ignore, FieldConversionRule.new(:ignore)
31
+
32
+ end
33
+
34
+ # ----------------------------------------------------------------------------------------------------
35
+ # Messaging:
36
+ # ----------------------------------------------------------------------------------------------------
37
+
38
+ class SMSRequest < OneApiModel
39
+
40
+ oneapi_attr_accessor :sender_address, FieldConversionRule.new(:senderAddress)
41
+ oneapi_attr_accessor :sender_name, FieldConversionRule.new(:senderName)
42
+ oneapi_attr_accessor :message, FieldConversionRule.new()
43
+ oneapi_attr_accessor :address, FieldConversionRule.new()
44
+ oneapi_attr_accessor :client_correlator, FieldConversionRule.new(:clientCorrelator)
45
+ oneapi_attr_accessor :notify_url, FieldConversionRule.new(:notifyUrl)
46
+ oneapi_attr_accessor :callback_data, FieldConversionRule.new()
47
+
48
+ end
49
+
50
+ class ResourceReference < OneApiModel
51
+
52
+ oneapi_attr_accessor :client_correlator, LastPartOfUrlFieldConversionRule.new('resourceReference.resourceURL')
53
+
54
+ end
55
+
56
+ class DeliveryInfo < OneApiModel
57
+
58
+ oneapi_attr_accessor :address, FieldConversionRule.new(:address)
59
+ oneapi_attr_accessor :delivery_status, FieldConversionRule.new(:deliveryStatus)
60
+
61
+ end
62
+
63
+ class DeliveryInfoList < OneApiModel
64
+
65
+ oneapi_attr_accessor :delivery_info, ObjectArrayConversionRule.new(DeliveryInfo, json_field_name='deliveryInfoList.deliveryInfo')
66
+
67
+ end
68
+
69
+ class DeliveryInfoNotification < OneApiModel
70
+
71
+ oneapi_attr_accessor :delivery_info, ObjectFieldConverter.new(DeliveryInfo, 'deliveryInfoNotification.deliveryInfo')
72
+ oneapi_attr_accessor :callback_data, FieldConversionRule.new('deliveryInfoNotification.callbackData')
73
+
74
+ end
75
+
76
+ # ----------------------------------------------------------------------------------------------------
77
+ # HLR:
78
+ # ----------------------------------------------------------------------------------------------------
79
+
80
+ class ServingMccMnc < OneApiModel
81
+
82
+ oneapi_attr_accessor :mcc, FieldConversionRule.new(:mcc)
83
+ oneapi_attr_accessor :mnc, FieldConversionRule.new(:mnc)
84
+
85
+ end
86
+
87
+ class TerminalRoamingExtendedData < OneApiModel
88
+
89
+ oneapi_attr_accessor :destination_address, FieldConversionRule.new('destinationAddress')
90
+ oneapi_attr_accessor :status_id, FieldConversionRule.new('statusId')
91
+ oneapi_attr_accessor :done_time, FieldConversionRule.new('doneTime')
92
+ oneapi_attr_accessor :price_per_message, FieldConversionRule.new('pricePerMessage')
93
+ oneapi_attr_accessor :mcc_mnc, FieldConversionRule.new('mccMnc')
94
+ oneapi_attr_accessor :serving_msc, FieldConversionRule.new('servingMsc')
95
+ oneapi_attr_accessor :censored_serving_msc, FieldConversionRule.new('censoredServingMsc')
96
+ oneapi_attr_accessor :gsm_error_code, FieldConversionRule.new('gsmErrorCode')
97
+ oneapi_attr_accessor :original_network_name, FieldConversionRule.new('originalNetworkName')
98
+ oneapi_attr_accessor :ported_network_name, FieldConversionRule.new('portedNetworkName')
99
+ oneapi_attr_accessor :serving_hlr, FieldConversionRule.new('servingHlr')
100
+ oneapi_attr_accessor :imsi, FieldConversionRule.new('imsi')
101
+ oneapi_attr_accessor :original_network_prefix, FieldConversionRule.new('originalNetworkPrefix')
102
+ oneapi_attr_accessor :original_country_prefix, FieldConversionRule.new('originalCountryPrefix')
103
+ oneapi_attr_accessor :original_country_name, FieldConversionRule.new('originalCountryName')
104
+ oneapi_attr_accessor :is_number_ported, FieldConversionRule.new('isNumberPorted')
105
+ oneapi_attr_accessor :ported_network_prefix, FieldConversionRule.new('portedNetworkPrefix')
106
+ oneapi_attr_accessor :ported_country_prefix, FieldConversionRule.new('portedCountryPrefix')
107
+ oneapi_attr_accessor :ported_country_name, FieldConversionRule.new('portedCountryName')
108
+ oneapi_attr_accessor :number_in_roaming, FieldConversionRule.new('numberInRoaming')
109
+
110
+ end
111
+
112
+ class TerminalRoamingStatus < OneApiModel
113
+
114
+ oneapi_attr_accessor :servingMccMnc, ObjectFieldConverter.new(ServingMccMnc, 'servingMccMnc')
115
+ oneapi_attr_accessor :address, FieldConversionRule.new()
116
+ oneapi_attr_accessor :currentRoaming, FieldConversionRule.new('currentRoaming')
117
+ oneapi_attr_accessor :resourceURL, FieldConversionRule.new('resourceURL')
118
+ oneapi_attr_accessor :retrievalStatus, FieldConversionRule.new('retrievalStatus')
119
+ oneapi_attr_accessor :callbackData, FieldConversionRule.new('callbackData')
120
+ oneapi_attr_accessor :extendedData, ObjectFieldConverter.new(TerminalRoamingExtendedData, 'extendedData')
121
+
122
+ end
123
+
124
+
125
+ class TerminalRoamingStatusNotification < OneApiModel
126
+
127
+ oneapi_attr_accessor :delivery_info, ObjectFieldConverter.new(TerminalRoamingStatus, 'terminalRoamingStatusList.roaming')
128
+ oneapi_attr_accessor :callback_data, FieldConversionRule.new('terminalRoamingStatusList.roaming.callbackData')
129
+
130
+ end
131
+
132
+ # ----------------------------------------------------------------------------------------------------
133
+ # Customer profile:
134
+ # ----------------------------------------------------------------------------------------------------
135
+
136
+ class CustomerProfile < OneApiModel
137
+
138
+ oneapi_attr_accessor :id, FieldConversionRule.new()
139
+ oneapi_attr_accessor :username, FieldConversionRule.new()
140
+ oneapi_attr_accessor :forename, FieldConversionRule.new()
141
+ oneapi_attr_accessor :surname, FieldConversionRule.new()
142
+ oneapi_attr_accessor :street, FieldConversionRule.new()
143
+ oneapi_attr_accessor :city, FieldConversionRule.new()
144
+ oneapi_attr_accessor :zip_code, FieldConversionRule.new('zipCode')
145
+ oneapi_attr_accessor :telephone, FieldConversionRule.new()
146
+ oneapi_attr_accessor :gsm, FieldConversionRule.new()
147
+ oneapi_attr_accessor :fax, FieldConversionRule.new()
148
+ oneapi_attr_accessor :email, FieldConversionRule.new()
149
+ oneapi_attr_accessor :msn, FieldConversionRule.new()
150
+ oneapi_attr_accessor :skype, FieldConversionRule.new()
151
+ oneapi_attr_accessor :country_id, FieldConversionRule.new('countryId')
152
+ oneapi_attr_accessor :timezone_id, FieldConversionRule.new('timezoneId')
153
+ oneapi_attr_accessor :primary_language_id, FieldConversionRule.new('primaryLanguageId')
154
+ oneapi_attr_accessor :secondary_language_id, FieldConversionRule.new('secondaryLanguageId')
155
+
156
+ end
157
+
158
+ class Currency < OneApiModel
159
+
160
+ oneapi_attr_accessor :id, FieldConversionRule.new()
161
+ oneapi_attr_accessor :currency_name, FieldConversionRule.new('currencyName')
162
+ oneapi_attr_accessor :symbol, FieldConversionRule.new()
163
+
164
+ end
165
+
166
+ class Currency < OneApiModel
167
+
168
+ oneapi_attr_accessor :id, FieldConversionRule.new()
169
+ oneapi_attr_accessor :currency_name, FieldConversionRule.new('currencyName')
170
+ oneapi_attr_accessor :symbol, FieldConversionRule.new()
171
+
172
+ end
173
+
174
+ class AccountBalance < OneApiModel
175
+
176
+ oneapi_attr_accessor :balance, FieldConversionRule.new()
177
+ oneapi_attr_accessor :currency, ObjectFieldConverter.new(Currency)
178
+
179
+ end
180
+
181
+ # ----------------------------------------------------------------------------------------------------
182
+ # Inbound:
183
+ # ----------------------------------------------------------------------------------------------------
184
+
185
+ class InboundSmsMessage < OneApiModel
186
+
187
+ oneapi_attr_accessor :date_time, FieldConversionRule.new('dateTime')
188
+ oneapi_attr_accessor :destination_address, FieldConversionRule.new('destinationAddress')
189
+ oneapi_attr_accessor :message_id, FieldConversionRule.new('messageId')
190
+ oneapi_attr_accessor :message, FieldConversionRule.new('message')
191
+ oneapi_attr_accessor :resource_url, FieldConversionRule.new('resourceURL')
192
+ oneapi_attr_accessor :sender_address, FieldConversionRule.new('senderAddress')
193
+
194
+ end
195
+
196
+ class InboundSmsMessages < OneApiModel
197
+
198
+ oneapi_attr_accessor :inbound_sms_message, ObjectArrayConversionRule.new(InboundSmsMessage, 'inboundSMSMessageList.inboundSMSMessage')
199
+ oneapi_attr_accessor :number_of_messages_in_this_batch, FieldConversionRule.new('inboundSMSMessageList.numberOfMessagesInThisBatch')
200
+ oneapi_attr_accessor :total_number_of_pending_messages, FieldConversionRule.new('inboundSMSMessageList.totalNumberOfPendingMessages')
201
+ oneapi_attr_accessor :callback_data, FieldConversionRule.new('inboundSMSMessageList.callbackData')
202
+
203
+ end
204
+
205
+ end
@@ -0,0 +1,174 @@
1
+ =begin
2
+ require_relative 'rubygems'
3
+ require_relative 'ruby-debug'
4
+ =end
5
+
6
+ require_relative 'utils'
7
+
8
+ module OneApi
9
+
10
+ class FieldConversionRule
11
+
12
+ attr_accessor :object_field_name
13
+ attr_accessor :json_field_name
14
+
15
+ def initialize(json_field_name=nil)
16
+ @json_field_name = json_field_name
17
+ @object_field_name = nil
18
+ end
19
+
20
+ def from_json(value)
21
+ value
22
+ end
23
+
24
+ def to_json(value)
25
+ value
26
+ end
27
+
28
+ end
29
+
30
+
31
+ class ObjectFieldConverter < FieldConversionRule
32
+
33
+ def initialize(classs, json_field_name=nil)
34
+ super(json_field_name)
35
+ @classs = classs
36
+ end
37
+
38
+ def from_json(value)
39
+ if value == nil
40
+ return nil
41
+ end
42
+
43
+ return Conversions.from_json(@classs, value, is_error=nil)
44
+ end
45
+
46
+ def to_json(value)
47
+ # TODO
48
+ end
49
+
50
+ end
51
+
52
+ class ObjectArrayConversionRule < FieldConversionRule
53
+
54
+ def initialize(classs, json_field_name=nil)
55
+ super(json_field_name)
56
+ @classs = classs
57
+ end
58
+
59
+ def from_json(values)
60
+ if not values
61
+ return []
62
+ end
63
+
64
+ result = []
65
+
66
+ for value in values
67
+ result.push(Conversions.from_json(@classs, value, is_error=nil))
68
+ end
69
+
70
+ return result
71
+ end
72
+
73
+ def to_json(value)
74
+ # TODO
75
+ end
76
+
77
+ end
78
+
79
+ class LastPartOfUrlFieldConversionRule < FieldConversionRule
80
+
81
+ def initialize(json_field_name=nil)
82
+ super(json_field_name)
83
+ end
84
+
85
+ def from_json(value)
86
+ if ! value
87
+ return nil
88
+ end
89
+
90
+ parts = value.split('/')
91
+
92
+ parts[-1]
93
+ end
94
+
95
+ def to_json(value)
96
+ value
97
+ end
98
+
99
+ end
100
+
101
+ class Conversions
102
+
103
+ def self.from_json(classs, json, is_error=nil)
104
+ object = classs.new
105
+
106
+ Conversions.fill_from_json(object, json, is_error)
107
+
108
+ object
109
+ end
110
+
111
+ def self.fill_from_json(object, json, is_error=nil)
112
+ if is_error
113
+ object.exception = Conversions.from_json(OneApiError, json, false)
114
+ return
115
+ end
116
+
117
+ json = JSONUtils.get_json(json)
118
+ conversion_rules = OneApiAccessorModifier.get_field_conversion_rules(object.class)
119
+ for conversion_rule in conversion_rules
120
+ json_value = JSONUtils.get(json, conversion_rule.json_field_name)
121
+ value = conversion_rule.from_json(json_value)
122
+ object.instance_variable_set("@#{conversion_rule.object_field_name}", value)
123
+ end
124
+ end
125
+
126
+ def self.to_json
127
+ # TODO(TK)
128
+ end
129
+
130
+ end
131
+
132
+ module OneApiAccessorModifier
133
+
134
+ @@field_conversion_rules = {}
135
+
136
+ def oneapi_attr_accessor(attr, field_conversion_rule)
137
+ attr_accessor attr
138
+
139
+ field_conversion_rule.object_field_name = attr
140
+
141
+ if Utils.empty(field_conversion_rule.json_field_name)
142
+ field_conversion_rule.json_field_name = attr
143
+ end
144
+
145
+ if not @@field_conversion_rules.has_key? self then
146
+ @@field_conversion_rules[self] = []
147
+ end
148
+
149
+ @@field_conversion_rules[self].push field_conversion_rule
150
+ #puts "field_conversion_rules is now #{@@field_conversion_rules}"
151
+ end
152
+
153
+ def OneApiAccessorModifier.get_field_conversion_rules(classs)
154
+ @@field_conversion_rules[classs]
155
+ end
156
+
157
+ end
158
+
159
+ class OneApiModel
160
+
161
+ extend OneApiAccessorModifier
162
+
163
+ attr_accessor :exception
164
+
165
+ def initialize
166
+ end
167
+
168
+ def is_success
169
+ return @exception == nil
170
+ end
171
+
172
+ end
173
+
174
+ end
@@ -0,0 +1,164 @@
1
+ #require 'pry'
2
+ require 'json'
3
+ require 'socket'
4
+
5
+ module OneApi
6
+
7
+ class JSONUtils
8
+
9
+ def self.get_json(json)
10
+ if json.instance_of? String
11
+ return JSON.parse(json)
12
+ end
13
+
14
+ return json
15
+ end
16
+
17
+ def self.get(json, field)
18
+ json = JSONUtils.get_json(json)
19
+
20
+ if not field
21
+ return nil
22
+ end
23
+
24
+ if field.instance_of? Symbol
25
+ field = field.to_s
26
+ end
27
+
28
+ if field.include?('|') then
29
+ field_parts = field.split('|')
30
+ for field_part in field_parts
31
+ value = JSONUtils.get(json, field_part.strip)
32
+ if value
33
+ return value
34
+ end
35
+ end
36
+ return nil
37
+ end
38
+
39
+ result = nil
40
+ parts = field.split('.')
41
+ result = json
42
+ for part in parts
43
+ if result == nil
44
+ return nil
45
+ end
46
+
47
+ if part.to_i.to_s == part
48
+ # Int index => array:
49
+ result = result[part.to_i]
50
+ else
51
+ # Hash:
52
+ result = result[part]
53
+ end
54
+ end
55
+
56
+ result
57
+ end
58
+
59
+ end
60
+
61
+ class Utils
62
+
63
+ def self.empty(obj)
64
+ if obj == nil
65
+ return true
66
+ end
67
+
68
+ if obj.instance_of? Hash or obj.instance_of? Array or obj.instance_of? String
69
+ return obj.size == 0
70
+ end
71
+
72
+ return obj == 0
73
+ end
74
+
75
+ def self.get_random_string(length, chars)
76
+ if not length
77
+ raise "Invalid random string length: #{length}"
78
+ end
79
+ if not chars
80
+ raise "Invalid random chars: #{chars}"
81
+ end
82
+
83
+ result = ''
84
+
85
+ for i in 0..length
86
+ result += chars[rand(chars.length - 1), 1]
87
+ end
88
+
89
+ result
90
+ end
91
+
92
+ def self.get_random_alphanumeric_string(length=10)
93
+ get_random_string(length, 'qwertzuiopasdfghjklyxcvbnm123456789')
94
+ end
95
+
96
+ end
97
+
98
+ # Web server, to be used only in examples
99
+ class DummyWebServer
100
+
101
+ attr_accessor :requests
102
+
103
+ def initialize(ip_address, port)
104
+ @webserver = TCPServer.new(ip_address, port)
105
+ @session = nil
106
+ @requests = []
107
+ end
108
+
109
+ def start(seconds)
110
+ Thread.new {
111
+ while (@session = @webserver.accept)
112
+ request_string = @session.sysread 10000
113
+ @session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
114
+ @session.print("OK")
115
+ @session.close
116
+ @requests.push(parse_request_string request_string)
117
+ end
118
+ }
119
+
120
+ sleep(seconds)
121
+
122
+ @webserver.close
123
+ if @session != nil
124
+ begin
125
+ @session.close
126
+ rescue
127
+ end
128
+ end
129
+ end
130
+
131
+ def parse_request_string request_string
132
+ lines = request_string.split(/\n/)
133
+ method, url, http_version = lines[0].split(' ')
134
+ headers = {}
135
+ body = nil
136
+ for line in lines[1,lines.length]
137
+ if body == nil
138
+ if line.strip == ''
139
+ body = ''
140
+ else
141
+ index = line.index ':'
142
+ key = line[0,index].strip
143
+ value = line[index + 1, line.length].strip
144
+ headers[key] = value
145
+ end
146
+ end
147
+
148
+ if body != nil
149
+ body += line + "\n"
150
+ end
151
+ end
152
+ return method, url, headers, body.strip
153
+ end
154
+
155
+ end
156
+
157
+ =begin
158
+ dummy_web_server = DummyWebServer.new('localhost', 2000)
159
+ dummy_web_server.start 10
160
+ puts dummy_web_server.requests
161
+ =end
162
+
163
+
164
+ end