Class: Facebooker::Parser

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

Direct Known Subclasses

AddTags, AreFriends, BatchRun, CommentsAdd, CommentsGet, CommentsRemove, CreateAlbum, CreateToken, DashboardAddGlobalNews, DashboardAddNews, DashboardClearGlobalNews, DashboardClearNews, DashboardDecrementCount, DashboardGetActivity, DashboardGetCount, DashboardGetGlobalNews, DashboardGetNews, DashboardIncrementCount, DashboardMultiAddNews, DashboardMultiClearNews, DashboardMultiDecrementCount, DashboardMultiGetCount, DashboardMultiGetNews, DashboardMultiIncrementCount, DashboardMultiSetCount, DashboardPublishActivity, DashboardRemoveActivity, DashboardSetCount, DeactivateTemplateBundleByID, Errors, EventMembersGet, EventsCancel, EventsCreate, EventsEdit, EventsGet, EventsInvite, EventsRsvp, FqlMultiquery, FqlQuery, FriendListsGet, GetAlbums, GetAllocation, GetAppProperties, GetAppUsers, GetCookies, GetFriends, GetLoggedInUser, GetPhotos, GetPreference, GetPublicInfo, GetRegisteredTemplateBundles, GetRestrictionInfo, GetSession, GetStatus, GetStream, GetTags, GetUnconnectedFriendsCount, GroupGetMembers, GroupsGet, IsAppUser, MessageGetThreadsInFolder, NotificationsGet, NotificationsSend, NotificationsSendEmail, PagesGetInfo, PagesIsAdmin, PagesIsFan, ProfileFBML, ProfileFBMLSet, ProfileInfo, ProfileInfoSet, PublishActionOfUser, PublishStoryToUser, PublishTemplatizedAction, PublishUserAction, RefreshImgSrc, RefreshRefURL, RegisterTemplateBundle, RegisterUsers, RevokeAuthorization, RevokeExtendedPermission, SendRequest, SetAppProperties, SetCookie, SetPreference, SetRefHandle, SetRestrictionInfo, SetStatus, SmsCanSend, SmsSend, StreamAddComment, StreamAddLike, StreamPublish, StreamRemoveLike, UnregisterUsers, UploadNativeStrings, UploadPhoto, UploadVideo, UserHasPermission, UserInfo, UserStandardInfo

Defined Under Namespace

Modules: REXMLElementExtensions, REXMLTextExtensions

Constant Summary collapse

PARSERS =
{
  'facebook.auth.revokeAuthorization' => RevokeAuthorization,
  'facebook.auth.revokeExtendedPermission' => RevokeExtendedPermission,
  'facebook.auth.createToken' => CreateToken,
  'facebook.auth.getSession' => GetSession,
  'facebook.connect.registerUsers' => RegisterUsers,
  'facebook.connect.unregisterUsers' => UnregisterUsers,
  'facebook.connect.getUnconnectedFriendsCount' => GetUnconnectedFriendsCount,
  'facebook.users.getInfo' => UserInfo,
  'facebook.users.getStandardInfo' => UserStandardInfo,
  'facebook.users.setStatus' => SetStatus,
  'facebook.status.get' => GetStatus,
  'facebook.users.getLoggedInUser' => GetLoggedInUser,
  'facebook.users.hasAppPermission' => UserHasPermission,
  'facebook.users.isAppUser' => IsAppUser,
  'facebook.pages.isAdmin' => PagesIsAdmin,
  'facebook.pages.getInfo' => PagesGetInfo,
  'facebook.pages.isFan' => PagesIsFan,
  '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.message.getThreadsInFolder' => MessageGetThreadsInFolder,
  '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.setRestrictionInfo' => SetRestrictionInfo,
  'facebook.admin.getRestrictionInfo' => GetRestrictionInfo,
  'facebook.admin.getAllocation' => GetAllocation,
  'facebook.application.getPublicInfo' => GetPublicInfo,
  'facebook.batch.run' => BatchRun,
  'facebook.fql.query' => FqlQuery,
  'facebook.fql.multiquery' => FqlMultiquery,
  '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.stream.get' => GetStream,
  'facebook.stream.publish' => StreamPublish,
  'facebook.stream.addComment' => StreamAddComment,
  'facebook.stream.addLike' => StreamAddLike,
  'facebook.stream.removeLike' => StreamRemoveLike,
  'facebook.events.create' => EventsCreate,
  'facebook.events.cancel' => EventsCancel,
  'facebook.events.get' => EventsGet,
  'facebook.events.edit' => EventsEdit,
  'facebook.events.invite' => EventsInvite,
  'facebook.events.rsvp' => EventsRsvp,
  'facebook.groups.get' => GroupsGet,
  'facebook.events.getMembers' => EventMembersGet,
  'facebook.groups.getMembers' => GroupGetMembers,
  'facebook.notifications.sendEmail' => NotificationsSendEmail,
  'facebook.data.getUserPreference' => GetPreference,
  'facebook.data.setUserPreference' => SetPreference,
  'facebook.video.upload' => UploadVideo,
  'facebook.sms.send' => SmsSend,
  'facebook.sms.canSend' => SmsCanSend,
  'facebook.comments.add' => CommentsAdd,
  'facebook.comments.remove' => CommentsRemove,
  'facebook.comments.get' => CommentsGet,
  'facebook.dashboard.setCount' => DashboardSetCount,
  'facebook.dashboard.getCount' => DashboardGetCount,
  'facebook.dashboard.incrementCount' => DashboardIncrementCount,
  'facebook.dashboard.decrementCount' => DashboardDecrementCount,
  'facebook.dashboard.multiGetCount' => DashboardMultiGetCount,
  'facebook.dashboard.multiSetCount' => DashboardMultiSetCount,
  'facebook.dashboard.multiIncrementCount' => DashboardMultiIncrementCount,
  'facebook.dashboard.multiDecrementCount' => DashboardMultiDecrementCount,
  'facebook.dashboard.addGlobalNews' => DashboardAddGlobalNews,
  'facebook.dashboard.getGlobalNews' => DashboardGetGlobalNews,
  'facebook.dashboard.clearGlobalNews' => DashboardClearGlobalNews,
  'facebook.dashboard.addNews' => DashboardAddNews,
  'facebook.dashboard.getNews' => DashboardGetNews,
  'facebook.dashboard.clearNews' => DashboardClearNews,
  'facebook.dashboard.multiAddNews' => DashboardMultiAddNews,
  'facebook.dashboard.multiGetNews' => DashboardMultiGetNews,
  'facebook.dashboard.multiClearNews' => DashboardMultiClearNews,
  'facebook.dashboard.publishActivity' => DashboardPublishActivity,
  'facebook.dashboard.removeActivity' => DashboardRemoveActivity,
  'facebook.dashboard.getActivity' => DashboardGetActivity,
  'facebook.intl.uploadNativeStrings' => UploadNativeStrings
}

Class Method Summary collapse

Class Method Details

.anonymous_field_from(child, hash) ⇒ Object



161
162
163
164
165
# File 'lib/facebooker/parser.rb', line 161

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

.array_of(response_element, element_name) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/facebooker/parser.rb', line 44

def self.array_of(response_element, element_name)
  values_to_return = []
  response_element.children.each do |element|
    next if element.text?
    next unless element.name == element_name
    values_to_return << yield(element)
  end
  values_to_return
end

.array_of_hashes(response_element, element_name) ⇒ Object



60
61
62
63
64
# File 'lib/facebooker/parser.rb', line 60

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



54
55
56
57
58
# File 'lib/facebooker/parser.rb', line 54

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

.booleanize(response) ⇒ Object



157
158
159
# File 'lib/facebooker/parser.rb', line 157

def self.booleanize(response)
  response == "1" ? true : false
end

.element(name, data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/facebooker/parser.rb', line 66

def self.element(name, data)
  data = data.body rescue data # either data or an HTTP response
  if Object.const_defined?(:Nokogiri)
    xml = Nokogiri::XML(data.strip)
    if node = xml.at(name)
      return node
    end
    if xml.root.name == name
      return xml.root
    end
  else
    doc = REXML::Document.new(data)
    doc.elements.each(name) do |element|
      return element
    end
  end
  raise "Element #{name} not found in #{data}"
end

.hash_by_key_or_value_for(element, convert_1_to_true = false) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/facebooker/parser.rb', line 119

def self.hash_by_key_or_value_for(element, convert_1_to_true=false)
  if element.children.size == 0
    { element['key'] => nil }
  elsif element.children.size == 1 && element.children.first.text?
    { element['key'] => (convert_1_to_true ? element.content.strip == '1' : element.content.strip) }
  else
    hashinate_by_key(element, convert_1_to_true)
  end
end

.hash_or_value_for(element) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/facebooker/parser.rb', line 85

def self.hash_or_value_for(element)
  if element.children.size == 1 && element.children.first.text?
    element.content.strip
  else
    # We can have lists in not list item
    if element['list'] == 'true'
      element.children.reject{|c| c.text? }.map { |subchild| hash_or_value_for(subchild)}
    else
      hashinate(element)
    end
  end
end

.hashinate(response_element) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/facebooker/parser.rb', line 98

def self.hashinate(response_element)
  response_element.children.reject{|c| c.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['nil'] == 'true')
      nil
    elsif (child.children.size == 1 && child.children.first.text?) || (child.children.size == 0 && child['list'] != 'true')
      anonymous_field_from(child, hash) || child.content.strip
    elsif child['list'] == 'true'
      child.children.reject{|c| c.text? }.map { |subchild| hash_or_value_for(subchild)}
    else
      child.children.reject{|c| c.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

.hashinate_by_key(response_element, convert_1_to_true = false) ⇒ Object

A modification to hashinate. The new dashboard API returns XML in a different format than the other calls. What used to be the element name has become an attribute called “key”.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/facebooker/parser.rb', line 131

def self.hashinate_by_key(response_element, convert_1_to_true=false)
  response_element.children.reject{|c| c.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['key']] =
    if (child['nil'] == 'true')
      nil
    elsif (child.children.size == 1 && child.children.first.text?) || (child.children.size == 0 && child['list'] != 'true')
      anonymous_field_from(child, hash) || (convert_1_to_true ? child.content.strip == '1' : child.content.strip)
    elsif child['list'] == 'true' && child.children.reject {|subchild| subchild.text?}.all? { |subchild| !subchild.text? && subchild['key'].nil? }
      child.children.reject{|c| c.text? }.map { |subchild| hash_by_key_or_value_for(subchild, convert_1_to_true)}
    elsif child['list'] == 'true'
      hash_by_key_or_value_for(child, convert_1_to_true)
    else
      child.children.reject{|c| c.text? }.inject({}) do |subhash, subchild|
        subhash[subchild['key']] = hash_by_key_or_value_for(subchild, convert_1_to_true)
        subhash
      end
    end
    hash
  end
end

.parse(method, data) ⇒ Object



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

def self.parse(method, data)
  Errors.process(data)
  parser = Parser::PARSERS[method]
  raise "Can't find a parser for '#{method}'" unless parser
  parser.process(data)
end