-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.rb
155 lines (119 loc) · 3.98 KB
/
client.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'json'
require 'uri_template'
require 'net/http'
require "link_header"
require "rest-client"
class Client
attr_reader :json_doc, :json_hyperschema
def self.run
puts "What is home?"
base_url = "https://hyper-api-example.herokuapp.com"
response = RestClient.get(base_url)
puts response
user_will = "continue"
loop do
options = {
"Go home" => nil,
"Exit" => nil
}
header_links = LinkHeader.parse(response.headers[:link]).to_a
if !header_links.empty?
schema_url = base_url + header_links[0][0]
schema = RestClient.get(schema_url)
client = self.new(response, schema)
retrieved_links = client.get_links
retrieved_links.map do |link|
key = link["title"] || link["rel"]
options[key] = link["rel"]
end
end
puts "What do you want to do?"
puts options.keys
option = gets.chomp
break if option == "Exit"
if option == "Go home"
response = RestClient.get(base_url)
end
next if option == "Go home"
rel = options[option]
link = retrieved_links.select{ |link| link["rel"] == rel }.first
url = base_url + link["href"]
case rel
when "create"
submission_schema_url = base_url + link["submissionSchema"]["$ref"]
submission_schema = RestClient.get(submission_schema_url)
schema_hash = JSON.parse(submission_schema)
required = schema_hash["required"]
form_body = {}
schema_hash["properties"].each do |property_name, property_info|
if property_info["readOnly"] == true
form_body[property_name] = schema_hash[property_name]
else
puts property_name + " | " + property_info["type"] + " | " + required.include?(property_name).to_s
value = gets.chomp
if property_info["type"] == "integer"
form_body[property_name] = value.to_i
else
form_body[property_name] = value
end
end
end
begin
response = RestClient.post(url, form_body.to_json, {content_type: :json, accept: :json})
rescue RestClient::ExceptionWithResponse => e
puts e.response
end
when "put"
schema_hash = client.json_hyperschema
required = schema_hash["required"]
form_body = {}
resp_hash = JSON.parse response
schema_hash["properties"].each do |property_name, property_info|
if property_info["readOnly"] == true
form_body[property_name] = resp_hash[property_name]
else
puts property_name + " | " + property_info["type"] + " | " + required.include?(property_name).to_s + " | " + resp_hash[property_name].to_s
value = gets.chomp
if value.empty?
value = resp_hash[property_name]
end
if property_info["type"] == "integer"
form_body[property_name] = value.to_i
else
form_body[property_name] = value
end
end
end
begin
response = RestClient.put(url, form_body.to_json, {content_type: :json, accept: :json})
rescue RestClient::ExceptionWithResponse => e
puts e.response
end
when "delete"
response = RestClient.delete(url)
else
response = RestClient.get(url)
end
puts "Here's what we found:"
puts response
end
end
def initialize(json_doc, json_hyperschema)
@json_doc = JSON.parse json_doc
@json_hyperschema = JSON.parse json_hyperschema
end
def get_links
links.map do |link|
template = URITemplate.new(link["href"])
link["href"] = template.expand(json_doc)
link
end
end
# def follow(retrieved_links, rel)
#
# end
def links
json_hyperschema["links"] || []
end
end
Client.run