moesif_api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+
2
+
3
+ module MoesifApi
4
+ class APIException < StandardError
5
+ attr_reader :context, :response_code
6
+
7
+ # The constructor.
8
+ # @param [String] The reason for raising an exception
9
+ # @param [HttpContext] The HttpContext of the API call.
10
+ def initialize(reason, context)
11
+ super(reason)
12
+ @context = context
13
+ @response_code = context.response.status_code
14
+ end
15
+ end
16
+ end
@@ -0,0 +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
17
+ end
@@ -0,0 +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
@@ -0,0 +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
15
+ end
@@ -0,0 +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
@@ -0,0 +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
@@ -0,0 +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
@@ -0,0 +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
36
+ end
@@ -0,0 +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
@@ -0,0 +1,71 @@
1
+
2
+
3
+ module MoesifApi
4
+ class EventModel < BaseModel
5
+ # API request object
6
+ # @return [EventRequestModel]
7
+ attr_accessor :request
8
+
9
+ # API response Object
10
+ # @return [ResponseModel]
11
+ attr_accessor :response
12
+
13
+ # End user's auth/session token
14
+ # @return [String]
15
+ attr_accessor :session_token
16
+
17
+ # comma separated list of tags, see documentation
18
+ # @return [String]
19
+ attr_accessor :tags
20
+
21
+ # End user's user_id string from your app
22
+ # @return [String]
23
+ attr_accessor :user_id
24
+
25
+ # A mapping from model property names to API property names
26
+ def self.names
27
+ if @hash.nil?
28
+ @hash = {}
29
+ @hash["request"] = "request"
30
+ @hash["response"] = "response"
31
+ @hash["session_token"] = "session_token"
32
+ @hash["tags"] = "tags"
33
+ @hash["user_id"] = "user_id"
34
+ end
35
+ @hash
36
+ end
37
+
38
+ def initialize(request = nil,
39
+ response = nil,
40
+ session_token = nil,
41
+ tags = nil,
42
+ user_id = nil)
43
+ @request = request
44
+ @response = response
45
+ @session_token = session_token
46
+ @tags = tags
47
+ @user_id = user_id
48
+ end
49
+
50
+ # Creates an instance of the object from a hash
51
+ def self.from_hash(hash)
52
+ if hash == nil
53
+ nil
54
+ else
55
+ # Extract variables from the hash
56
+ request = EventRequestModel.from_hash(hash["request"]) if hash["request"]
57
+ response = ResponseModel.from_hash(hash["response"]) if hash["response"]
58
+ session_token = hash["session_token"]
59
+ tags = hash["tags"]
60
+ user_id = hash["user_id"]
61
+
62
+ # Create object from extracted values
63
+ EventModel.new(request,
64
+ response,
65
+ session_token,
66
+ tags,
67
+ user_id)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,90 @@
1
+
2
+
3
+ require 'date'
4
+ module MoesifApi
5
+ class EventRequestModel < BaseModel
6
+ # Time when request was made
7
+ # @return [DateTime]
8
+ attr_accessor :time
9
+
10
+ # full uri of request such as https://www.example.com/my_path?param=1
11
+ # @return [String]
12
+ attr_accessor :uri
13
+
14
+ # verb of the API request such as GET or POST
15
+ # @return [String]
16
+ attr_accessor :verb
17
+
18
+ # Key/Value map of request headers
19
+ # @return [Object]
20
+ attr_accessor :headers
21
+
22
+ # Optionally tag the call with your API or App version
23
+ # @return [String]
24
+ attr_accessor :api_version
25
+
26
+ # IP Address of the client if known.
27
+ # @return [String]
28
+ attr_accessor :ip_address
29
+
30
+ # Request body
31
+ # @return [Object]
32
+ attr_accessor :body
33
+
34
+ # A mapping from model property names to API property names
35
+ def self.names
36
+ if @hash.nil?
37
+ @hash = {}
38
+ @hash["time"] = "time"
39
+ @hash["uri"] = "uri"
40
+ @hash["verb"] = "verb"
41
+ @hash["headers"] = "headers"
42
+ @hash["api_version"] = "api_version"
43
+ @hash["ip_address"] = "ip_address"
44
+ @hash["body"] = "body"
45
+ end
46
+ @hash
47
+ end
48
+
49
+ def initialize(time = nil,
50
+ uri = nil,
51
+ verb = nil,
52
+ headers = nil,
53
+ api_version = nil,
54
+ ip_address = nil,
55
+ body = nil)
56
+ @time = time
57
+ @uri = uri
58
+ @verb = verb
59
+ @headers = headers
60
+ @api_version = api_version
61
+ @ip_address = ip_address
62
+ @body = body
63
+ end
64
+
65
+ # Creates an instance of the object from a hash
66
+ def self.from_hash(hash)
67
+ if hash == nil
68
+ nil
69
+ else
70
+ # Extract variables from the hash
71
+ time = DateTime.iso8601(hash["time"]) if hash["time"]
72
+ uri = hash["uri"]
73
+ verb = hash["verb"]
74
+ headers = hash["headers"]
75
+ api_version = hash["api_version"]
76
+ ip_address = hash["ip_address"]
77
+ body = hash["body"]
78
+
79
+ # Create object from extracted values
80
+ EventRequestModel.new(time,
81
+ uri,
82
+ verb,
83
+ headers,
84
+ api_version,
85
+ ip_address,
86
+ body)
87
+ end
88
+ end
89
+ end
90
+ end