Class: TTSApiClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/tts/api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TTSApiClient

Returns a new instance of TTSApiClient.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tts/api/client.rb', line 14

def initialize(opts = {})
	# Configuration defaults
  @config = {
            :base_uri => "api.techtalentsouth.com",
            :token => ""
          }

  #establish valid config keys based on keys in @config hash
  @valid_config_keys = @config.keys

	#set configuration based on options passed in
	configure(opts)

	#setup HTTParty based on cofig
	setup

end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/tts/api/client.rb', line 12

def config
  @config
end

Instance Method Details

#add_campus(campus) ⇒ Object

add a campus



152
153
154
155
156
# File 'lib/tts/api/client.rb', line 152

def add_campus(campus)
  handle_errors do
    self.class.post("/campuses", :body => {"campus" => campus})
  end
end

#add_course(course) ⇒ Object

add a courses



41
42
43
44
45
# File 'lib/tts/api/client.rb', line 41

def add_course(course)
  handle_errors do
    self.class.post("/courses", :body => {"course" => course})
  end
end

#add_course_location(course_location) ⇒ Object

add a course_locations (class)



189
190
191
192
193
# File 'lib/tts/api/client.rb', line 189

def add_course_location(course_location)
  handle_errors do
    self.class.post("/course_locations", :body => {"course_location" => course_location})
  end
end

#add_instructor(instructor) ⇒ Object

add an instructor



78
79
80
81
82
# File 'lib/tts/api/client.rb', line 78

def add_instructor(instructor)
  handle_errors do
    self.class.post("/instructors", :body => {"instructor" => instructor})
  end
end

#add_location(location) ⇒ Object

add an location



115
116
117
118
119
# File 'lib/tts/api/client.rb', line 115

def add_location(location)
  handle_errors do
    self.class.post("/locations", :body => {"location" => location})
  end
end

#configure(opts = {}) ⇒ Object

Configure through hash



234
235
236
237
# File 'lib/tts/api/client.rb', line 234

def configure(opts = {})
  opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
  setup
end

#configure_with_yaml(path_to_yaml_file) ⇒ Object

Configure through yaml file



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/tts/api/client.rb', line 240

def configure_with_yaml(path_to_yaml_file)
  begin
    config = YAML::load(IO.read(path_to_yaml_file))
  rescue Errno::ENOENT => e
    raise e, "YAML configuration file couldn't be found. Using defaults."
  rescue Psych::SyntaxError
    raise e, "YAML configuration file contains invalid syntax. Using defaults."
  end

  configure(config)
end

#delete_campus(campus_id) ⇒ Object

delete an campus



166
167
168
169
170
# File 'lib/tts/api/client.rb', line 166

def delete_campus(campus_id)
  handle_errors do
    self.class.delete("/campuses/" + campus_id.to_s)
  end
end

#delete_course(course_id) ⇒ Object

delete a course



55
56
57
58
59
# File 'lib/tts/api/client.rb', line 55

def delete_course(course_id)
  handle_errors do
    self.class.delete("/courses/" + course_id.to_s)
  end
end

#delete_course_location(course_location_id) ⇒ Object

delete a course_location (class)



203
204
205
206
207
# File 'lib/tts/api/client.rb', line 203

def delete_course_location(course_location_id)
  handle_errors do
    self.class.delete("/course_locations/" + course_location_id.to_s)
  end
end

#delete_instructor(instructor_id) ⇒ Object

delete an instructor



92
93
94
95
96
# File 'lib/tts/api/client.rb', line 92

def delete_instructor(instructor_id)
  handle_errors do
    self.class.delete("/instructors/" + instructor_id.to_s)
  end
end

#delete_location(location_id) ⇒ Object

delete an location



129
130
131
132
133
# File 'lib/tts/api/client.rb', line 129

def delete_location(location_id)
  handle_errors do
    self.class.delete("/locations/" + location_id.to_s)
  end
end

#get_campus(campus_id) ⇒ Object

get a specific campus



159
160
161
162
163
# File 'lib/tts/api/client.rb', line 159

def get_campus(campus_id)
  handle_errors do
    self.class.get("/campuses/" + campus_id.to_s)
  end
end

#get_campusesObject

Campuses ### get all available campuses



145
146
147
148
149
# File 'lib/tts/api/client.rb', line 145

def get_campuses()
	handle_errors do
  	self.class.get("/campuses")
 end
end

#get_course(course_id) ⇒ Object

get a specific course



48
49
50
51
52
# File 'lib/tts/api/client.rb', line 48

def get_course(course_id)
  handle_errors do
    self.class.get("/courses/" + course_id.to_s)
  end
end

#get_course_location(course_location_id) ⇒ Object

get a specific course_location (class)



196
197
198
199
200
# File 'lib/tts/api/client.rb', line 196

def get_course_location(course_location_id)
  handle_errors do
    self.class.get("/course_locations/" + course_location_id.to_s)
  end
end

#get_course_locationsObject

Course_Locations (classes) ### get all available course_locations (classes)



182
183
184
185
186
# File 'lib/tts/api/client.rb', line 182

def get_course_locations()
  handle_errors do
    self.class.get("/course_locations")
  end
end

#get_coursesObject

Courses ### get all available courses



34
35
36
37
38
# File 'lib/tts/api/client.rb', line 34

def get_courses()
	handle_errors do
  	self.class.get("/courses")
 end
end

#get_instructor(instructor_id) ⇒ Object

get a specific instructor



85
86
87
88
89
# File 'lib/tts/api/client.rb', line 85

def get_instructor(instructor_id)
  handle_errors do
    self.class.get("/instructors/" + instructor_id.to_s)
  end
end

#get_instructorsObject

Instructors ### get all available istructors



71
72
73
74
75
# File 'lib/tts/api/client.rb', line 71

def get_instructors()
	handle_errors do
  	self.class.get("/instructors")
 end
end

#get_location(location_id) ⇒ Object

get a specific location



122
123
124
125
126
# File 'lib/tts/api/client.rb', line 122

def get_location(location_id)
  handle_errors do
    self.class.get("/locations/" + location_id.to_s)
  end
end

#get_locationsObject

Locations ### get all available locations



108
109
110
111
112
# File 'lib/tts/api/client.rb', line 108

def get_locations()
	handle_errors do
  	self.class.get("/locations")
 end
end

#handle_errorsObject

handle HTTParty errors



225
226
227
228
229
230
231
# File 'lib/tts/api/client.rb', line 225

def handle_errors
  begin
    yield
  rescue Net::OpenTimeout, Net::ReadTimeout, SocketError => e
   raise e, "Cannot connect to host: #{@config[:base_uri]}"
  end
end

#setupObject

set HTTParty options based on @config



218
219
220
221
222
# File 'lib/tts/api/client.rb', line 218

def setup
	self.class.base_uri @config[:base_uri]
  self.class.headers 'Authorization' => @config[:token]
  #self.class.headers 'Content-Type' => 'application/json'
end

#update_campus(campus) ⇒ Object

update a campus



173
174
175
176
177
178
# File 'lib/tts/api/client.rb', line 173

def update_campus(campus)
  id = campus["id"]
  handle_errors do
    self.class.put("/campuses/" + id, :body => {"campus" => campus})
  end
end

#update_course(course) ⇒ Object

update a course



62
63
64
65
66
67
# File 'lib/tts/api/client.rb', line 62

def update_course(course)
  id = course["id"]
  handle_errors do
    self.class.put("/courses/" + id, :body => {"course" => course})
  end
end

#update_course_location(course_location) ⇒ Object

update a course_location (class)



210
211
212
213
214
215
# File 'lib/tts/api/client.rb', line 210

def update_course_location(course_location)
  id = course_location["id"]
  handle_errors do
    self.class.put("/course_locations/" + id, :body => {"course_location" => course_location})
  end
end

#update_instructor(instructor) ⇒ Object

update a instructor



99
100
101
102
103
104
# File 'lib/tts/api/client.rb', line 99

def update_instructor(instructor)
  id = instructor["id"]
  handle_errors do
    self.class.put("/instructors/" + id, :body => {"instructor" => instructor})
  end
end

#update_location(location) ⇒ Object

update a location



136
137
138
139
140
141
# File 'lib/tts/api/client.rb', line 136

def update_location(location)
  id = location["id"]
  handle_errors do
    self.class.put("/locations/" + id, :body => {"location" => location})
  end
end