Module: FacebookHttp

Included in:
FacebookGraph, FacebookQuery
Defined in:
lib/fbauth/http.rb

Instance Method Summary collapse

Instance Method Details

#build_get_url(url, params = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/fbauth/http.rb', line 9

def build_get_url(url, params = {})
  q = build_query_string(params)
  if q
    url + q
  else
    url
  end
end

#build_query_string(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/fbauth/http.rb', line 74

def build_query_string options={}
  params = []
  str_keys = options.keys.collect{ |k| k.to_s }
  str_keys.sort.each do |str_key|
    key = str_key.to_sym
    value = options[key]
    params << "#{key.to_s}=#{URI.escape(value.to_s)}" unless value.nil?
  end
  "?#{params.join('&')}" unless params.empty?
end

#caching_enabled?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/fbauth/http.rb', line 18

def caching_enabled?
  ActionController::Base.cache_store.class.name == "ActiveSupport::Cache::MemCacheStore"
end

#get(url, params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fbauth/http.rb', line 22

def get(url, params = {})
  json = nil
  uri = URI.parse(build_get_url(url, params))

  json = Rails.cache.read(uri.to_s) if caching_enabled? && uri.to_s.size < 250
  if json.nil?
    bench = Benchmark.measure do
      http = Net::HTTP.new uri.host, uri.port
      begin
        http.use_ssl = (uri.scheme == "https")
        req = Net::HTTP::Get.new(uri.request_uri)
        response = http.request(req)
        raise "Facebook error response #{response.code} - #{response.body}" unless response.code == '200'
        begin
          json = JSON.parse(response.body)
        rescue => e
          raise "Error parsing facebook response: #{response.body}"
        end
      ensure
        http.finish if http.started?
      end
    end
    logger.warn("Facebook GET call to #{uri.to_s} completed in #{bench.real} seconds")
    Rails.cache.write(uri.to_s, json, :expires_in => 60) if caching_enabled? && json && uri.to_s.size < 250
  end
  json
end

#has_access_token?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/fbauth/http.rb', line 90

def has_access_token?(options = {})
  merged_options.has_key? :access_token
end

#loggerObject



94
95
96
# File 'lib/fbauth/http.rb', line 94

def logger
  Rails.logger
end

#merged_options(options = {}) ⇒ Object



85
86
87
88
# File 'lib/fbauth/http.rb', line 85

def merged_options(options = {})
  options.merge!(@options) if @options
  options
end

#post(url, params = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fbauth/http.rb', line 50

def post(url, params = {})
  json = nil
  uri = URI.parse(url)
  bench = Benchmark.measure do
    http = Net::HTTP.new uri.host, uri.port
    begin
      http.use_ssl = (uri.scheme == "https")
      req = Net::HTTP::Post.new(uri.path)
      req.set_form_data(params)
      response = http.request(req)
      raise "Facebook error response #{response.code} - #{response.body}" unless response.code == '200'
      begin
        json = JSON.parse(response.body)
      rescue => e
        raise "Error parsing Facebook response: #{response.body}"
      end
    ensure
      http.finish if http.started?
    end
  end
  logger.warn("Facebook POST call to #{uri.to_s} completed in #{bench.real} seconds")
  json
end