require "httparty" require "json" require 'active_support/inflector' require "caseblocks_api/authentication_exception" module CaseblocksAPI require 'caseblocks_api/bucket_results' require 'caseblocks_api/finder' require 'caseblocks_api/update_case' class Client include HTTParty attr_reader :auth_token headers "Accept" => "application/json" headers "Content-Type" => "application/json" def initialize(email, password, url) self.class.base_uri url result = self.class.post("/tokens", :body => {email: email, password: password}.to_json) if result["message"] raise CaseblocksAPI::AuthenticationException.new(result["message"]) else @auth_token = result["token"] if result self.class.default_params("auth_token" => @auth_token) end end def create_case(params, case_type=nil) if case_type case_type_id = get_casetype_id(case_type) end if case_type_id params = params.merge(:case_type_id => case_type_id) end self.class.post("/case_blocks/cases", :body => {:case => params}.to_json) end def get_casetype_id(case_type_name) response = self.class.get("/case_blocks/case_types")["case_types"] response.each do |case_type| if case_type.has_value? case_type_name return case_type["id"] end end nil end end end