Sha256: d7a4943a3cb7965918b80e47bec894bbfca10edfa400fbb8a9dacd1d10bbfa74

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 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)
      params.merge!(:case_type_id => fetch_case_type_id(case_type))
			self.class.post("/case_blocks/cases", :body => {:case => params}.to_json)
		end

    private

  		def fetch_case_type_id(case_type_name)
        case_types[case_type_name] || 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
            return case_type["id"]
          end
        end
        nil
      end

      def case_types
        @case_types ||= {}
      end

	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
caseblocks_api-0.2.5 lib/caseblocks_api.rb