moesif_api 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +24 -0
- data/README.md +53 -0
- data/lib/moesif_api.rb +30 -0
- data/lib/moesif_api/api_helper.rb +151 -0
- data/lib/moesif_api/configuration.rb +17 -0
- data/lib/moesif_api/controllers/api_controller.rb +87 -0
- data/lib/moesif_api/controllers/base_controller.rb +20 -0
- data/lib/moesif_api/controllers/health_controller.rb +52 -0
- data/lib/moesif_api/exceptions/api_exception.rb +16 -0
- data/lib/moesif_api/http/http_call_back.rb +17 -0
- data/lib/moesif_api/http/http_client.rb +112 -0
- data/lib/moesif_api/http/http_context.rb +15 -0
- data/lib/moesif_api/http/http_method_enum.rb +7 -0
- data/lib/moesif_api/http/http_request.rb +28 -0
- data/lib/moesif_api/http/http_response.rb +19 -0
- data/lib/moesif_api/http/unirest_client.rb +36 -0
- data/lib/moesif_api/models/base_model.rb +32 -0
- data/lib/moesif_api/models/event_model.rb +71 -0
- data/lib/moesif_api/models/event_request_model.rb +90 -0
- data/lib/moesif_api/models/event_response_model.rb +72 -0
- data/lib/moesif_api/models/status_model.rb +44 -0
- data/lib/moesif_api/moesif_api_client.rb +22 -0
- data/test/controllers/controller_test_base.rb +29 -0
- data/test/controllers/test_api_controller.rb +35 -0
- data/test/http_response_catcher.rb +16 -0
- data/test/test_helper.rb +94 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 135adc3a0b9e77788fc3e44c590989af8c6f42b5
|
4
|
+
data.tar.gz: 2cc15b30740683c4c16c68cc68e905a63ccd8310
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5c7ebe96c975c087ec86a69298bc48d64b0a666c52b6e966e8e6c1a4c02ceb2322c7026932aecc6ff7a7bf7d1e98da1fc97dd09f33a22a571bf19ad377ec7f8f
|
7
|
+
data.tar.gz: 97f1ea4a0f21246e5ce75a7ab3200841ebbff031bc91e82a827a28baaf9f77ca8baf92bf69fd0d6df2a892a7aebac60e489f7271e85c4388ad1e2cf60ca0f32b
|
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
License:
|
2
|
+
========
|
3
|
+
The MIT License (MIT)
|
4
|
+
http://opensource.org/licenses/MIT
|
5
|
+
|
6
|
+
Copyright (c) 2016 Moesif, Inc
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
16
|
+
all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
MoesifApi
|
2
|
+
=================
|
3
|
+
|
4
|
+
How to configure:
|
5
|
+
=================
|
6
|
+
The generated code might need to be configured with your API credentials. To do that,
|
7
|
+
open the file "configuration.rb" and edit it's contents.
|
8
|
+
|
9
|
+
Alternatively, you can modify the Configuration parameters at run-time through the following:
|
10
|
+
```
|
11
|
+
MoesifApi::Configuration.your_paramater = YOUR_VALUE
|
12
|
+
```
|
13
|
+
|
14
|
+
How to build and install:
|
15
|
+
=============================
|
16
|
+
The generated code depends on a few Ruby gems. The references to these gems are
|
17
|
+
added in the gemspec file. The easiest way to resolve the dependencies,
|
18
|
+
build the gem and install it is through Rake:
|
19
|
+
|
20
|
+
1. Install Rake if not already installed: `gem install rake`
|
21
|
+
2. Install Bundler if not already installed: `gem install bundler`
|
22
|
+
3. From terminal/cmd navigate to the root directory of the SDK.
|
23
|
+
4. Invoke: `rake install`
|
24
|
+
|
25
|
+
Alternatively, you can build and install the gem manually:
|
26
|
+
|
27
|
+
1. From terminal/cmd navigate to the root directory of the SDK.
|
28
|
+
2. Run the build command: `gem build moesif_api.gemspec`
|
29
|
+
3. Run the install command: `gem install ./moesif_api-1.0.0.gem`
|
30
|
+
|
31
|
+
Note: You will need to have internet access for this step.
|
32
|
+
|
33
|
+
How to test:
|
34
|
+
=============
|
35
|
+
You can test the generated SDK and the server with automatically generated test
|
36
|
+
cases as follows:
|
37
|
+
|
38
|
+
1. From terminal/cmd navigate to the root directory of the SDK.
|
39
|
+
2. Invoke: `bundle exec rake`
|
40
|
+
|
41
|
+
How to use:
|
42
|
+
===========
|
43
|
+
After having installed the gem, you can easily use the SDK following these steps.
|
44
|
+
|
45
|
+
1. Create a "moesif_api_test.rb" file in the root directory.
|
46
|
+
2. Use any controller as follows:
|
47
|
+
```ruby
|
48
|
+
require 'moesif_api'
|
49
|
+
|
50
|
+
api_client = MoesifApi::MoesifAPIClient.new
|
51
|
+
controller = api_client.api_controller
|
52
|
+
response = controller.create_event(<required parameters if any>)
|
53
|
+
```
|
data/lib/moesif_api.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
require 'openssl'
|
3
|
+
require 'json'
|
4
|
+
require 'unirest'
|
5
|
+
|
6
|
+
# Helper Files
|
7
|
+
require_relative 'moesif_api/api_helper.rb'
|
8
|
+
require_relative 'moesif_api/configuration.rb'
|
9
|
+
require_relative 'moesif_api/moesif_api_client.rb'
|
10
|
+
|
11
|
+
# Http
|
12
|
+
require_relative 'moesif_api/http/http_call_back.rb'
|
13
|
+
require_relative 'moesif_api/http/http_client.rb'
|
14
|
+
require_relative 'moesif_api/http/http_method_enum.rb'
|
15
|
+
require_relative 'moesif_api/http/http_request.rb'
|
16
|
+
require_relative 'moesif_api/http/http_response.rb'
|
17
|
+
require_relative 'moesif_api/http/http_context.rb'
|
18
|
+
require_relative 'moesif_api/http/unirest_client.rb'
|
19
|
+
|
20
|
+
# Models
|
21
|
+
require_relative 'moesif_api/models/base_model.rb'
|
22
|
+
require_relative 'moesif_api/models/event_request_model.rb'
|
23
|
+
require_relative 'moesif_api/models/event_model.rb'
|
24
|
+
require_relative 'moesif_api/models/event_response_model.rb'
|
25
|
+
require_relative 'moesif_api/models/status_model.rb'
|
26
|
+
|
27
|
+
# Controllers
|
28
|
+
require_relative 'moesif_api/controllers/base_controller.rb'
|
29
|
+
require_relative 'moesif_api/controllers/api_controller.rb'
|
30
|
+
require_relative 'moesif_api/controllers/health_controller.rb'
|
@@ -0,0 +1,151 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class APIHelper
|
5
|
+
# Replaces template parameters in the given url
|
6
|
+
# @param [String] The query string builder to replace the template parameters
|
7
|
+
# @param [Array] The parameters to replace in the url
|
8
|
+
def self.append_url_with_template_parameters(query_builder, parameters)
|
9
|
+
# perform parameter validation
|
10
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
11
|
+
|
12
|
+
# return if there are no parameters to replace
|
13
|
+
if parameters.nil?
|
14
|
+
query_builder
|
15
|
+
else
|
16
|
+
# iterate and append parameters
|
17
|
+
parameters.each do |key, value|
|
18
|
+
replace_value = ''
|
19
|
+
|
20
|
+
if value.nil?
|
21
|
+
replace_value = ''
|
22
|
+
elsif value.instance_of? Array
|
23
|
+
value.map!{|element| CGI.escape(element.to_s)}
|
24
|
+
replace_value = value.join('/')
|
25
|
+
else
|
26
|
+
replace_value = CGI.escape(value.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
# find the template parameter and replace it with its value
|
30
|
+
query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
return query_builder
|
34
|
+
end
|
35
|
+
|
36
|
+
# Appends the given set of parameters to the given query string
|
37
|
+
# @param [String] The query string builder to replace the template parameters
|
38
|
+
# @param [Array] The parameters to append
|
39
|
+
def self.append_url_with_query_parameters(query_builder, parameters)
|
40
|
+
# perform parameter validation
|
41
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
42
|
+
|
43
|
+
# return if there are no parameters to replace
|
44
|
+
if parameters.nil?
|
45
|
+
return query_builder
|
46
|
+
else
|
47
|
+
# remove any nil values
|
48
|
+
parameters = parameters.reject { |_key, value| value.nil? }
|
49
|
+
|
50
|
+
# does the query string already has parameters
|
51
|
+
has_params = query_builder.include? '?'
|
52
|
+
separator = has_params ? '&' : '?'
|
53
|
+
|
54
|
+
# append query with separator and parameters and return
|
55
|
+
return query_builder << separator << URI.encode_www_form(parameters)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Validates and processes the given Url
|
60
|
+
# @param [String] The given Url to process
|
61
|
+
# @return [String] Pre-processed Url as string
|
62
|
+
def self.clean_url(url)
|
63
|
+
# perform parameter validation
|
64
|
+
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
65
|
+
|
66
|
+
# ensure that the urls are absolute
|
67
|
+
matches = url.match(%r{^(https?:\/\/[^\/]+)})
|
68
|
+
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
69
|
+
|
70
|
+
# get the http protocol match
|
71
|
+
protocol = matches[1]
|
72
|
+
|
73
|
+
# check if parameters exist
|
74
|
+
index = url.index('?')
|
75
|
+
|
76
|
+
# remove redundant forward slashes
|
77
|
+
query = url[protocol.length...(index != nil ? index : url.length)]
|
78
|
+
query.gsub!(%r{\/\/+}, '/')
|
79
|
+
|
80
|
+
# get the parameters
|
81
|
+
parameters = index != nil ? url[url.index('?')...url.length] : ""
|
82
|
+
|
83
|
+
# return processed url
|
84
|
+
return protocol + query + parameters
|
85
|
+
end
|
86
|
+
|
87
|
+
# Parses JSON string.
|
88
|
+
# @param [String] A JSON string.
|
89
|
+
def self.json_deserialize(json)
|
90
|
+
begin
|
91
|
+
return JSON.parse(json)
|
92
|
+
rescue
|
93
|
+
raise TypeError, "Server responded with invalid JSON."
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Form encodes a hash of parameters.
|
98
|
+
# @param [Hash] The hash of parameters to encode.
|
99
|
+
# @return [Hash] A hash with the same parameters form encoded.
|
100
|
+
def self.form_encode_parameters(form_parameters)
|
101
|
+
encoded = Hash.new
|
102
|
+
form_parameters.each do |key, value|
|
103
|
+
encoded.merge!(APIHelper.form_encode value, key)
|
104
|
+
end
|
105
|
+
return encoded
|
106
|
+
end
|
107
|
+
|
108
|
+
# Form encodes an object.
|
109
|
+
# @param [Dynamic] An object to form encode.
|
110
|
+
# @param [String] The name of the object.
|
111
|
+
# @return [Hash] A form encoded representation of the object in the form of a hash.
|
112
|
+
def self.form_encode(obj, instance_name)
|
113
|
+
retval = Hash.new
|
114
|
+
|
115
|
+
# If this is a structure, resolve it's field names.
|
116
|
+
if obj.kind_of? BaseModel
|
117
|
+
obj = obj.to_hash
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create a form encoded hash for this object.
|
121
|
+
if obj == nil
|
122
|
+
nil
|
123
|
+
elsif obj.instance_of? Array
|
124
|
+
obj.each_with_index do |value, index|
|
125
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + "[" + index.to_s + "]"))
|
126
|
+
end
|
127
|
+
elsif obj.instance_of? Hash
|
128
|
+
obj.each do |key, value|
|
129
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + "[" + key + "]"))
|
130
|
+
end
|
131
|
+
else
|
132
|
+
retval[instance_name] = obj
|
133
|
+
end
|
134
|
+
return retval
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# extend types to support to_bool
|
140
|
+
module ToBoolean
|
141
|
+
def to_bool
|
142
|
+
return true if self == true || self.to_s.strip =~ /^(true|yes|y|1)$/i
|
143
|
+
return false
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
class NilClass; include ToBoolean; end
|
148
|
+
class TrueClass; include ToBoolean; end
|
149
|
+
class FalseClass; include ToBoolean; end
|
150
|
+
class Numeric; include ToBoolean; end
|
151
|
+
class String; include ToBoolean; end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class Configuration
|
5
|
+
# The base Uri for API calls
|
6
|
+
@base_uri = 'https://api.moesif.net'
|
7
|
+
|
8
|
+
# Your Application Id for authentication/authorization
|
9
|
+
@application_id = 'TODO: Replace'
|
10
|
+
|
11
|
+
# create the getters and setters
|
12
|
+
class << self
|
13
|
+
attr_accessor :base_uri
|
14
|
+
attr_accessor :application_id
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class ApiController < BaseController
|
5
|
+
@@instance = ApiController.new
|
6
|
+
# Singleton instance of the controller class
|
7
|
+
def self.instance
|
8
|
+
@@instance
|
9
|
+
end
|
10
|
+
|
11
|
+
# Add Single API Event Call
|
12
|
+
# @param [EventModel] body Required parameter: Example:
|
13
|
+
# @return void response from the API call
|
14
|
+
def create_event(body)
|
15
|
+
# the base uri for api requests
|
16
|
+
_query_builder = Configuration.base_uri.dup
|
17
|
+
|
18
|
+
# prepare query string for API call
|
19
|
+
_query_builder << '/v1/events'
|
20
|
+
|
21
|
+
# validate and preprocess url
|
22
|
+
_query_url = APIHelper.clean_url _query_builder
|
23
|
+
|
24
|
+
# prepare headers
|
25
|
+
_headers = {
|
26
|
+
'content-type' => 'application/json; charset=utf-8',
|
27
|
+
'X-Moesif-Application-Id' => Configuration.application_id
|
28
|
+
}
|
29
|
+
|
30
|
+
# Create the HttpRequest object for the call
|
31
|
+
_request = @http_client.post _query_url, headers: _headers, parameters: body.to_json
|
32
|
+
|
33
|
+
# Call the on_before_request callback
|
34
|
+
@http_call_back.on_before_request(_request) if @http_call_back
|
35
|
+
|
36
|
+
# Invoke the API call and get the response
|
37
|
+
_response = @http_client.execute_as_string(_request)
|
38
|
+
|
39
|
+
# Wrap the request and response in an HttpContext object
|
40
|
+
_context = HttpContext.new(_request, _response)
|
41
|
+
|
42
|
+
# Call the on_after_response callback
|
43
|
+
@http_call_back.on_after_response(_context) if @http_call_back
|
44
|
+
|
45
|
+
# Global error handling using HTTP status codes.
|
46
|
+
validate_response(_context)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Add multiple API Events in a single batch (batch size must be less than 250kb)
|
50
|
+
# @param [List of EventModel] body Required parameter: Example:
|
51
|
+
# @return void response from the API call
|
52
|
+
def create_events_batch(body)
|
53
|
+
# the base uri for api requests
|
54
|
+
_query_builder = Configuration.base_uri.dup
|
55
|
+
|
56
|
+
# prepare query string for API call
|
57
|
+
_query_builder << '/v1/events/batch'
|
58
|
+
|
59
|
+
# validate and preprocess url
|
60
|
+
_query_url = APIHelper.clean_url _query_builder
|
61
|
+
|
62
|
+
# prepare headers
|
63
|
+
_headers = {
|
64
|
+
'content-type' => 'application/json; charset=utf-8',
|
65
|
+
'X-Moesif-Application-Id' => Configuration.application_id
|
66
|
+
}
|
67
|
+
|
68
|
+
# Create the HttpRequest object for the call
|
69
|
+
_request = @http_client.post _query_url, headers: _headers, parameters: body.to_json
|
70
|
+
|
71
|
+
# Call the on_before_request callback
|
72
|
+
@http_call_back.on_before_request(_request) if @http_call_back
|
73
|
+
|
74
|
+
# Invoke the API call and get the response
|
75
|
+
_response = @http_client.execute_as_string(_request)
|
76
|
+
|
77
|
+
# Wrap the request and response in an HttpContext object
|
78
|
+
_context = HttpContext.new(_request, _response)
|
79
|
+
|
80
|
+
# Call the on_after_response callback
|
81
|
+
@http_call_back.on_after_response(_context) if @http_call_back
|
82
|
+
|
83
|
+
# Global error handling using HTTP status codes.
|
84
|
+
validate_response(_context)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class BaseController
|
5
|
+
attr_accessor :http_client, :http_call_back
|
6
|
+
|
7
|
+
@@http_client = UnirestClient.new
|
8
|
+
|
9
|
+
def initialize(http_client: nil, http_call_back: nil)
|
10
|
+
@http_client = http_client ||= @@http_client
|
11
|
+
@http_call_back = http_call_back
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate_response(context)
|
15
|
+
if !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK
|
16
|
+
raise APIException.new 'HTTP Response Not OK', context
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module MoesifApi
|
4
|
+
class HealthController < BaseController
|
5
|
+
@@instance = HealthController.new
|
6
|
+
# Singleton instance of the controller class
|
7
|
+
def self.instance
|
8
|
+
@@instance
|
9
|
+
end
|
10
|
+
|
11
|
+
# Health Probe
|
12
|
+
# @return StatusModel response from the API call
|
13
|
+
def get_health_probe
|
14
|
+
# the base uri for api requests
|
15
|
+
_query_builder = Configuration.base_uri.dup
|
16
|
+
|
17
|
+
# prepare query string for API call
|
18
|
+
_query_builder << '/health/probe'
|
19
|
+
|
20
|
+
# validate and preprocess url
|
21
|
+
_query_url = APIHelper.clean_url _query_builder
|
22
|
+
|
23
|
+
# prepare headers
|
24
|
+
_headers = {
|
25
|
+
'accept' => 'application/json',
|
26
|
+
'X-Moesif-Application-Id' => Configuration.application_id
|
27
|
+
}
|
28
|
+
|
29
|
+
# Create the HttpRequest object for the call
|
30
|
+
_request = @http_client.get _query_url, headers: _headers
|
31
|
+
|
32
|
+
# Call the on_before_request callback
|
33
|
+
@http_call_back.on_before_request(_request) if @http_call_back
|
34
|
+
|
35
|
+
# Invoke the API call and get the response
|
36
|
+
_response = @http_client.execute_as_string(_request)
|
37
|
+
|
38
|
+
# Wrap the request and response in an HttpContext object
|
39
|
+
_context = HttpContext.new(_request, _response)
|
40
|
+
|
41
|
+
# Call the on_after_response callback
|
42
|
+
@http_call_back.on_after_response(_context) if @http_call_back
|
43
|
+
|
44
|
+
# Global error handling using HTTP status codes.
|
45
|
+
validate_response(_context)
|
46
|
+
|
47
|
+
# Return appropriate response type
|
48
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
49
|
+
return StatusModel.from_hash(decoded)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|