Class: FbUtils::Comment

Inherits:
Object
  • Object
show all
Defined in:
lib/fb_utils/comment.rb

Constant Summary collapse

"http://graph.facebook.com/comments/?ids="

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Comment

Returns a new instance of Comment.



7
8
9
10
11
12
13
14
# File 'lib/fb_utils/comment.rb', line 7

def initialize(hash={})
  return if hash.empty?
  @id = hash["id"]
  @from_name = hash["from"]["name"]
  @from_id = hash["from"]["id"]
  @message = hash["message"]
  @created_time = hash["created_time"]
end

Instance Attribute Details

#created_timeObject

Returns the value of attribute created_time.



5
6
7
# File 'lib/fb_utils/comment.rb', line 5

def created_time
  @created_time
end

#from_idObject

Returns the value of attribute from_id.



5
6
7
# File 'lib/fb_utils/comment.rb', line 5

def from_id
  @from_id
end

#from_nameObject

Returns the value of attribute from_name.



5
6
7
# File 'lib/fb_utils/comment.rb', line 5

def from_name
  @from_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/fb_utils/comment.rb', line 5

def id
  @id
end

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/fb_utils/comment.rb', line 5

def message
  @message
end

Class Method Details

.get(href) ⇒ Object

Get comments related to href Return array of comments



51
52
53
54
55
56
57
# File 'lib/fb_utils/comment.rb', line 51

def self.get(href)
  result = JSON.parse(Net::HTTP.get_response(URI(LINK + href)).body)
  data = result[href]["data"]
  data.inject([]) do |comments, c|
    comments << Comment.new(c)
  end
end

.save(href, filename, *args) ⇒ Object

Get comments and save these in file system

The rightmost argument is an optional hash of options, which is pass further to particular writer, specified from :format key Default is: :format => ‘txt’

For example:

FbUtils::Comment.save('http://www.example.com', 'example_com')

FbUtils::Comment.save('http://www.example.com', 'example_com',
  :format => :csv,
  :fields_terminated_by => ";", 
  :fields_enclosed_by => "'", 
  :lines_terminated_by => "\n"


39
40
41
42
43
44
45
46
47
# File 'lib/fb_utils/comment.rb', line 39

def self.save(href, filename, *args)
  options = args.extract_options!
  options[:format] ||= "txt"

  comments = get(href)
  writer = "FbUtils::Writer::#{options[:format].to_s.camelize}".constantize.new(filename, options)
  writer.write(comments)
  comments
end

Instance Method Details

#to_arrayObject



20
21
22
# File 'lib/fb_utils/comment.rb', line 20

def to_array
  [@id, @message, @from_name, @from_id, @created_time]
end

#to_sObject



16
17
18
# File 'lib/fb_utils/comment.rb', line 16

def to_s
  to_array.join(' ')
end