Class: Facebooker::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/facebooker/parser.rb,
lib/facebooker/parser.rb

Defined Under Namespace

Modules: REXMLElementExtensions

Constant Summary collapse

PARSERS =
{
  'facebook.auth.createToken' => CreateToken,
  'facebook.auth.getSession' => GetSession,
  'facebook.connect.registerUsers' => RegisterUsers,
  'facebook.users.getInfo' => UserInfo,
  'facebook.users.getStandardInfo' => UserStandardInfo,
  'facebook.users.setStatus' => SetStatus,
  'facebook.users.getLoggedInUser' => GetLoggedInUser,
  'facebook.users.hasAppPermission' => UserHasPermission,
  'facebook.pages.isAdmin' => PagesIsAdmin,
  'facebook.pages.getInfo' => PagesGetInfo,
  'facebook.friends.get' => GetFriends,
  'facebook.friends.getLists' => FriendListsGet,
  'facebook.friends.areFriends' => AreFriends,
  'facebook.friends.getAppUsers' => GetAppUsers,
  'facebook.feed.publishStoryToUser' => PublishStoryToUser,
  'facebook.feed.publishActionOfUser' => PublishActionOfUser,
  'facebook.feed.publishTemplatizedAction' => PublishTemplatizedAction,
  'facebook.feed.registerTemplateBundle' => RegisterTemplateBundle,
  'facebook.feed.deactivateTemplateBundleByID' => DeactivateTemplateBundleByID,
  'facebook.feed.getRegisteredTemplateBundles' => GetRegisteredTemplateBundles,
  'facebook.feed.publishUserAction' => PublishUserAction,
  'facebook.notifications.get' => NotificationsGet,
  'facebook.notifications.send' => NotificationsSend,
  'facebook.notifications.sendRequest' => SendRequest,
  'facebook.profile.getFBML' => ProfileFBML,
  'facebook.profile.setFBML' => ProfileFBMLSet,
  'facebook.profile.getInfo' => ProfileInfo,
  'facebook.profile.setInfo' => ProfileInfoSet,
  'facebook.fbml.setRefHandle' => SetRefHandle,
  'facebook.fbml.refreshRefUrl' => RefreshRefURL,
  'facebook.fbml.refreshImgSrc' => RefreshImgSrc,
  'facebook.data.setCookie' => SetCookie,
  'facebook.data.getCookies' => GetCookies,
  'facebook.admin.setAppProperties' => SetAppProperties,
  'facebook.admin.getAppProperties' => GetAppProperties,
  'facebook.admin.getAllocation' => GetAllocation,
  'facebook.batch.run' => BatchRun,
  'facebook.fql.query' => FqlQuery,
  'facebook.photos.get' => GetPhotos,
  'facebook.photos.getAlbums' => GetAlbums,
  'facebook.photos.createAlbum' => CreateAlbum,
  'facebook.photos.getTags' => GetTags,
  'facebook.photos.addTag' => AddTags,
  'facebook.photos.upload' => UploadPhoto,
  'facebook.events.get' => EventsGet,
  'facebook.groups.get' => GroupsGet,
  'facebook.events.getMembers' => EventMembersGet,
  'facebook.groups.getMembers' => GroupGetMembers,
  'facebook.notifications.sendEmail' => NotificationsSendEmail,
  'facebook.data.getUserPreference' => GetPreference,
  'facebook.data.setUserPreference' => SetPreference
}

Class Method Summary collapse

Class Method Details

.anonymous_field_from(child, hash) ⇒ Object



80
81
82
83
84
# File 'lib/facebooker/parser.rb', line 80

def self.anonymous_field_from(child, hash)
  if child.name == 'anon'
    (hash[child.name] || []) << child.text_value
  end
end

.array_of(response_element, element_name) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/facebooker/parser.rb', line 22

def self.array_of(response_element, element_name)
  values_to_return = []
  response_element.elements.each(element_name) do |element|
    values_to_return << yield(element)
  end
  values_to_return
end

.array_of_hashes(response_element, element_name) ⇒ Object



36
37
38
39
40
# File 'lib/facebooker/parser.rb', line 36

def self.array_of_hashes(response_element, element_name)
  array_of(response_element, element_name) do |element|
    hashinate(element)
  end
end

.array_of_text_values(response_element, element_name) ⇒ Object



30
31
32
33
34
# File 'lib/facebooker/parser.rb', line 30

def self.array_of_text_values(response_element, element_name)
  array_of(response_element, element_name) do |element|
    element.text_value
  end
end

.element(name, data) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/facebooker/parser.rb', line 42

def self.element(name, data)
  data = data.body rescue data # either data or an HTTP response
  doc = REXML::Document.new(data)
  doc.elements.each(name) do |element|
    return element
  end
  raise "Element #{name} not found in #{data}"
end

.hash_or_value_for(element) ⇒ Object



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

def self.hash_or_value_for(element)
  if element.children.size == 1 && element.children.first.kind_of?(REXML::Text)
    element.text_value
  else
    hashinate(element)
  end
end

.hashinate(response_element) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/facebooker/parser.rb', line 59

def self.hashinate(response_element)
  response_element.children.reject{|c| c.kind_of? REXML::Text}.inject({}) do |hash, child|
    # If the node hasn't any child, and is not a list, we want empty strings, not empty hashes,
    #   except if attributes['nil'] == true
    hash[child.name] = 
    if (child.attributes['nil'] == 'true')
      nil 
    elsif (child.children.size == 1 && child.children.first.kind_of?(REXML::Text)) || (child.children.size == 0 && child.attributes['list'] != 'true')
      anonymous_field_from(child, hash) || child.text_value
    elsif child.attributes['list'] == 'true'
      child.children.reject{|c| c.kind_of? REXML::Text}.map { |subchild| hash_or_value_for(subchild)}    
    else
      child.children.reject{|c| c.kind_of? REXML::Text}.inject({}) do |subhash, subchild|
        subhash[subchild.name] = hash_or_value_for(subchild)
        subhash
      end
    end #if (child.attributes)
    hash
  end #do |hash, child|      
end

.parse(method, data) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/facebooker/parser.rb', line 14

def self.parse(method, data)
  Errors.process(data)
  parser = Parser::PARSERS[method]
  parser.process(
    data
  )
end