require "httparty" require "json" require 'active_support/inflector' require "caseblocks_api/authentication_exception" require 'caseblocks_api/case_creation_error' module CaseblocksAPI require 'caseblocks_api/bucket_results' require 'caseblocks_api/searcher' require 'caseblocks_api/update_case' require 'caseblocks_api/get_cases' 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) params.merge!(:case_type_id => fetch_case_type_id(case_type)) response = self.class.post("/case_blocks/cases", :body => {:case => params}.to_json) if response.code != 201 response_hash = {:body => response.body, :code => response.code, :message => response.message, :headers => response.headers.inspect} raise CaseblocksAPI::CaseCreationError.new("Unable to create case - received response code #{response.code}", response_hash) end end private def fetch_case_type_id(case_type_name) case_type || find_case_type_by_name(case_type_name) end def find_case_type_by_name(case_type_name) case_types = self.class.get("/case_blocks/case_types")["case_types"] case_types.each do |case_type| if case_type["name"] == case_type_name @case_type = case_type["id"] end end @case_type end def case_type @case_type end end end