trust-ruby-client 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/client.rb +153 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58d7862ebdef3ce0f0e5ea5801128c65950a332f
4
+ data.tar.gz: fc636f533ca59136a390c51d9c53041624e43707
5
+ SHA512:
6
+ metadata.gz: 904a5d4e0002b920f012a5df5db1b15f425af72a2606dd29767255507ad7265f210bd413f84e18bfc8369801f70598e8f4d5a93ebe5bbfae2cf04f6445351dbd
7
+ data.tar.gz: 42b808492828bb1a5de29726f418c39878e6d47d77476a17febe190603dae59fbefc2119cf17f3e73a192ed8ad7f9a8ee688b05ccbef6e88b396926d6ce4f944
data/lib/client.rb ADDED
@@ -0,0 +1,153 @@
1
+ require_relative 'user_auth_result'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+ require "base64"
6
+
7
+ module AnalyzeData
8
+
9
+ ##
10
+ # The Client allows interaction with the various types of AnalyzeData Projects
11
+ #
12
+ # try creating a new client, and use (e.g.) UserAuth() to gain access to UserAuth Project API
13
+ #
14
+ #
15
+ #
16
+ class Client
17
+ #Constructor, supply it with your AnalyzeData DSN
18
+ def initialize(dsn)
19
+
20
+ @dsn = dsn;
21
+
22
+ #Regex the DSN into parts
23
+ regex = /(http(s)?:\/\/)([0-9]+:[a-zA-Z0-9]+)@([a-z.]+(:[0-9]+)?\/)/
24
+ result = regex.match(dsn)
25
+ if (!result)
26
+ raise 'Invalid DSN provided'
27
+ end
28
+
29
+ @url = result[1]+result[4]
30
+ @auth = result[3]
31
+ end
32
+
33
+
34
+ def UserAuth(user_id)
35
+ return UserAuth.new(user_id, @url, @auth);
36
+ end
37
+
38
+
39
+
40
+
41
+
42
+
43
+ end #.Client class
44
+
45
+
46
+
47
+
48
+
49
+ ##
50
+ # The UserAuth class allows interaction with a UserAuth project
51
+ #
52
+ #
53
+ class UserAuth
54
+
55
+ ##
56
+ # Construction, when interacting with UserAuth, a user_id is required
57
+ def initialize(user_id, endpoint, authToken)
58
+ #The map containing
59
+ @data = {}
60
+
61
+ #The array with reserved keywords
62
+ @reservedKeys = ['user_id']
63
+
64
+ #init the user_id
65
+ @data["user_id"] = user_id
66
+
67
+
68
+ @endpoint = endpoint
69
+ @authToken = authToken
70
+
71
+
72
+ self
73
+ end
74
+
75
+ ##
76
+ # Send an arbitrary piece of data
77
+ # @return a reference to this
78
+ #
79
+ def setParameter(name, value)
80
+ if @reservedKeys.include?(name)
81
+ throw new Exception("You cannot set a parameter with name="+name+", as is a reserved entry");
82
+ end
83
+ @data[name] = value
84
+ self
85
+ end
86
+
87
+
88
+ ##
89
+ # Set the request URI
90
+ def setRequestURI(uri)
91
+ @data['request_uri'] = uri
92
+ self
93
+ end
94
+
95
+
96
+
97
+ ##
98
+ # Performs a synchronous request
99
+ #
100
+ def sync(default = UserAuthResult::INDECISIVE)
101
+ begin
102
+ #return doSync
103
+ response = doSync()
104
+ #parse the result body
105
+ json = JSON.parse(response.body)
106
+
107
+ #if it is a valid result, return it, otherwise default
108
+ if (json['code'] != 200)
109
+ default
110
+ else
111
+ json['result']
112
+ end
113
+ rescue Exception => e
114
+ #Return the default value
115
+ puts e.message
116
+ default
117
+ end
118
+ end #.end sync request
119
+
120
+
121
+ ##
122
+ # Performs an actual synchronous request
123
+ private def doSync()
124
+ #The target URI
125
+ uri = URI.parse(@endpoint+"events/asynchronous.json")
126
+
127
+ #Make a new JSON-HTTP Request to the URI
128
+ request = Net::HTTP::Post.new(uri)
129
+ request.content_type = "application/json"
130
+
131
+ #Set the DSN for authorization
132
+ request["Authorization"] = "Basic "+Base64.strict_encode64(@authToken)
133
+
134
+ #put all the JSON values
135
+ request.body = JSON.dump(@data)
136
+
137
+ #Set additional options
138
+ req_options = {
139
+ use_ssl: uri.scheme == "https",
140
+ }
141
+
142
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
143
+ http.request(request)
144
+ end
145
+
146
+ #Return the respionse
147
+ response
148
+ end
149
+
150
+ end #.UserAuth Class
151
+
152
+
153
+ end #.AnalyzeDAta Module
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trust-ruby-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - J.G.M.Mengerink
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/client.rb
20
+ homepage:
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.5.2.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A basic ruby client for the AnalyzeData UserAuth API
43
+ test_files: []