Sha256: d15e6520b18ce12f603a8ff638419216b2bff2fa7cdd8a8c9a421a316d472982

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
caseblocks_api-0.2.3 lib/caseblocks_api.rb