forked from anthology-inc/student-api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapiclient.rb
98 lines (78 loc) · 2.33 KB
/
webapiclient.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'faraday'
require 'faraday_middleware'
require 'ruby_dig'
# A sample Ruby program demonstrating usage of the CampusNexus Command Web API
# See https://github.com/campusmanagement/integration-samples/
class CommandModelClient
attr_accessor :client
attr_accessor :entity_url
def initialize(client, entity_url)
@client = client
@entity_url = entity_url.strip.sub(/\/$/, '')
end
def execute_command(command, value=nil)
command_url = "#{entity_url}/#{command}"
r = client.post(command_url, { "payload" => value })
r.body
end
def create
execute_command('create')
end
def delete(entity)
execute_command('delete', entity)
end
def save_new(entity)
execute_command('saveNew', entity)
end
def save(entity)
execute_command('save', entity)
end
def retrieve(id_value)
body = { "id" => id_value }
execute_command('get', body)
end
end
def main
# Set the following variables to match your environment.
# Username needs authorization in CampusNexus Web Client Security Console
# to "Academics - Configuration - Manage" task
root_uri = 'http://university-a.campusnexus.cloud/' # Web Client URI
username = 'user@university-a.campusnexus.cloud'
password = 'password'
url = "#{root_uri}/api/commands/"
conn = Faraday.new(url: url) do |faraday|
faraday.basic_auth(username, password)
faraday.request :json # Encode the request as JSON
faraday.response :json # Decode the response from JSON
#faraday.response :logger # Output to log
faraday.adapter Faraday.default_adapter
end
command_uri = "Academics/CourseType"
command_model_client = CommandModelClient.new(conn, command_uri)
# create
puts "Creating..."
result = command_model_client.create
# save new
puts "Saving..."
data = result.dig('payload','data')
data["code"] = "Test"
data["name"] = "Test"
result = command_model_client.save_new(data)
# update
puts "Updating..."
data = result.dig('payload','data')
data["name"] = "Test2"
result = command_model_client.save(data)
# retrieve
puts "Retrieving..."
id_token = result.dig('payload','data', 'id')
result = command_model_client.retrieve(id_token)
# delete
puts "Deleting..."
data = result.dig('payload','data')
result = command_model_client.delete(data)
end
# Running as a program
if __FILE__ == $0
main
end