Module: Koala::HTTPService

Included in:
NetHTTPService, TyphoeusService
Defined in:
lib/koala/http_services.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

common functionality for all HTTP services



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/koala/http_services.rb', line 13

def self.included(base)
  base.class_eval do
    class << self
      attr_accessor :always_use_ssl, :proxy, :timeout
    end

    def self.server(options = {})
      server = "#{options[:rest_api] ? Facebook::REST_SERVER : Facebook::GRAPH_SERVER}"
      server.gsub!(/\.facebook/, "-video.facebook") if options[:video]
      "#{options[:beta] ? "beta." : ""}#{server}"
    end
    
    def self.encode_params(param_hash)
      # unfortunately, we can't use to_query because that's Rails, not Ruby
      # if no hash (e.g. no auth token) return empty string
      ((param_hash || {}).collect do |key_and_value|
        key_and_value[1] = MultiJson.encode(key_and_value[1]) unless key_and_value[1].is_a? String
        "#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}"
      end).join("&")
    end
                                  
    protected
    
    def self.params_require_multipart?(param_hash)
      param_hash.any? { |key, value| value.kind_of?(Koala::UploadableIO) }
    end

    def self.multipart_requires_content_type?
      true
    end
  end
end