Sha256: 5d33b8be346050f4a00f06d1d2a49b72c3a99a2005f20b4427a5eae323c1ed09

Contents?: true

Size: 1.98 KB

Versions: 21

Compression:

Stored size: 1.98 KB

Contents

module Salesforce
  module Connection
    module HttpMethods
      extend ActiveSupport::Concern

      module ClassMethods

        def headers(options = {})
          {'Authorization' => "OAuth #{::Salesforce::Authentication.session_id}"}.merge(content_type_headers(options))
        end

        def get(path, options = {})
          http(:get, path, nil, options)
        end

        def patch(path, body, options = {})
          http(:patch, path, body, options)
        end

        def post(path, body, options = {})
          http(:post, path, body, options)
        end
        
        def delete(path)
          true if http(:delete, path, nil, :format => :xml)
        end
        
        def salesforce_url(path)
          if path.include?("services/data")
            ::Salesforce::Config.server_host + path
          else
            ::Salesforce::Config.server_url + "/" + path
          end
        end
        
        def http(action, path, body, options)
          as_logged_in_user do
            begin
              response = if body
                RestClient.send(action, salesforce_url(path), body, headers(options))
              else
                RestClient.send(action, salesforce_url(path), headers(options))
              end

              if response.body.present?
                convert_body(response, options) 
              else
                response
              end
            rescue RestClient::ResourceNotFound, RestClient::BadRequest => e
              convert_error(e, salesforce_url(path), options)
            end
          end
        end
        
        def content_type_headers(options)
          {}.tap do |hash|
            hash[:content_type] = if options[:content_type]
               options[:content_type]
            elsif options[:format].to_s == 'json'
              "application/json"
            elsif options[:format].to_s == 'xml'
              "application/xml"
            end
          end
        end
        
      end #ClassMethods
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
activeforce-5.0.0 lib/salesforce/connection/http_methods.rb
activeforce-1.10.5 lib/salesforce/connection/http_methods.rb
activeforce-1.10.4 lib/salesforce/connection/http_methods.rb
activeforce-4.0.0 lib/salesforce/connection/http_methods.rb
activeforce-1.10.3 lib/salesforce/connection/http_methods.rb
activeforce-3.1.0 lib/salesforce/connection/http_methods.rb
activeforce-1.10.2 lib/salesforce/connection/http_methods.rb
activeforce-3.0.0 lib/salesforce/connection/http_methods.rb
activeforce-2.1.1 lib/salesforce/connection/http_methods.rb
activeforce-1.10.1 lib/salesforce/connection/http_methods.rb
activeforce-2.1.0 lib/salesforce/connection/http_methods.rb
activeforce-1.10.0 lib/salesforce/connection/http_methods.rb
activeforce-2.0.1 lib/salesforce/connection/http_methods.rb
activeforce-2.0.0 lib/salesforce/connection/http_methods.rb
activeforce-1.9.1 lib/salesforce/connection/http_methods.rb
activeforce-1.9.0 lib/salesforce/connection/http_methods.rb
activeforce-1.8.0 lib/salesforce/connection/http_methods.rb
activeforce-1.7.1 lib/salesforce/connection/http_methods.rb
activeforce-1.7.0 lib/salesforce/connection/http_methods.rb
activeforce-1.6.0 lib/salesforce/connection/http_methods.rb