Class: Facebooker::Parser
- Inherits:
-
Object
- Object
- Facebooker::Parser
- Defined in:
- lib/facebooker/parser.rb,
lib/facebooker/parser.rb
Direct Known Subclasses
AddTags, AreFriends, BatchRun, CreateAlbum, CreateToken, DeactivateTemplateBundleByID, Errors, EventMembersGet, EventsGet, EventsRsvp, FqlMultiquery, FqlQuery, FriendListsGet, GetAlbums, GetAllocation, GetAppProperties, GetAppUsers, GetCookies, GetFriends, GetLoggedInUser, GetPhotos, GetPreference, GetRegisteredTemplateBundles, GetRestrictionInfo, GetSession, GetTags, GetUnconnectedFriendsCount, GroupGetMembers, GroupsGet, NotificationsGet, NotificationsSend, NotificationsSendEmail, PagesGetInfo, PagesIsAdmin, PagesIsFan, ProfileFBML, ProfileFBMLSet, ProfileInfo, ProfileInfoSet, PublishActionOfUser, PublishStoryToUser, PublishTemplatizedAction, PublishUserAction, RefreshImgSrc, RefreshRefURL, RegisterTemplateBundle, RegisterUsers, RevokeAuthorization, SendRequest, SetAppProperties, SetCookie, SetPreference, SetRefHandle, SetRestrictionInfo, SetStatus, SmsCanSend, SmsSend, StreamAddComment, StreamPublish, UnregisterUsers, UploadPhoto, UploadVideo, UserHasPermission, UserInfo, UserStandardInfo
Defined Under Namespace
Modules: REXMLElementExtensions, REXMLTextExtensions
Constant Summary collapse
- PARSERS =
{ 'facebook.auth.revokeAuthorization' => RevokeAuthorization, '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.users.getLoggedInUser' => GetLoggedInUser, 'facebook.users.hasAppPermission' => UserHasPermission, '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.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.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.publish' => StreamPublish, 'facebook.stream.addComment' => StreamAddComment, 'facebook.events.get' => EventsGet, 'facebook.events.rsvp' => EventsRsvp, 'facebook.groups.get' => GroupsGet, 'facebook.events.getMembers' => EventMembersGet, 'facebook.events.rsvp' => EventsRsvp, 'facebook.groups.get' => GroupsGet, '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 }
Class Method Summary collapse
- .anonymous_field_from(child, hash) ⇒ Object
- .array_of(response_element, element_name) ⇒ Object
- .array_of_hashes(response_element, element_name) ⇒ Object
- .array_of_text_values(response_element, element_name) ⇒ Object
- .booleanize(response) ⇒ Object
- .element(name, data) ⇒ Object
- .hash_or_value_for(element) ⇒ Object
- .hashinate(response_element) ⇒ Object
- .parse(method, data) ⇒ Object
Class Method Details
.anonymous_field_from(child, hash) ⇒ Object
111 112 113 114 115 |
# File 'lib/facebooker/parser.rb', line 111 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
37 38 39 40 41 42 43 44 45 |
# File 'lib/facebooker/parser.rb', line 37 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
53 54 55 56 57 |
# File 'lib/facebooker/parser.rb', line 53 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
47 48 49 50 51 |
# File 'lib/facebooker/parser.rb', line 47 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
107 108 109 |
# File 'lib/facebooker/parser.rb', line 107 def self.booleanize(response) response == "1" ? true : false end |
.element(name, data) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/facebooker/parser.rb', line 59 def self.element(name, data) data = data.body rescue data # either data or an HTTP response begin xml = Nokogiri::XML(data.strip) if node = xml.at(name) return node end if xml.root.name == name return xml.root end rescue NameError # Can't parse with Nokogiri doc = REXML::Document.new(data) doc.elements.each(name) do |element| return element end end raise "Element #{name} not found in #{data}" end |
.hash_or_value_for(element) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/facebooker/parser.rb', line 78 def self.hash_or_value_for(element) if element.children.size == 1 && element.children.first.text? element.content.strip else hashinate(element) end end |
.hashinate(response_element) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/facebooker/parser.rb', line 86 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 |
.parse(method, data) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/facebooker/parser.rb', line 30 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 |