Module FacebookHelper
In: facebook_bot/helper.rb

These are some helper methods to get you along in life. Use wisely, young one.

Methods

Public Instance methods

Connect somewhere to Facebook!

  fb.connect 'emerson.facebook.com'

[Source]

    # File facebook_bot/helper.rb, line 82
82:   def connect host=nil
83:     # should we try to connect to our default_host?

84:     # changes on a network to network basis.

85:     # www if you're on a regional network.

86:     if host.nil? && @default_host
87:       host = @default_host
88:     elsif host.nil?
89:       host = 'www.facebook.com'
90:     end
91:     if @http.nil? || @http.address != host
92:       @http = Net::HTTP.new(host)
93:       puts "Connected to #{host}."
94:     end
95:   end

Gets the contents of any IDs it finds on a given page. Takes an array of IDs. Returns a hash of ID information, keys being the IDs provided. Useful for getting basic information from a page, such as the verification post_form_id‘s, and any other information you so desire. Will try to extract this information from the ID, or give up: (in order) ID value, ID src, ID innerHTML.

    id_info = fb.get_ids_from_url '/home.php', ['post_form_id']

[Source]

    # File facebook_bot/helper.rb, line 25
25:   def get_ids_from_url url, ids
26:     elements = {}
27:     # go to the url

28:     doc = hpricot_get_url url
29:     
30:     ids.each do |id|
31:       tries = 0
32:       while elements[id].nil? 
33:         ele = doc.at("##{id}")
34:         if ele.nil?
35:           puts "cannot get id #{id}"
36:           elements[id] = 'unknown'
37:         elsif ele.attributes['value']
38:           elements[id] = ele.attributes['value']
39:         elsif ele.attributes['src']
40:           elements[id] = ele.attributes['src']
41:         elsif ele.inner_html
42:           elements[id] = ele.inner_html
43:         end
44:       end
45:     end
46:     elements
47:   end

Will get a URL and parse it with the (lovely) hpricot library. Returns The parsed Hpricot object, to which you can do what you please.

    doc = fb.hpricot_get_url '/home.php'

[Source]

    # File facebook_bot/helper.rb, line 6
 6:   def hpricot_get_url url
 7:     req = try_try_again do
 8:       @http.get2(url, @opts[:headers])
 9:     end
10:     
11:     if req.code.to_i != 200
12:       log(req) and return
13:     end
14:     
15:     Hpricot(req.body)
16:   end

Logs a bad Net::HTTP request. Appends to a file(log.txt) and prints to the console.

[Source]

    # File facebook_bot/helper.rb, line 68
68:   def log req
69:     msg = "ERROR: #{req.code}: #{req.message}\nbody: #{req.body}\nheaders: #{req.response.to_hash.inspect}"
70:     File.open('log.txt','a') do |f|
71:       f.puts msg
72:     end
73:     puts msg
74:   end

If at first you don‘t succeed… try try again. This function takes a block and will try(and try and try and try) that block until it finally doesn‘t raise an error. This is useful if you are getting timeouts or other such errors while grabbing some information. I use it liberally just in case I get disconnected temporarily (which is often).

[Source]

    # File facebook_bot/helper.rb, line 54
54:   def try_try_again
55:     begin
56:       yield
57:     rescue Timeout::Error => err
58:       puts "Timeout Error: #{err}! Retrying.."
59:       retry
60:     rescue Exception => exception
61:       puts "Exception: #{exception.message}! Retrying.."
62:       retry
63:     end
64:   end

[Validate]