Sha256: 7c02d74c74e819c7b2dc6b1f04c180553233db0195e792f231683767d52d311a

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module ChurchCommunityBuilder
  require 'cgi'
  require 'json'

  def self.admin_request(method, params = {}, body = nil)
    url = "https://#{ChurchCommunityBuilder::Api.api_subdomain}.ccbchurch.com/api.php"
    username = ChurchCommunityBuilder::Api.api_username
    password = ChurchCommunityBuilder::Api.api_password

    response =
    case method
    when :post
      Typhoeus::Request.post(url, {:headers => headers, :body => body})
    when :get
      Typhoeus::Request.get(url, params: params, userpwd: username+":"+password)
    when :put
      Typhoeus::Request.put(url, {:headers => headers, :body => body})
    when :delete
      Typhoeus::Request.delete(url, {:headers => headers, :params => params})
    end    


    unless response.success?
      if response.code > 0
        raise ChurchCommunityBuilderExceptions::UnableToConnectToChurchCommunityBuilder.new(response.body)
      else
        begin
          error_messages = JSON.parse(response.body)['error_message']
        rescue
          response_code_desc = response.headers.partition("\r\n")[0].sub(/^\S+/, '') rescue nil
          raise ChurchCommunityBuilderExceptions::UnknownErrorConnectingToChurchCommunityBuilder.new("Unknown error when connecting to The City.#{response_code_desc}")
        else
          raise ChurchCommunityBuilderExceptions::ChurchCommunityBuilderResponseError.new(error_messages)
        end
      end
    end    
    
    response
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
church-community-builder-0.1.0 lib/common.rb