#self.class, modules, and httparty #http://stackoverflow.com/questions/42366427/how-does-self-class-method-work-when-including-a-module require "tts/api/client/version" require 'httparty' class TTSApiClient #Include HTTParty as client include HTTParty attr_reader :config 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 ### Courses ### #get all available courses def get_courses() handle_errors do self.class.get("/courses") end end #add a courses def add_course(course) handle_errors do self.class.post("/courses", :body => {"course" => course}) end end #get a specific course def get_course(course_id) handle_errors do self.class.get("/courses/" + course_id.to_s) end end #delete a course def delete_course(course_id) handle_errors do self.class.delete("/courses/" + course_id.to_s) end end #update a course def update_course(course) id = course["id"] handle_errors do self.class.put("/courses/" + id, :body => {"course" => course}) end end ### Instructors ### #get all available istructors def get_instructors() handle_errors do self.class.get("/instructors") end end #add an instructor def add_instructor(instructor) handle_errors do self.class.post("/instructors", :body => {"instructor" => instructor}) end end #get a specific instructor def get_instructor(instructor_id) handle_errors do self.class.get("/instructors/" + instructor_id.to_s) end end #delete an instructor def delete_instructor(instructor_id) handle_errors do self.class.delete("/instructors/" + instructor_id.to_s) end end #update a instructor def update_instructor(instructor) id = instructor["id"] handle_errors do self.class.put("/instructors/" + id, :body => {"instructor" => instructor}) end end ### Locations ### #get all available locations def get_locations() handle_errors do self.class.get("/locations") end end #add an location def add_location(location) handle_errors do self.class.post("/locations", :body => {"location" => location}) end end #get a specific location def get_location(location_id) handle_errors do self.class.get("/locations/" + location_id.to_s) end end #delete an location def delete_location(location_id) handle_errors do self.class.delete("/locations/" + location_id.to_s) end end #update a location def update_location(location) id = location["id"] handle_errors do self.class.put("/locations/" + id, :body => {"location" => location}) end end ### Campuses ### #get all available campuses def get_campuses() handle_errors do self.class.get("/campuses") end end #add a campus def add_campus(campus) handle_errors do self.class.post("/campuses", :body => {"campus" => campus}) end end #get a specific campus def get_campus(campus_id) handle_errors do self.class.get("/campuses/" + campus_id.to_s) end end #delete an campus def delete_campus(campus_id) handle_errors do self.class.delete("/campuses/" + campus_id.to_s) end end #update a campus def update_campus(campus) id = campus["id"] handle_errors do self.class.put("/campuses/" + id, :body => {"campus" => campus}) end end ### Course_Locations (classes) ### #get all available course_locations (classes) def get_course_locations() handle_errors do self.class.get("/course_locations") end end #add a course_locations (class) def add_course_location(course_location) handle_errors do self.class.post("/course_locations", :body => {"course_location" => course_location}) end end #get a specific course_location (class) def get_course_location(course_location_id) handle_errors do self.class.get("/course_locations/" + course_location_id.to_s) end end #delete a course_location (class) def delete_course_location(course_location_id) handle_errors do self.class.delete("/course_locations/" + course_location_id.to_s) end end #update a course_location (class) 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 #set HTTParty options based on @config def setup self.class.base_uri @config[:base_uri] self.class.headers 'Authorization' => @config[:token] #self.class.headers 'Content-Type' => 'application/json' end #handle HTTParty errors def handle_errors begin yield rescue Net::OpenTimeout, Net::ReadTimeout, SocketError => e raise e, "Cannot connect to host: #{@config[:base_uri]}" end end # Configure through hash def configure(opts = {}) opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym} setup end # Configure through yaml file 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 end