Class: Social::Network::Graph::Ok::Base

Inherits:
Object
  • Object
show all
Includes:
Tail
Defined in:
lib/social/network/graph/ok/base.rb

Direct Known Subclasses

Notification, User

Instance Method Summary collapse

Methods included from Tail

#root=

Instance Method Details

#build_request_params(params = {}) ⇒ Object



80
81
82
83
84
85
# File 'lib/social/network/graph/ok/base.rb', line 80

def build_request_params(params = {})
  params.merge!({ :format => "JSON", :application_key => config[:application_key]})
  key = params.delete(:session_secret_key) || config[:secret_key]
  params[:sig] = build_sig(params, key)
  params
end

#build_sig(params, key) ⇒ Object



75
76
77
78
# File 'lib/social/network/graph/ok/base.rb', line 75

def build_sig(params, key)
  params_to_sign_string = params.keys.inject([]) {|m,e| m << "#{e}=#{params[e]}"}.sort.join
  Digest::MD5.hexdigest(params_to_sign_string + key)
end

#configObject



33
34
35
# File 'lib/social/network/graph/ok/base.rb', line 33

def config
  Social::Network(:ok).config
end

#deliver(params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/social/network/graph/ok/base.rb', line 61

def deliver(params)
  result = []
  request_params = build_request_params(params)
  query = "/fb.do?#{Rack::Utils.universal_build(request_params)}"
  result = self.http_query(query)
rescue => e
  puts "====== Send problem"
  puts params.inspect
  puts e.to_s
  puts e.backtrace
ensure
  result
end

#http_query(query) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/social/network/graph/ok/base.rb', line 46

def http_query(query)
  result = []
  url = URI.parse(config['api_server'])
  retries = 0
  result = Net::HTTP.start(url.host, url.port) { |http|
    http.read_timeout = 10
    http.get(query)
  }
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  retries += 1
  retry if retries < 3
ensure
  result
end

#normalize_msg(msg) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/social/network/graph/ok/base.rb', line 37

def normalize_msg(msg)
  [ ['&nbps;', ' '],
    ['&mdash;', '-'],
    ['&', ''],
    ['%', '']
  ].each { |rep| msg.gsub!(*rep) }
  msg
end