moesif_api 1.0.3 → 1.0.4
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 +4 -4
- data/LICENSE +216 -24
- data/README.md +124 -117
- data/lib/moesif_api/api_helper.rb +150 -150
- data/lib/moesif_api/configuration.rb +17 -17
- data/lib/moesif_api/controllers/api_controller.rb +87 -87
- data/lib/moesif_api/controllers/health_controller.rb +52 -52
- data/lib/moesif_api/exceptions/api_exception.rb +16 -16
- data/lib/moesif_api/http/http_call_back.rb +16 -16
- data/lib/moesif_api/http/http_client.rb +112 -112
- data/lib/moesif_api/http/http_context.rb +14 -14
- data/lib/moesif_api/http/http_method_enum.rb +7 -7
- data/lib/moesif_api/http/http_request.rb +28 -28
- data/lib/moesif_api/http/http_response.rb +19 -19
- data/lib/moesif_api/http/unirest_client.rb +35 -35
- data/lib/moesif_api/models/base_model.rb +32 -32
- data/lib/moesif_api/models/event_model.rb +71 -71
- data/lib/moesif_api/models/event_request_model.rb +90 -90
- data/lib/moesif_api/models/event_response_model.rb +72 -72
- data/lib/moesif_api/models/status_model.rb +44 -44
- data/lib/moesif_api/moesif_api_client.rb +22 -22
- data/lib/moesif_api.rb +33 -33
- data/test/controllers/controller_test_base.rb +28 -28
- data/test/http_response_catcher.rb +16 -16
- data/test/test_helper.rb +94 -94
- metadata +4 -4
@@ -1,17 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class HttpCallBack
|
5
|
-
# A controller will call this method before making an HTTP Request.
|
6
|
-
# @param [HttpRequest] The HttpRequest object which the HttpClient will execute.
|
7
|
-
def on_before_request(http_request)
|
8
|
-
raise NotImplementedError, "This method needs to be implemented in a child class."
|
9
|
-
end
|
10
|
-
|
11
|
-
# A controller will call this method after making an HTTP Request.
|
12
|
-
# @param [HttpResponse] The HttpResponse object received from the HttpClient.
|
13
|
-
def on_after_response(http_response)
|
14
|
-
raise NotImplementedError, "This method needs to be implemented in a child class."
|
15
|
-
end
|
16
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class HttpCallBack
|
5
|
+
# A controller will call this method before making an HTTP Request.
|
6
|
+
# @param [HttpRequest] The HttpRequest object which the HttpClient will execute.
|
7
|
+
def on_before_request(http_request)
|
8
|
+
raise NotImplementedError, "This method needs to be implemented in a child class."
|
9
|
+
end
|
10
|
+
|
11
|
+
# A controller will call this method after making an HTTP Request.
|
12
|
+
# @param [HttpResponse] The HttpResponse object received from the HttpClient.
|
13
|
+
def on_after_response(http_response)
|
14
|
+
raise NotImplementedError, "This method needs to be implemented in a child class."
|
15
|
+
end
|
16
|
+
end
|
17
17
|
end
|
@@ -1,112 +1,112 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class HttpClient
|
5
|
-
# Execute an HttpRequest when the response is expected to be a string.
|
6
|
-
# @param [HttpRequest] The HttpRequest to be executed.
|
7
|
-
def execute_as_string(http_request)
|
8
|
-
raise NotImplementedError, "This method needs to be implemented in a child class."
|
9
|
-
end
|
10
|
-
|
11
|
-
# Execute an HttpRequest when the response is expected to be binary.
|
12
|
-
# @param [HttpRequest] The HttpRequest to be executed.
|
13
|
-
def execute_as_binary(http_request)
|
14
|
-
raise NotImplementedError, "This method needs to be implemented in a child class."
|
15
|
-
end
|
16
|
-
|
17
|
-
# Converts the HTTP Response from the client to an HttpResponse object.
|
18
|
-
# @param [Dynamic] The response object received from the client.
|
19
|
-
def convert_response(response)
|
20
|
-
raise NotImplementedError, "This method needs to be implemented in a child class."
|
21
|
-
end
|
22
|
-
|
23
|
-
# Get a GET HttpRequest object.
|
24
|
-
# @param [String] The URL to send the request to.
|
25
|
-
# @param [Hash, Optional] The headers for the HTTP Request.
|
26
|
-
# @param [String, Optional] Username for Basic Auth requests.
|
27
|
-
# @param [String, Optional] Password for Basic Auth requests.
|
28
|
-
def get(query_url,
|
29
|
-
headers: nil,
|
30
|
-
username: nil,
|
31
|
-
password: nil)
|
32
|
-
return HttpRequest.new(HttpMethodEnum::GET,
|
33
|
-
query_url,
|
34
|
-
headers: headers,
|
35
|
-
username: username,
|
36
|
-
password: password)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Get a POST HttpRequest object.
|
40
|
-
# @param [String] The URL to send the request to.
|
41
|
-
# @param [Hash, Optional] The headers for the HTTP Request.
|
42
|
-
# @param [Hash, Optional] The parameters for the HTTP Request.
|
43
|
-
# @param [String, Optional] Username for Basic Auth requests.
|
44
|
-
# @param [String, Optional] Password for Basic Auth requests.
|
45
|
-
def post(query_url,
|
46
|
-
headers: nil,
|
47
|
-
parameters: nil,
|
48
|
-
username: nil,
|
49
|
-
password: nil)
|
50
|
-
return HttpRequest.new(HttpMethodEnum::POST,
|
51
|
-
query_url,
|
52
|
-
headers: headers,
|
53
|
-
parameters: parameters,
|
54
|
-
username: username,
|
55
|
-
password: password)
|
56
|
-
end
|
57
|
-
|
58
|
-
# Get a PUT HttpRequest object.
|
59
|
-
# @param [String] The URL to send the request to.
|
60
|
-
# @param [Hash, Optional] The headers for the HTTP Request.
|
61
|
-
# @param [Hash, Optional] The parameters for the HTTP Request.
|
62
|
-
# @param [String, Optional] Username for Basic Auth requests.
|
63
|
-
# @param [String, Optional] Password for Basic Auth requests.
|
64
|
-
def put(query_url,
|
65
|
-
headers: nil,
|
66
|
-
parameters: nil,
|
67
|
-
username: nil,
|
68
|
-
password: nil)
|
69
|
-
return HttpRequest.new(HttpMethodEnum::PUT,
|
70
|
-
query_url,
|
71
|
-
headers: headers,
|
72
|
-
parameters: parameters,
|
73
|
-
username: username,
|
74
|
-
password: password)
|
75
|
-
end
|
76
|
-
|
77
|
-
# Get a PATCH HttpRequest object.
|
78
|
-
# @param [String] The URL to send the request to.
|
79
|
-
# @param [Hash, Optional] The headers for the HTTP Request.
|
80
|
-
# @param [Hash, Optional] The parameters for the HTTP Request.
|
81
|
-
# @param [String, Optional] Username for Basic Auth requests.
|
82
|
-
# @param [String, Optional] Password for Basic Auth requests.
|
83
|
-
def patch(query_url,
|
84
|
-
headers: nil,
|
85
|
-
parameters: nil,
|
86
|
-
username: nil,
|
87
|
-
password: nil)
|
88
|
-
return HttpRequest.new(HttpMethodEnum::PATCH,
|
89
|
-
query_url,
|
90
|
-
headers: headers,
|
91
|
-
parameters: parameters,
|
92
|
-
username: username,
|
93
|
-
password: password)
|
94
|
-
end
|
95
|
-
|
96
|
-
# Get a DELETE HttpRequest object.
|
97
|
-
# @param [String] The URL to send the request to.
|
98
|
-
# @param [Hash, Optional] The headers for the HTTP Request.
|
99
|
-
# @param [String, Optional] Username for Basic Auth requests.
|
100
|
-
# @param [String, Optional] Password for Basic Auth requests.
|
101
|
-
def delete(query_url,
|
102
|
-
headers: nil,
|
103
|
-
username: nil,
|
104
|
-
password: nil)
|
105
|
-
return HttpRequest.new(HttpMethodEnum::DELETE,
|
106
|
-
query_url,
|
107
|
-
headers: headers,
|
108
|
-
username: username,
|
109
|
-
password: password)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class HttpClient
|
5
|
+
# Execute an HttpRequest when the response is expected to be a string.
|
6
|
+
# @param [HttpRequest] The HttpRequest to be executed.
|
7
|
+
def execute_as_string(http_request)
|
8
|
+
raise NotImplementedError, "This method needs to be implemented in a child class."
|
9
|
+
end
|
10
|
+
|
11
|
+
# Execute an HttpRequest when the response is expected to be binary.
|
12
|
+
# @param [HttpRequest] The HttpRequest to be executed.
|
13
|
+
def execute_as_binary(http_request)
|
14
|
+
raise NotImplementedError, "This method needs to be implemented in a child class."
|
15
|
+
end
|
16
|
+
|
17
|
+
# Converts the HTTP Response from the client to an HttpResponse object.
|
18
|
+
# @param [Dynamic] The response object received from the client.
|
19
|
+
def convert_response(response)
|
20
|
+
raise NotImplementedError, "This method needs to be implemented in a child class."
|
21
|
+
end
|
22
|
+
|
23
|
+
# Get a GET HttpRequest object.
|
24
|
+
# @param [String] The URL to send the request to.
|
25
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
26
|
+
# @param [String, Optional] Username for Basic Auth requests.
|
27
|
+
# @param [String, Optional] Password for Basic Auth requests.
|
28
|
+
def get(query_url,
|
29
|
+
headers: nil,
|
30
|
+
username: nil,
|
31
|
+
password: nil)
|
32
|
+
return HttpRequest.new(HttpMethodEnum::GET,
|
33
|
+
query_url,
|
34
|
+
headers: headers,
|
35
|
+
username: username,
|
36
|
+
password: password)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Get a POST HttpRequest object.
|
40
|
+
# @param [String] The URL to send the request to.
|
41
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
42
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
43
|
+
# @param [String, Optional] Username for Basic Auth requests.
|
44
|
+
# @param [String, Optional] Password for Basic Auth requests.
|
45
|
+
def post(query_url,
|
46
|
+
headers: nil,
|
47
|
+
parameters: nil,
|
48
|
+
username: nil,
|
49
|
+
password: nil)
|
50
|
+
return HttpRequest.new(HttpMethodEnum::POST,
|
51
|
+
query_url,
|
52
|
+
headers: headers,
|
53
|
+
parameters: parameters,
|
54
|
+
username: username,
|
55
|
+
password: password)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Get a PUT HttpRequest object.
|
59
|
+
# @param [String] The URL to send the request to.
|
60
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
61
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
62
|
+
# @param [String, Optional] Username for Basic Auth requests.
|
63
|
+
# @param [String, Optional] Password for Basic Auth requests.
|
64
|
+
def put(query_url,
|
65
|
+
headers: nil,
|
66
|
+
parameters: nil,
|
67
|
+
username: nil,
|
68
|
+
password: nil)
|
69
|
+
return HttpRequest.new(HttpMethodEnum::PUT,
|
70
|
+
query_url,
|
71
|
+
headers: headers,
|
72
|
+
parameters: parameters,
|
73
|
+
username: username,
|
74
|
+
password: password)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Get a PATCH HttpRequest object.
|
78
|
+
# @param [String] The URL to send the request to.
|
79
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
80
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
81
|
+
# @param [String, Optional] Username for Basic Auth requests.
|
82
|
+
# @param [String, Optional] Password for Basic Auth requests.
|
83
|
+
def patch(query_url,
|
84
|
+
headers: nil,
|
85
|
+
parameters: nil,
|
86
|
+
username: nil,
|
87
|
+
password: nil)
|
88
|
+
return HttpRequest.new(HttpMethodEnum::PATCH,
|
89
|
+
query_url,
|
90
|
+
headers: headers,
|
91
|
+
parameters: parameters,
|
92
|
+
username: username,
|
93
|
+
password: password)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Get a DELETE HttpRequest object.
|
97
|
+
# @param [String] The URL to send the request to.
|
98
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
99
|
+
# @param [String, Optional] Username for Basic Auth requests.
|
100
|
+
# @param [String, Optional] Password for Basic Auth requests.
|
101
|
+
def delete(query_url,
|
102
|
+
headers: nil,
|
103
|
+
username: nil,
|
104
|
+
password: nil)
|
105
|
+
return HttpRequest.new(HttpMethodEnum::DELETE,
|
106
|
+
query_url,
|
107
|
+
headers: headers,
|
108
|
+
username: username,
|
109
|
+
password: password)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class HttpContext
|
5
|
-
attr_accessor :request, :response
|
6
|
-
|
7
|
-
# The constructor.
|
8
|
-
# @param [HttpRequest] An HttpRequest object representing the HTTP request.
|
9
|
-
# @param [HttpResponse] An HttpResponse object representing the HTTP response.
|
10
|
-
def initialize(request, response)
|
11
|
-
@request = request
|
12
|
-
@response = response
|
13
|
-
end
|
14
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class HttpContext
|
5
|
+
attr_accessor :request, :response
|
6
|
+
|
7
|
+
# The constructor.
|
8
|
+
# @param [HttpRequest] An HttpRequest object representing the HTTP request.
|
9
|
+
# @param [HttpResponse] An HttpResponse object representing the HTTP response.
|
10
|
+
def initialize(request, response)
|
11
|
+
@request = request
|
12
|
+
@response = response
|
13
|
+
end
|
14
|
+
end
|
15
15
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class HttpMethodEnum
|
5
|
-
HTTPMETHODENUM = [GET = "GET", POST = "POST", PUT = "PUT", PATCH = "PATCH", DELETE = "DELETE"]
|
6
|
-
end
|
7
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class HttpMethodEnum
|
5
|
+
HTTPMETHODENUM = [GET = "GET", POST = "POST", PUT = "PUT", PATCH = "PATCH", DELETE = "DELETE"]
|
6
|
+
end
|
7
|
+
end
|
@@ -1,28 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class HttpRequest
|
5
|
-
attr_accessor :http_method, :query_url, :headers, :parameters, :username, :password
|
6
|
-
|
7
|
-
# The constructor.
|
8
|
-
# @param [HttpMethodEnum] The HTTP method.
|
9
|
-
# @param [String] The URL to send the request to.
|
10
|
-
# @param [Hash, Optional] The headers for the HTTP Request.
|
11
|
-
# @param [Hash, Optional] The parameters for the HTTP Request.
|
12
|
-
# @param [String, Optional] Username for Basic Auth requests.
|
13
|
-
# @param [String, Optional] Password for Basic Auth requests.
|
14
|
-
def initialize(http_method,
|
15
|
-
query_url,
|
16
|
-
headers: nil,
|
17
|
-
parameters: nil,
|
18
|
-
username: nil,
|
19
|
-
password: nil)
|
20
|
-
@http_method = http_method
|
21
|
-
@query_url = query_url
|
22
|
-
@headers = headers
|
23
|
-
@parameters = parameters
|
24
|
-
@username = username
|
25
|
-
@password = password
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class HttpRequest
|
5
|
+
attr_accessor :http_method, :query_url, :headers, :parameters, :username, :password
|
6
|
+
|
7
|
+
# The constructor.
|
8
|
+
# @param [HttpMethodEnum] The HTTP method.
|
9
|
+
# @param [String] The URL to send the request to.
|
10
|
+
# @param [Hash, Optional] The headers for the HTTP Request.
|
11
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
12
|
+
# @param [String, Optional] Username for Basic Auth requests.
|
13
|
+
# @param [String, Optional] Password for Basic Auth requests.
|
14
|
+
def initialize(http_method,
|
15
|
+
query_url,
|
16
|
+
headers: nil,
|
17
|
+
parameters: nil,
|
18
|
+
username: nil,
|
19
|
+
password: nil)
|
20
|
+
@http_method = http_method
|
21
|
+
@query_url = query_url
|
22
|
+
@headers = headers
|
23
|
+
@parameters = parameters
|
24
|
+
@username = username
|
25
|
+
@password = password
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,19 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class HttpResponse
|
5
|
-
attr_accessor :status_code, :headers, :raw_body
|
6
|
-
|
7
|
-
# The constructor.
|
8
|
-
# @param [Integer] The status code returned by the server.
|
9
|
-
# @param [Hash] The headers sent by the server in the response.
|
10
|
-
# @param [String] The raw body of the response.
|
11
|
-
def initialize(status_code,
|
12
|
-
headers,
|
13
|
-
raw_body)
|
14
|
-
@status_code = status_code
|
15
|
-
@headers = headers
|
16
|
-
@raw_body = raw_body
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class HttpResponse
|
5
|
+
attr_accessor :status_code, :headers, :raw_body
|
6
|
+
|
7
|
+
# The constructor.
|
8
|
+
# @param [Integer] The status code returned by the server.
|
9
|
+
# @param [Hash] The headers sent by the server in the response.
|
10
|
+
# @param [String] The raw body of the response.
|
11
|
+
def initialize(status_code,
|
12
|
+
headers,
|
13
|
+
raw_body)
|
14
|
+
@status_code = status_code
|
15
|
+
@headers = headers
|
16
|
+
@raw_body = raw_body
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,36 +1,36 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class UnirestClient < HttpClient
|
5
|
-
# Method overridden from HttpClient.
|
6
|
-
def execute_as_string(http_request)
|
7
|
-
if http_request.username || http_request.password
|
8
|
-
auth = {:user=>http_request.username, :password=>http_request.password}
|
9
|
-
end
|
10
|
-
|
11
|
-
response = Unirest.method(http_request.http_method.downcase).call(http_request.query_url,
|
12
|
-
headers: http_request.headers, parameters: http_request.parameters,
|
13
|
-
auth: auth)
|
14
|
-
|
15
|
-
return convert_response(response)
|
16
|
-
end
|
17
|
-
|
18
|
-
# Method overridden from HttpClient.
|
19
|
-
def execute_as_binary(http_request)
|
20
|
-
if http_request.username || http_request.password
|
21
|
-
auth = {:user=>http_request.username, :password=>http_request.password}
|
22
|
-
end
|
23
|
-
|
24
|
-
response = Unirest.method(http_request.http_method.downcase).call(http_request.query_url,
|
25
|
-
headers: http_request.headers, parameters: http_request.parameters,
|
26
|
-
auth: auth)
|
27
|
-
|
28
|
-
return convert_response(response)
|
29
|
-
end
|
30
|
-
|
31
|
-
# Method overridden from HttpClient.
|
32
|
-
def convert_response(response)
|
33
|
-
return HttpResponse.new(response.code, response.headers.dup, response.raw_body.dup)
|
34
|
-
end
|
35
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class UnirestClient < HttpClient
|
5
|
+
# Method overridden from HttpClient.
|
6
|
+
def execute_as_string(http_request)
|
7
|
+
if http_request.username || http_request.password
|
8
|
+
auth = {:user=>http_request.username, :password=>http_request.password}
|
9
|
+
end
|
10
|
+
|
11
|
+
response = Unirest.method(http_request.http_method.downcase).call(http_request.query_url,
|
12
|
+
headers: http_request.headers, parameters: http_request.parameters,
|
13
|
+
auth: auth)
|
14
|
+
|
15
|
+
return convert_response(response)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Method overridden from HttpClient.
|
19
|
+
def execute_as_binary(http_request)
|
20
|
+
if http_request.username || http_request.password
|
21
|
+
auth = {:user=>http_request.username, :password=>http_request.password}
|
22
|
+
end
|
23
|
+
|
24
|
+
response = Unirest.method(http_request.http_method.downcase).call(http_request.query_url,
|
25
|
+
headers: http_request.headers, parameters: http_request.parameters,
|
26
|
+
auth: auth)
|
27
|
+
|
28
|
+
return convert_response(response)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Method overridden from HttpClient.
|
32
|
+
def convert_response(response)
|
33
|
+
return HttpResponse.new(response.code, response.headers.dup, response.raw_body.dup)
|
34
|
+
end
|
35
|
+
end
|
36
36
|
end
|
@@ -1,32 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module MoesifApi
|
4
|
-
class BaseModel
|
5
|
-
# Returns a Hash representation of the current object
|
6
|
-
def to_hash
|
7
|
-
hash = {}
|
8
|
-
self.instance_variables.each do |name|
|
9
|
-
value = self.instance_variable_get(name)
|
10
|
-
name = name[1..-1]
|
11
|
-
key = self.class.names.key?(name) ? self.class.names[name] : name
|
12
|
-
if value.instance_of? Array
|
13
|
-
hash[key] = value.map{|v| v.kind_of?(BaseModel) ? v.to_hash : v}
|
14
|
-
elsif value.instance_of? Hash
|
15
|
-
hash[key] = {}
|
16
|
-
value.each do |k, v|
|
17
|
-
hash[key][k] = v.kind_of?(BaseModel) ? v.to_hash : v
|
18
|
-
end
|
19
|
-
else
|
20
|
-
hash[key] = value.kind_of?(BaseModel) ? value.to_hash : value
|
21
|
-
end
|
22
|
-
end
|
23
|
-
hash
|
24
|
-
end
|
25
|
-
|
26
|
-
# Returns a JSON representation of the curent object
|
27
|
-
def to_json(options = {})
|
28
|
-
hash = to_hash
|
29
|
-
hash.to_json(options)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class BaseModel
|
5
|
+
# Returns a Hash representation of the current object
|
6
|
+
def to_hash
|
7
|
+
hash = {}
|
8
|
+
self.instance_variables.each do |name|
|
9
|
+
value = self.instance_variable_get(name)
|
10
|
+
name = name[1..-1]
|
11
|
+
key = self.class.names.key?(name) ? self.class.names[name] : name
|
12
|
+
if value.instance_of? Array
|
13
|
+
hash[key] = value.map{|v| v.kind_of?(BaseModel) ? v.to_hash : v}
|
14
|
+
elsif value.instance_of? Hash
|
15
|
+
hash[key] = {}
|
16
|
+
value.each do |k, v|
|
17
|
+
hash[key][k] = v.kind_of?(BaseModel) ? v.to_hash : v
|
18
|
+
end
|
19
|
+
else
|
20
|
+
hash[key] = value.kind_of?(BaseModel) ? value.to_hash : value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
hash
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns a JSON representation of the curent object
|
27
|
+
def to_json(options = {})
|
28
|
+
hash = to_hash
|
29
|
+
hash.to_json(options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|