1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
|
#--
# Copyright (c) 2005 Robert Aman
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
require 'feed_tools/feed_structures'
module FeedTools
# The <tt>FeedTools::FeedItem</tt> class represents the structure of
# a single item within a web feed.
class FeedItem
# Initialize the feed object
def initialize
super
@feed_data = nil
@feed_data_type = :xml
@xml_document = nil
@root_node = nil
@title = nil
@id = nil
@time = Time.now.gmtime
@version = FeedTools::FEED_TOOLS_VERSION::STRING
end
# Breaks any references that the feed entry may be keeping around, thus
# making the job of the garbage collector much, much easier. Call this
# method prior to feed entries going out of scope to prevent memory leaks.
def dispose()
@feed_data = nil
@feed_data_type = nil
@xml_document = nil
@root_node = nil
@title = nil
@id = nil
@time = nil
end
# Returns the parent feed of this feed item
# Warning, this method may be slow if you have a
# large number of FeedTools::Feed objects. Can't
# use a direct reference to the parent because it plays
# havoc with the garbage collector. Could've used
# a WeakRef object, but really, if there are multiple
# parent feeds, something is going to go wrong, and the
# programmer needs to be notified. A WeakRef
# implementation can't detect this condition.
def feed
parent_feed = nil
ObjectSpace.each_object(FeedTools::Feed) do |feed|
if feed.instance_variable_get("@entries").nil?
feed.items
end
unsorted_items = feed.instance_variable_get("@entries")
for item in unsorted_items
if item.object_id == self.object_id
if parent_feed.nil?
parent_feed = feed
break
else
raise "Multiple parent feeds found."
end
end
end
end
return parent_feed
end
# Does a full parse of the feed item.
def full_parse
self.configurations
self.encoding
self.xml_document
self.root_node
self.feed_type
self.feed_version
self.id
self.title
self.content
self.summary
self.links
self.link
self.comments
self.time
self.updated
self.published
self.source
self.categories
self.tags
self.images
self.rights
self.author
self.publisher
self.itunes_summary
self.itunes_subtitle
self.itunes_image_link
self.itunes_author
self.itunes_duration
self.media_text
self.media_thumbnail_link
self.explicit?
end
# Returns a duplicate object suitable for serialization
def serializable
self.full_parse()
feed_item_to_dump = self.dup
feed_item_to_dump.author
feed_item_to_dump.publisher
feed_item_to_dump.instance_variable_set("@xml_document", nil)
feed_item_to_dump.instance_variable_set("@root_node", nil)
return feed_item_to_dump
end
# Returns the load options for this feed.
def configurations
if @configurations.blank?
parent_feed = self.feed
if parent_feed != nil
@configurations = parent_feed.configurations.dup
else
@configurations = FeedTools.configurations.dup
end
end
return @configurations
end
# Sets the load options for this feed.
def configurations=(new_configurations)
@configurations = new_configurations
end
# Returns the feed item's encoding.
def encoding
if @encoding.nil?
parent_feed = self.feed
if parent_feed != nil
@encoding = parent_feed.encoding
else
@encoding = nil
end
end
return @encoding
end
# Returns the feed item's raw data.
def feed_data
return @feed_data
end
# Sets the feed item's data.
def feed_data=(new_feed_data)
@time = nil
@feed_data = new_feed_data
end
# Returns the feed item's data type.
def feed_data_type
return @feed_data_type
end
# Sets the feed item's data type.
def feed_data_type=(new_feed_data_type)
@feed_data_type = new_feed_data_type
if self.feed_data_type != :xml
@xml_document = nil
end
end
# Returns a REXML Document of the feed_data
def xml_document
if @xml_document.nil?
return nil if self.feed_data.blank?
if self.feed_data_type != :xml
@xml_document = nil
else
# TODO: :ignore_whitespace_nodes => :all
# Add that?
# ======================================
@xml_document = REXML::Document.new(self.feed_data)
end
end
return @xml_document
end
# Returns the first node within the root_node that matches the xpath
# query.
def find_node(xpath, select_result_value=false)
if self.feed_data_type != :xml
raise "The feed data type is not xml."
end
return FeedTools::XmlHelper.try_xpaths(self.root_node, [xpath],
:select_result_value => select_result_value)
end
# Returns all nodes within the root_node that match the xpath query.
def find_all_nodes(xpath, select_result_value=false)
if self.feed_data_type != :xml
raise "The feed data type is not xml."
end
return FeedTools::XmlHelper.try_xpaths_all(self.root_node, [xpath],
:select_result_value => select_result_value)
end
# Returns the root node of the feed item.
def root_node
if @root_node.nil?
if self.xml_document.nil?
return nil
end
@root_node = self.xml_document.root
end
return @root_node
end
# Sets the root node of the feed item.
#
# This allows namespace information to be inherited by the feed item
# from the feed itself. When creating individual nodes from scratch,
# the <tt>feed_data=</tt> method should be used instead.
def root_node=(new_root_node)
@root_node = new_root_node
end
# Returns the feed type of this item
def feed_type
if @feed_type.nil?
parent_feed = self.feed
@feed_type = parent_feed.feed_type unless parent_feed.nil?
end
return @feed_type
end
# Returns the feed version of this item
def feed_version
if @feed_version.nil?
parent_feed = self.feed
@feed_version = parent_feed.feed_version unless parent_feed.nil?
end
return @feed_version
end
# Returns the feed items's unique id
def id
if @id.nil?
@id = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:id/@gr:original-id",
"atom03:id/@gr:original-id",
"atom:id/@gr:original-id",
"id/@gr:original-id",
"atom10:id/text()",
"atom03:id/text()",
"atom:id/text()",
"id/text()",
"guid/text()"
], :select_result_value => true)
end
return @id
end
# Sets the feed item's unique id
def id=(new_id)
@id = new_id
end
# Returns the feed item title
def title
if @title.nil?
repair_entities = false
title_node = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:title",
"atom03:title",
"atom:title",
"title",
"dc:title",
"headline"
])
@title = FeedTools::HtmlHelper.process_text_construct(title_node,
self.feed_type, self.feed_version, [self.base_uri])
if self.feed_type == "atom" ||
self.configurations[:always_strip_wrapper_elements]
@title = FeedTools::HtmlHelper.strip_wrapper_element(@title)
end
if !@title.blank? && self.configurations[:strip_comment_count]
# Some blogging tools include the number of comments in a post
# in the title... this is supremely ugly, and breaks any
# applications which expect the title to be static, so we're
# gonna strip them out.
#
# If for some incredibly wierd reason you need the actual
# unstripped title, just use find_node("title/text()").to_s
@title = @title.strip.gsub(/\[\d*\]$/, "").strip
end
@title = nil if @title.blank?
end
return @title
end
# Sets the feed item title
def title=(new_title)
@title = new_title
end
# Returns the feed item content
def content
if @content.nil?
repair_entities = false
content_node = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:content",
"atom03:content",
"atom:content",
"body/datacontent",
"xhtml:body",
"body",
"xhtml:div",
"div",
"p:payload",
"payload",
"content:encoded",
"content",
"fullitem",
"encoded",
"description",
"tagline",
"subtitle",
"atom10:summary",
"atom03:summary",
"atom:summary",
"summary",
"abstract",
"blurb",
"info"
])
@content = FeedTools::HtmlHelper.process_text_construct(content_node,
self.feed_type, self.feed_version, [self.base_uri])
if self.feed_type == "atom" ||
self.configurations[:always_strip_wrapper_elements]
@content = FeedTools::HtmlHelper.strip_wrapper_element(@content)
end
if @content.nil?
@content = self.media_text
end
if @content.nil?
@content = self.itunes_summary
end
if @content.nil?
@content = self.itunes_subtitle
end
end
return @content
end
# Sets the feed item content
def content=(new_content)
@content = new_content
end
# Returns the feed item summary
def summary
if @summary.nil?
repair_entities = false
summary_node = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:summary",
"atom03:summary",
"atom:summary",
"summary",
"abstract",
"blurb",
"description",
"tagline",
"subtitle",
"xhtml:body",
"body",
"xhtml:div",
"div",
"p:payload",
"payload",
"fullitem",
"content:encoded",
"encoded",
"atom10:content",
"atom03:content",
"atom:content",
"content",
"info",
"body/datacontent"
])
@summary = FeedTools::HtmlHelper.process_text_construct(summary_node,
self.feed_type, self.feed_version, [self.base_uri])
if self.feed_type == "atom" ||
self.configurations[:always_strip_wrapper_elements]
@summary = FeedTools::HtmlHelper.strip_wrapper_element(@summary)
end
if @summary.blank?
@summary = self.media_text
end
if @summary.blank?
@summary = self.itunes_summary
end
if @summary.blank?
@summary = self.itunes_subtitle
end
end
return @summary
end
# Sets the feed item summary
def summary=(new_summary)
@summary = new_summary
end
# Returns the links collection
def links
if @links.nil?
@links = []
link_nodes =
FeedTools::XmlHelper.combine_xpaths_all(self.root_node, [
"atom10:link",
"atom03:link",
"atom:link",
"link",
"a",
"url",
"href"
])
for link_node in link_nodes
link_object = FeedTools::Link.new
link_object.href = FeedTools::XmlHelper.try_xpaths(link_node, [
"@atom10:href",
"@atom03:href",
"@atom:href",
"@href",
"@url",
"text()"
], :select_result_value => true)
if link_object.href.nil? && link_node.base_uri != nil
link_object.href = ""
end
begin
if !(link_object.href =~ /^file:/) &&
!FeedTools::UriHelper.is_uri?(link_object.href)
stored_base_uri =
FeedTools::GenericHelper.recursion_trap(:feed_link) do
self.base_uri if self.feed != nil
end
link_object.href = FeedTools::UriHelper.resolve_relative_uri(
link_object.href,
[link_node.base_uri, stored_base_uri])
end
rescue
end
if self.configurations[:url_normalization_enabled]
link_object.href =
FeedTools::UriHelper.normalize_url(link_object.href)
end
link_object.href.strip! unless link_object.href.nil?
next if link_object.href.blank?
link_object.hreflang = FeedTools::XmlHelper.try_xpaths(link_node, [
"@atom10:hreflang",
"@atom03:hreflang",
"@atom:hreflang",
"@hreflang"
], :select_result_value => true)
unless link_object.hreflang.nil?
link_object.hreflang = link_object.hreflang.downcase
end
link_object.rel = FeedTools::XmlHelper.try_xpaths(link_node, [
"@atom10:rel",
"@atom03:rel",
"@atom:rel",
"@rel"
], :select_result_value => true)
unless link_object.rel.nil?
link_object.rel = link_object.rel.downcase
end
link_object.type = FeedTools::XmlHelper.try_xpaths(link_node, [
"@atom10:type",
"@atom03:type",
"@atom:type",
"@type"
], :select_result_value => true)
unless link_object.type.nil?
link_object.type = link_object.type.downcase
end
link_object.title = FeedTools::XmlHelper.try_xpaths(link_node, [
"@atom10:title",
"@atom03:title",
"@atom:title",
"@title",
"text()"
], :select_result_value => true)
# This catches the ambiguities between atom, rss, and cdf
if link_object.title == link_object.href
link_object.title = nil
end
link_object.length = FeedTools::XmlHelper.try_xpaths(link_node, [
"@atom10:length",
"@atom03:length",
"@atom:length",
"@length"
], :select_result_value => true)
if !link_object.length.nil?
link_object.length = link_object.length.to_i
else
if !link_object.type.nil? && link_object.type[0..4] != "text" &&
link_object.type[-3..-1] != "xml" &&
link_object.href =~ /^http:\/\//
# Retrieve the length with an http HEAD request
else
link_object.length = nil
end
end
@links << link_object
end
if @links.empty? && self.enclosures.size > 0
# If there's seriously nothing to link to, but there's enclosures
# available, then add a link to the first one.
enclosure_link = self.enclosures[0]
link_object = FeedTools::Link.new
link_object.href = enclosure_link.url
link_object.type = enclosure_link.type
@links << link_object
end
end
return @links
end
# Sets the links collection
def links=(new_links)
@links = new_links
end
# Returns the feed item link
def link
if @link.nil?
max_score = 0
for link_object in self.links.reverse
score = 0
if FeedTools::HtmlHelper.html_type?(link_object.type)
score = score + 2
elsif link_object.type != nil
score = score - 1
end
if FeedTools::HtmlHelper.xml_type?(link_object.type)
score = score + 1
end
if link_object.type =~ /^video/ && self.links.size == 1
score = score + 1
elsif link_object.type =~ /^audio/ && self.links.size == 1
score = score + 1
end
if link_object.rel == "alternate"
score = score + 1
end
if link_object.rel == "self"
score = score - 1
end
if score >= max_score
max_score = score
@link = link_object.href
end
end
if @link.blank?
@link = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"@href",
"@rdf:about",
"@about"
], :select_result_value => true)
end
if @link.blank?
if FeedTools::UriHelper.is_uri?(self.id) &&
(self.id =~ /^http/)
@link = self.id
end
end
if !@link.blank?
@link = FeedTools::HtmlHelper.unescape_entities(@link)
end
@link = self.comments if @link.blank?
@link = nil if @link.blank?
begin
if !(@link =~ /^file:/) &&
!FeedTools::UriHelper.is_uri?(@link)
stored_base_uri =
FeedTools::GenericHelper.recursion_trap(:feed_link) do
self.base_uri if self.feed != nil
end
root_base_uri = nil
unless self.root_node.nil?
root_base_uri = self.root_node.base_uri
end
@link = FeedTools::UriHelper.resolve_relative_uri(
@link, [root_base_uri,stored_base_uri])
end
rescue
end
if self.configurations[:url_normalization_enabled]
@link = FeedTools::UriHelper.normalize_url(@link)
end
end
return @link
end
# Sets the feed item link
def link=(new_link)
@link = new_link
end
# Returns the parent feed's base_uri if any.
def base_uri
parent_feed = self.feed
if parent_feed != nil
return parent_feed.base_uri
else
return nil
end
end
# Returns the url for posting comments
def comments
if @comments.nil?
@comments = FeedTools::XmlHelper.try_xpaths(
self.root_node, ["comments/text()"],
:select_result_value => true)
begin
if !(@comments =~ /^file:/) &&
!FeedTools::UriHelper.is_uri?(@comments)
root_base_uri = nil
unless self.root_node.nil?
root_base_uri = self.root_node.base_uri
end
@comments = FeedTools::UriHelper.resolve_relative_uri(
@comments, [root_base_uri, self.base_uri])
end
rescue
end
if self.configurations[:url_normalization_enabled]
@comments = FeedTools::UriHelper.normalize_url(@comments)
end
end
return @comments
end
# Sets the url for posting comments
def comments=(new_comments)
@comments = new_comments
end
# Returns the contents of the itunes:summary element
def itunes_summary
if @itunes_summary.nil?
@itunes_summary = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"itunes:summary/text()"
], :select_result_value => true)
unless @itunes_summary.blank?
@itunes_summary =
FeedTools::HtmlHelper.unescape_entities(@itunes_summary)
@itunes_summary =
FeedTools::HtmlHelper.sanitize_html(@itunes_summary)
@itunes_summary.strip!
else
@itunes_summary = nil
end
end
return @itunes_summary
end
# Sets the contents of the itunes:summary element
def itunes_summary=(new_itunes_summary)
@itunes_summary = new_itunes_summary
end
# Returns the contents of the itunes:subtitle element
def itunes_subtitle
if @itunes_subtitle.nil?
@itunes_subtitle = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"itunes:subtitle/text()"
], :select_result_value => true)
unless @itunes_subtitle.blank?
@itunes_subtitle =
FeedTools::HtmlHelper.unescape_entities(@itunes_subtitle)
@itunes_subtitle =
FeedTools::HtmlHelper.sanitize_html(@itunes_subtitle)
@itunes_subtitle.strip!
else
@itunes_subtitle = nil
end
end
return @itunes_subtitle
end
# Sets the contents of the itunes:subtitle element
def itunes_subtitle=(new_itunes_subtitle)
@itunes_subtitle = new_itunes_subtitle
end
# Returns the contents of the media:text element
def media_text
if @media_text.nil?
@media_text = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"media:text/text()"
], :select_result_value => true)
unless @media_text.blank?
@media_text = FeedTools::HtmlHelper.unescape_entities(@media_text)
@media_text = FeedTools::HtmlHelper.sanitize_html(@media_text)
@media_text.strip!
else
@media_text = nil
end
end
return @media_text
end
# Sets the contents of the media:text element
def media_text=(new_media_text)
@media_text = new_media_text
end
# Returns a list of the feed item's categories
def categories
if @categories.nil?
@categories = []
category_nodes = FeedTools::XmlHelper.try_xpaths_all(self.root_node, [
"category",
"dc:subject"
])
for category_node in category_nodes
category = FeedTools::Category.new
category.term = FeedTools::XmlHelper.try_xpaths(
category_node, ["@term", "text()"],
:select_result_value => true)
category.term.strip! unless category.term.nil?
category.label = FeedTools::XmlHelper.try_xpaths(
category_node, ["@label"],
:select_result_value => true)
category.label.strip! unless category.label.nil?
category.scheme = FeedTools::XmlHelper.try_xpaths(
category_node, [
"@scheme",
"@domain"
], :select_result_value => true)
category.scheme.strip! unless category.scheme.nil?
@categories << category
end
end
return @categories
end
# Returns a list of the feed items's images
def images
if @images.nil?
@images = []
image_nodes = FeedTools::XmlHelper.try_xpaths_all(self.root_node, [
"image",
"logo",
"apple-wallpapers:image",
"imageUrl"
])
unless image_nodes.blank?
for image_node in image_nodes
image = FeedTools::Image.new
image.href = FeedTools::XmlHelper.try_xpaths(image_node, [
"url/text()",
"@rdf:resource",
"@href",
"@url",
"text()"
], :select_result_value => true)
if image.href.nil? && image_node.base_uri != nil
image.href = ""
end
begin
if !(image.href =~ /^file:/) &&
!FeedTools::UriHelper.is_uri?(image.href)
stored_base_uri =
FeedTools::GenericHelper.recursion_trap(:feed_link) do
self.base_uri if self.feed != nil
end
image.href = FeedTools::UriHelper.resolve_relative_uri(
image.href, [image_node.base_uri, stored_base_uri])
end
rescue
end
if self.configurations[:url_normalization_enabled]
image.href = FeedTools::UriHelper.normalize_url(image.href)
end
image.href.strip! unless image.href.nil?
next if image.href.blank?
image.title = FeedTools::XmlHelper.try_xpaths(image_node,
["title/text()"], :select_result_value => true)
image.title.strip! unless image.title.nil?
image.description = FeedTools::XmlHelper.try_xpaths(image_node,
["description/text()"], :select_result_value => true)
image.description.strip! unless image.description.nil?
image.link = FeedTools::XmlHelper.try_xpaths(image_node,
["link/text()"], :select_result_value => true)
image.link.strip! unless image.link.nil?
image.height = FeedTools::XmlHelper.try_xpaths(image_node,
["height/text()"], :select_result_value => true).to_i
image.height = nil if image.height <= 0
image.width = FeedTools::XmlHelper.try_xpaths(image_node,
["width/text()"], :select_result_value => true).to_i
image.width = nil if image.width <= 0
image.style = FeedTools::XmlHelper.try_xpaths(image_node, [
"style/text()",
"@style"
], :select_result_value => true)
image.style.strip! unless image.style.nil?
image.style.downcase! unless image.style.nil?
@images << image unless image.url.nil?
end
end
for link_object in self.links
if link_object.type != nil && link_object.type =~ /^image/
image = FeedTools::Image.new
image.href = link_object.href
image.title = link_object.title
@images << image unless image.href.nil?
end
end
end
return @images
end
# Returns the feed item itunes image link
def itunes_image_link
if @itunes_image_link.nil?
@itunes_image_link = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"itunes:image/@href",
"itunes:link[@rel='image']/@href"
], :select_result_value => true)
if self.configurations[:url_normalization_enabled]
@itunes_image_link =
FeedTools::UriHelper.normalize_url(@itunes_image_link)
end
end
return @itunes_image_link
end
# Sets the feed item itunes image link
def itunes_image_link=(new_itunes_image_link)
@itunes_image_link = new_itunes_image_link
end
# Returns the feed item media thumbnail link
def media_thumbnail_link
if @media_thumbnail_link.nil?
@media_thumbnail_link = FeedTools::XmlHelper.try_xpaths(
self.root_node, [
"media:thumbnail/@url"
], :select_result_value => true)
if self.configurations[:url_normalization_enabled]
@media_thumbnail_link =
FeedTools::UriHelper.normalize_url(@media_thumbnail_link)
end
end
return @media_thumbnail_link
end
# Sets the feed item media thumbnail url
def media_thumbnail_link=(new_media_thumbnail_link)
@media_thumbnail_link = new_media_thumbnail_link
end
# Returns the feed item's rights information
def rights
if @rights.nil?
repair_entities = false
rights_node = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:copyright",
"atom03:copyright",
"atom:copyright",
"copyright",
"copyrights",
"dc:rights",
"rights"
])
@rights = FeedTools::HtmlHelper.process_text_construct(rights_node,
self.feed_type, self.feed_version, [self.base_uri])
if self.feed_type == "atom" ||
self.configurations[:always_strip_wrapper_elements]
@rights = FeedTools::HtmlHelper.strip_wrapper_element(@rights)
end
end
return @rights
end
# Sets the feed item's rights information
def rights=(new_rights)
@rights = new_rights
end
# Returns the first license link for the feed item.
def license
return self.licenses.first
end
# Returns all licenses linked from this feed item.
def licenses
if @licenses.nil?
@licenses = self.links.select do |link|
link.rel == "license"
end
end
return @licenses
end
# Sets the feed item's licenses.
def licenses=(new_licenses)
@licenses = new_licenses
end
# Returns all feed item enclosures
def enclosures
if @enclosures.nil?
@enclosures = []
# First, load up all the different possible sources of enclosures
rss_enclosures =
FeedTools::XmlHelper.try_xpaths_all(self.root_node, ["enclosure"])
atom_enclosures =
FeedTools::XmlHelper.try_xpaths_all(self.root_node, [
"atom10:link[@rel='enclosure']",
"atom03:link[@rel='enclosure']",
"atom:link[@rel='enclosure']",
"link[@rel='enclosure']"
])
media_content_enclosures =
FeedTools::XmlHelper.try_xpaths_all(self.root_node,
["media:content"])
media_group_enclosures =
FeedTools::XmlHelper.try_xpaths_all(self.root_node, ["media:group"])
bogus_enclosures =
FeedTools::XmlHelper.try_xpaths_all(self.root_node, ["video"])
# TODO: Implement this
bittorrent_enclosures =
FeedTools::XmlHelper.try_xpaths_all(self.root_node,
["bitTorrent:torrent"])
# Parse RSS-type enclosures. Thanks to a few buggy enclosures
# implementations, sometimes these also manage to show up in atom
# files.
for enclosure_node in rss_enclosures
enclosure = FeedTools::Enclosure.new
enclosure.url = FeedTools::HtmlHelper.unescape_entities(
enclosure_node.attributes["url"].to_s)
enclosure.type = enclosure_node.attributes["type"].to_s
enclosure.file_size = enclosure_node.attributes["length"].to_i
enclosure.credits = []
enclosure.explicit = false
@enclosures << enclosure
end
# Parse atom-type enclosures. If there are repeats of the same
# enclosure object, we merge the two together.
for enclosure_node in atom_enclosures
enclosure_url = FeedTools::HtmlHelper.unescape_entities(
enclosure_node.attributes["href"].to_s)
enclosure = nil
new_enclosure = false
for existing_enclosure in @enclosures
if existing_enclosure.url == enclosure_url
enclosure = existing_enclosure
break
end
end
if enclosure.nil?
new_enclosure = true
enclosure = FeedTools::Enclosure.new
end
enclosure.url = enclosure_url
enclosure.type = enclosure_node.attributes["type"].to_s
enclosure.file_size = enclosure_node.attributes["length"].to_i
enclosure.credits = []
enclosure.explicit = false
if new_enclosure
@enclosures << enclosure
end
end
# Parse atom-type enclosures. If there are repeats of the same
# enclosure object, we merge the two together.
for enclosure_node in bogus_enclosures
enclosure_url = FeedTools::HtmlHelper.unescape_entities(
enclosure_node.attributes["url"].to_s)
enclosure = nil
new_enclosure = false
for existing_enclosure in @enclosures
if existing_enclosure.url == enclosure_url
enclosure = existing_enclosure
break
end
end
if enclosure.nil?
new_enclosure = true
enclosure = FeedTools::Enclosure.new
end
enclosure.url = enclosure_url
if File.extname(enclosure_url) == ".wmv"
enclosure.type = "video/x-ms-wmv"
end
enclosure.explicit = false
if new_enclosure
@enclosures << enclosure
end
end
# Creates an anonymous method to parse content objects from the media
# module. We do this to avoid excessive duplication of code since we
# have to do identical processing for content objects within group
# objects.
parse_media_content = lambda do |media_content_nodes|
affected_enclosures = []
for enclosure_node in media_content_nodes
enclosure_url = FeedTools::HtmlHelper.unescape_entities(
enclosure_node.attributes["url"].to_s)
enclosure = nil
new_enclosure = false
for existing_enclosure in @enclosures
if existing_enclosure.url == enclosure_url
enclosure = existing_enclosure
break
end
end
if enclosure.nil?
new_enclosure = true
enclosure = FeedTools::Enclosure.new
end
enclosure.url = enclosure_url
enclosure.type = enclosure_node.attributes["type"].to_s
enclosure.file_size = enclosure_node.attributes["fileSize"].to_i
enclosure.duration = enclosure_node.attributes["duration"].to_s
enclosure.height = enclosure_node.attributes["height"].to_i
enclosure.width = enclosure_node.attributes["width"].to_i
enclosure.bitrate = enclosure_node.attributes["bitrate"].to_i
enclosure.framerate = enclosure_node.attributes["framerate"].to_i
enclosure.expression =
enclosure_node.attributes["expression"].to_s
enclosure.is_default =
(enclosure_node.attributes["isDefault"].to_s.downcase == "true")
enclosure_thumbnail_url =
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:thumbnail/@url"], :select_result_value => true)
if !enclosure_thumbnail_url.blank?
enclosure.thumbnail = FeedTools::EnclosureThumbnail.new(
FeedTools::HtmlHelper.unescape_entities(
enclosure_thumbnail_url),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:thumbnail/@height"],
:select_result_value => true)),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:thumbnail/@width"],
:select_result_value => true))
)
end
enclosure.categories = []
for category in FeedTools::XmlHelper.try_xpaths_all(
enclosure_node, ["media:category"])
enclosure.categories << FeedTools::Category.new
enclosure.categories.last.term =
FeedTools::HtmlHelper.unescape_entities(category.inner_xml)
enclosure.categories.last.scheme =
FeedTools::HtmlHelper.unescape_entities(
category.attributes["scheme"].to_s)
enclosure.categories.last.label =
FeedTools::HtmlHelper.unescape_entities(
category.attributes["label"].to_s)
if enclosure.categories.last.scheme.blank?
enclosure.categories.last.scheme = nil
end
if enclosure.categories.last.label.blank?
enclosure.categories.last.label = nil
end
end
enclosure_media_hash =
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:hash/text()"], :select_result_value => true)
if !enclosure_media_hash.nil?
enclosure.hash = FeedTools::EnclosureHash.new(
FeedTools::HtmlHelper.sanitize_html(
FeedTools::HtmlHelper.unescape_entities(
enclosure_media_hash), :strip),
"md5"
)
end
enclosure_media_player_url =
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:player/@url"], :select_result_value => true)
if !enclosure_media_player_url.blank?
enclosure.player = FeedTools::EnclosurePlayer.new(
FeedTools::HtmlHelper.unescape_entities(
enclosure_media_player_url),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:player/@height"], :select_result_value => true)),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:player/@width"], :select_result_value => true))
)
end
enclosure.credits = []
for credit in FeedTools::XmlHelper.try_xpaths_all(
enclosure_node, ["media:credit"])
enclosure.credits << FeedTools::EnclosureCredit.new(
FeedTools::HtmlHelper.unescape_entities(
credit.inner_xml.to_s.strip),
FeedTools::HtmlHelper.unescape_entities(
credit.attributes["role"].to_s.downcase)
)
if enclosure.credits.last.name.blank?
enclosure.credits.last.name = nil
end
if enclosure.credits.last.role.blank?
enclosure.credits.last.role = nil
end
end
enclosure.explicit =
(FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:adult/text()"]).to_s.downcase == "true")
enclosure_media_text =
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:text/text()"])
if !enclosure_media_text.blank?
enclosure.text = FeedTools::HtmlHelper.unescape_entities(
enclosure_media_text)
end
affected_enclosures << enclosure
if new_enclosure
@enclosures << enclosure
end
end
affected_enclosures
end
# Parse the independant content objects.
parse_media_content.call(media_content_enclosures)
media_groups = []
# Parse the group objects.
for media_group in media_group_enclosures
group_media_content_enclosures =
FeedTools::XmlHelper.try_xpaths_all(media_group,
["media:content"])
# Parse the content objects within the group objects.
affected_enclosures =
parse_media_content.call(group_media_content_enclosures)
# Now make sure that content objects inherit certain properties from
# the group objects.
for enclosure in affected_enclosures
media_group_thumbnail =
FeedTools::XmlHelper.try_xpaths(media_group,
["media:thumbnail/@url"], :select_result_value => true)
if enclosure.thumbnail.nil? && !media_group_thumbnail.blank?
enclosure.thumbnail = FeedTools::EnclosureThumbnail.new(
FeedTools::HtmlHelper.unescape_entities(
media_group_thumbnail),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(media_group,
["media:thumbnail/@height"],
:select_result_value => true)),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(media_group,
["media:thumbnail/@width"],
:select_result_value => true))
)
end
if (enclosure.categories.blank?)
enclosure.categories = []
for category in FeedTools::XmlHelper.try_xpaths_all(
media_group, ["media:category"])
enclosure.categories << FeedTools::Category.new
enclosure.categories.last.term =
FeedTools::HtmlHelper.unescape_entities(category.inner_xml)
enclosure.categories.last.scheme =
FeedTools::HtmlHelper.unescape_entities(
category.attributes["scheme"].to_s)
enclosure.categories.last.label =
FeedTools::HtmlHelper.unescape_entities(
category.attributes["label"].to_s)
if enclosure.categories.last.scheme.blank?
enclosure.categories.last.scheme = nil
end
if enclosure.categories.last.label.blank?
enclosure.categories.last.label = nil
end
end
end
enclosure_media_group_hash =
FeedTools::XmlHelper.try_xpaths(enclosure_node,
["media:hash/text()"], :select_result_value => true)
if enclosure.hash.nil? && !enclosure_media_group_hash.blank?
enclosure.hash = FeedTools::EnclosureHash.new(
FeedTools::HtmlHelper.sanitize_html(
FeedTools::HtmlHelper.unescape_entities(
enclosure_media_group_hash), :strip),
"md5"
)
end
enclosure_media_group_url = FeedTools::XmlHelper.try_xpaths(
media_group,
"media:player/@url",
:select_result_value => true
)
if enclosure.player.nil? && !enclosure_media_group_url.blank?
enclosure.player = FeedTools::EnclosurePlayer.new(
FeedTools::HtmlHelper.unescape_entities(
enclosure_media_group_url),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(media_group,
["media:player/@height"],
:select_result_value => true)),
FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(media_group,
["media:player/@width"],
:select_result_value => true
)
)
)
end
if enclosure.credits.nil? || enclosure.credits.size == 0
enclosure.credits = []
for credit in FeedTools::XmlHelper.try_xpaths_all(
media_group, ["media:credit"])
enclosure.credits << FeedTools::EnclosureCredit.new(
FeedTools::HtmlHelper.unescape_entities(credit.inner_xml),
FeedTools::HtmlHelper.unescape_entities(
credit.attributes["role"].to_s.downcase)
)
if enclosure.credits.last.role.blank?
enclosure.credits.last.role = nil
end
end
end
if enclosure.explicit?.nil?
enclosure.explicit =
((FeedTools::XmlHelper.try_xpaths(media_group, [
"media:adult/text()"
], :select_result_value => true).downcase == "true") ?
true : false)
end
enclosure_media_group_text =
FeedTools::XmlHelper.try_xpaths(media_group,
["media:text/text()"], :select_result_value => true)
if enclosure.text.nil? && !enclosure_media_group_text.blank?
enclosure.text = FeedTools::HtmlHelper.sanitize_html(
FeedTools::HtmlHelper.unescape_entities(
enclosure_media_group_text), :strip)
end
end
# Keep track of the media groups
media_groups << affected_enclosures
end
# Now we need to inherit any relevant item level information.
if self.explicit?
for enclosure in @enclosures
enclosure.explicit = true
end
end
# Add all the itunes categories
itunes_categories =
FeedTools::XmlHelper.try_xpaths_all(self.root_node,
["itunes:category"])
for itunes_category in itunes_categories
genre = "Podcasts"
category = itunes_category.attributes["text"].to_s
subcategory =
FeedTools::XmlHelper.try_xpaths(itunes_category,
["itunes:category/@text"],
:select_result_value => true)
category_path = genre
if !category.blank?
category_path << "/" + category
end
if !subcategory.blank?
category_path << "/" + subcategory
end
for enclosure in @enclosures
if enclosure.categories.nil?
enclosure.categories = []
end
enclosure.categories << FeedTools::Category.new
enclosure.categories.last.term =
FeedTools::HtmlHelper.unescape_entities(category_path)
enclosure.categories.last.scheme =
"http://www.apple.com/itunes/store/"
enclosure.categories.last.label =
"iTunes Music Store Categories"
end
end
for enclosure in @enclosures
# Clean up any of those attributes that incorrectly have ""
# or 0 as their values
if enclosure.type.blank?
enclosure.type = nil
end
if enclosure.file_size == 0
enclosure.file_size = nil
end
if enclosure.duration == 0
enclosure.duration = nil
end
if enclosure.height == 0
enclosure.height = nil
end
if enclosure.width == 0
enclosure.width = nil
end
if enclosure.bitrate == 0
enclosure.bitrate = nil
end
if enclosure.framerate == 0
enclosure.framerate = nil
end
if enclosure.expression.blank?
enclosure.expression = "full"
end
# If an enclosure is missing the text field, fall back on the
# itunes:summary field
if enclosure.text.blank?
enclosure.text = self.itunes_summary
end
# Make sure we don't have duplicate categories
unless enclosure.categories.nil?
enclosure.categories.uniq!
end
# Normalize enclosure URIs
if !enclosure.href.blank?
enclosure.href =
FeedTools::UriHelper.normalize_url(enclosure.href)
else
enclosure.href = nil
end
end
# And finally, now things get complicated. This is where we make
# sure that the enclosures method only returns either default
# enclosures or enclosures with only one version. Any enclosures
# that are wrapped in a media:group will be placed in the appropriate
# versions field.
affected_enclosure_urls = []
for media_group in media_groups
affected_enclosure_urls =
affected_enclosure_urls | (media_group.map do |enclosure|
enclosure.url
end)
end
@enclosures.delete_if do |enclosure|
(affected_enclosure_urls.include? enclosure.url)
end
for media_group in media_groups
default_enclosure = nil
for enclosure in media_group
if enclosure.is_default?
default_enclosure = enclosure
end
end
for enclosure in media_group
enclosure.default_version = default_enclosure
enclosure.versions = media_group.clone
enclosure.versions.delete(enclosure)
end
@enclosures << default_enclosure
end
end
# If we have a single enclosure, it's safe to inherit the
# itunes:duration field if it's missing.
if @enclosures.size == 1
if @enclosures.first.duration.nil? || @enclosures.first.duration == 0
@enclosures.first.duration = self.itunes_duration
end
end
return @enclosures
end
def enclosures=(new_enclosures)
@enclosures = new_enclosures
end
# Returns the feed item author
def author
if @author.nil?
@author = FeedTools::Author.new
author_node = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:author",
"atom03:author",
"atom:author",
"author",
"managingEditor",
"dc:author",
"dc:creator",
"creator"
])
unless author_node.nil?
@author.raw = FeedTools::XmlHelper.try_xpaths(
author_node, ["text()"], :select_result_value => true)
@author.raw = FeedTools::HtmlHelper.unescape_entities(@author.raw)
unless @author.raw.nil?
raw_scan = @author.raw.scan(
/(.*)\((\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\)/i)
if raw_scan.nil? || raw_scan.size == 0
raw_scan = @author.raw.scan(
/(\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\s*\((.*)\)/i)
unless raw_scan.size == 0
author_raw_pair = raw_scan.first.reverse
end
else
author_raw_pair = raw_scan.first
end
if raw_scan.nil? || raw_scan.size == 0
email_scan = @author.raw.scan(
/\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b/i)
if email_scan != nil && email_scan.size > 0
@author.email = email_scan.first.strip
end
end
unless author_raw_pair.nil? || author_raw_pair.size == 0
@author.name = author_raw_pair.first.strip
@author.email = author_raw_pair.last.strip
else
unless @author.raw.include?("@")
# We can be reasonably sure we are looking at something
# that the creator didn't intend to contain an email address
# if it got through the preceeding regexes and it doesn't
# contain the tell-tale '@' symbol.
@author.name = @author.raw
end
end
end
if @author.name.blank?
@author.name = FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(author_node, [
"atom10:name/text()",
"atom03:name/text()",
"atom:name/text()",
"name/text()",
"@name"
], :select_result_value => true)
)
end
if @author.email.blank?
@author.email = FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(author_node, [
"atom10:email/text()",
"atom03:email/text()",
"atom:email/text()",
"email/text()",
"@email"
], :select_result_value => true)
)
end
if @author.url.blank?
@author.url = FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(author_node, [
"atom10:url/text()",
"atom03:url/text()",
"atom:url/text()",
"url/text()",
"atom10:uri/text()",
"atom03:uri/text()",
"atom:uri/text()",
"uri/text()",
"@url",
"@uri",
"@href"
], :select_result_value => true)
)
end
if @author.name.blank? && !@author.raw.blank? &&
!@author.email.blank?
name_scan = @author.raw.scan(
/"?([^"]*)"? ?[\(<].*#{@author.email}.*[\)>].*/)
if name_scan.flatten.size == 1
@author.name = name_scan.flatten[0].strip
end
if @author.name.blank?
name_scan = @author.raw.scan(
/.*#{@author.email} ?[\(<]"?([^"]*)"?[\)>].*/)
if name_scan.flatten.size == 1
@author.name = name_scan.flatten[0].strip
end
end
end
@author.name = nil if @author.name.blank?
@author.raw = nil if @author.raw.blank?
@author.email = nil if @author.email.blank?
@author.url = nil if @author.url.blank?
if @author.url != nil
begin
if !(@author.url =~ /^file:/) &&
!FeedTools::UriHelper.is_uri?(@author.url)
@author.url = FeedTools::UriHelper.resolve_relative_uri(
@author.url, [author_node.base_uri, self.base_uri])
end
rescue
end
end
if FeedTools::XmlHelper.try_xpaths(author_node,
["@gr:unknown-author"], :select_result_value => true) == "true"
if @author.name == "(author unknown)"
@author.name = nil
end
end
end
# Fallback on the itunes module if we didn't find an author name
begin
@author.name = self.itunes_author if @author.name.nil?
rescue
@author.name = nil
end
if @author.name.blank? && @author.email.blank? &&
@author.href.blank?
parent_feed = self.feed
if parent_feed != nil
@author = parent_feed.author.dup
end
end
end
return @author
end
# Sets the feed item author
def author=(new_author)
if new_author.respond_to?(:name) &&
new_author.respond_to?(:email) &&
new_author.respond_to?(:url)
# It's a complete author object, just set it.
@author = new_author
else
# We're not looking at an author object, this is probably a string,
# default to setting the author's name.
if @author.nil?
@author = FeedTools::Author.new
end
@author.name = new_author
end
end
# Returns the feed publisher
def publisher
if @publisher.nil?
@publisher = FeedTools::Author.new
# Set the author name
@publisher.raw = FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(self.root_node, [
"dc:publisher/text()",
"webMaster/text()"
], :select_result_value => true))
unless @publisher.raw.blank?
raw_scan = @publisher.raw.scan(
/(.*)\((\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\)/i)
if raw_scan.nil? || raw_scan.size == 0
raw_scan = @publisher.raw.scan(
/(\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\s*\((.*)\)/i)
unless raw_scan.size == 0
publisher_raw_pair = raw_scan.first.reverse
end
else
publisher_raw_pair = raw_scan.first
end
if raw_scan.nil? || raw_scan.size == 0
email_scan = @publisher.raw.scan(
/\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b/i)
if email_scan != nil && email_scan.size > 0
@publisher.email = email_scan.first.strip
end
end
unless publisher_raw_pair.nil? || publisher_raw_pair.size == 0
@publisher.name = publisher_raw_pair.first.strip
@publisher.email = publisher_raw_pair.last.strip
else
unless @publisher.raw.include?("@")
# We can be reasonably sure we are looking at something
# that the creator didn't intend to contain an email address if
# it got through the preceeding regexes and it doesn't
# contain the tell-tale '@' symbol.
@publisher.name = @publisher.raw
end
end
end
@publisher.name = nil if @publisher.name.blank?
@publisher.raw = nil if @publisher.raw.blank?
@publisher.email = nil if @publisher.email.blank?
@publisher.url = nil if @publisher.url.blank?
if @publisher.url != nil
begin
if !(@publisher.url =~ /^file:/) &&
!FeedTools::UriHelper.is_uri?(@publisher.url)
root_base_uri = nil
unless self.root_node.nil?
root_base_uri = self.root_node.base_uri
end
@publisher.url = FeedTools::UriHelper.resolve_relative_uri(
@publisher.url, [root_base_uri, self.base_uri])
end
rescue
end
end
if @publisher.name.blank? && @publisher.email.blank? &&
@publisher.href.blank?
parent_feed = self.feed
if parent_feed != nil
@publisher = parent_feed.publisher.dup
end
end
end
return @publisher
end
# Sets the feed publisher
def publisher=(new_publisher)
if new_publisher.respond_to?(:name) &&
new_publisher.respond_to?(:email) &&
new_publisher.respond_to?(:url)
# It's a complete Author object, just set it.
@publisher = new_publisher
else
# We're not looking at an Author object, this is probably a string,
# default to setting the publisher's name.
if @publisher.nil?
@publisher = FeedTools::Author.new
end
@publisher.name = new_publisher
end
end
# Returns the contents of the itunes:author element
#
# This inherits from any incorrectly placed channel-level itunes:author
# elements. They're actually amazingly common. People don't read specs.
def itunes_author
if @itunes_author.nil?
@itunes_author = FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(self.root_node,
["itunes:author/text()"], :select_result_value => true))
if @itunes_author.blank?
parent_feed = self.feed
if parent_feed != nil
@itunes_author = parent_feed.itunes_author
end
end
end
return @itunes_author
end
# Sets the contents of the itunes:author element
def itunes_author=(new_itunes_author)
@itunes_author = new_itunes_author
end
# Returns the number of seconds that the associated media runs for
def itunes_duration
if @itunes_duration.nil?
raw_duration = FeedTools::HtmlHelper.unescape_entities(
FeedTools::XmlHelper.try_xpaths(self.root_node,
["itunes:duration/text()"], :select_result_value => true))
if !raw_duration.blank?
hms = raw_duration.split(":").map { |x| x.to_i }
if hms.size == 3
@itunes_duration = hms[0].hours + hms[1].minutes + hms[2]
elsif hms.size == 2
@itunes_duration = hms[0].minutes + hms[1]
elsif hms.size == 1
@itunes_duration = hms[0]
end
end
end
return @itunes_duration
end
# Sets the number of seconds that the associate media runs for
def itunes_duration=(new_itunes_duration)
@itunes_duration = new_itunes_duration
end
# Returns the feed item time
def time(options = {})
FeedTools::GenericHelper.validate_options([ :estimate_timestamp ],
options.keys)
options = { :estimate_timestamp => true }.merge(options)
if @time.nil?
time_string = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:updated/text()",
"atom03:updated/text()",
"atom:updated/text()",
"updated/text()",
"atom10:modified/text()",
"atom03:modified/text()",
"atom:modified/text()",
"modified/text()",
"time/text()",
"lastBuildDate/text()",
"atom10:issued/text()",
"atom03:issued/text()",
"atom:issued/text()",
"issued/text()",
"atom10:published/text()",
"atom03:published/text()",
"atom:published/text()",
"published/text()",
"dc:date/text()",
"pubDate/text()",
"date/text()",
"lastupdated/text()"
], :select_result_value => true)
begin
if !time_string.blank?
@time = Time.parse(time_string).gmtime
elsif self.configurations[:timestamp_estimation_enabled] &&
!self.title.nil? &&
(Time.parse(self.title) - Time.now).abs > 100
@time = Time.parse(self.title).gmtime
end
rescue
end
if self.configurations[:timestamp_estimation_enabled]
if options[:estimate_timestamp]
if @time.nil?
begin
@time = succ_time
if @time.nil?
@time = prev_time
end
rescue
end
if @time.nil?
@time = Time.now.gmtime
end
end
end
end
end
return @time
end
# Sets the feed item time
def time=(new_time)
@time = new_time
end
# Returns 1 second after the previous item's time.
def succ_time #:nodoc:
begin
parent_feed = self.feed
if parent_feed.nil?
return nil
end
if parent_feed.instance_variable_get("@entries").nil?
parent_feed.items
end
unsorted_items = parent_feed.instance_variable_get("@entries")
item_index = unsorted_items.index(self)
if item_index.nil?
return nil
end
if item_index <= 0
return nil
end
previous_item = unsorted_items[item_index - 1]
return (previous_item.time(:estimate_timestamp => false) + 1)
rescue
return nil
end
end
private :succ_time
# Returns 1 second before the succeeding item's time.
def prev_time #:nodoc:
begin
parent_feed = self.feed
if parent_feed.nil?
return nil
end
if parent_feed.instance_variable_get("@entries").nil?
parent_feed.items
end
unsorted_items = parent_feed.instance_variable_get("@entries")
item_index = unsorted_items.index(self)
if item_index.nil?
return nil
end
if item_index >= (unsorted_items.size - 1)
return nil
end
succeeding_item = unsorted_items[item_index + 1]
return (succeeding_item.time(:estimate_timestamp => false) - 1)
rescue
return nil
end
end
private :prev_time
# Returns the feed item updated time
def updated
if @updated.nil?
updated_string = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:updated/text()",
"atom03:updated/text()",
"atom:updated/text()",
"updated/text()",
"atom10:modified/text()",
"atom03:modified/text()",
"atom:modified/text()",
"modified/text()",
"lastBuildDate/text()",
"lastupdated/text()"
], :select_result_value => true)
if !updated_string.blank?
@updated = Time.parse(updated_string).gmtime rescue nil
else
@updated = nil
end
end
return @updated
end
# Sets the feed item updated time
def updated=(new_updated)
@updated = new_updated
end
# Returns the feed item published time
def published
if @published.nil?
published_string = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"atom10:issued/text()",
"atom03:issued/text()",
"atom:issued/text()",
"issued/text()",
"atom10:published/text()",
"atom03:published/text()",
"atom:published/text()",
"published/text()",
"dc:date/text()",
"pubDate/text()",
"date/text()"
], :select_result_value => true)
if !published_string.blank?
@published = Time.parse(published_string).gmtime rescue nil
else
@published = nil
end
end
return @published
end
# Sets the feed item published time
def published=(new_published)
@published = new_published
end
# TODO: FIX ME! This code is completely wrong.
# The source that this post was based on
def source
if @source.nil?
@source = FeedTools::Link.new
@source.href = FeedTools::XmlHelper.try_xpaths(
self.root_node, ["source/@url"],
:select_result_value => true)
@source.title = FeedTools::XmlHelper.try_xpaths(
self.root_node, ["source/text()"],
:select_result_value => true)
end
return @source
end
# Returns the feed item tags
def tags
# TODO: support the rel="tag" microformat
# =======================================
if @tags.nil?
@tags = []
if root_node.nil?
return @tags
end
if @tags.nil? || @tags.size == 0
@tags = []
tag_list = FeedTools::XmlHelper.try_xpaths_all(self.root_node,
["dc:subject/rdf:Bag/rdf:li/text()"],
:select_result_value => true)
if tag_list != nil && tag_list.size > 0
for tag in tag_list
@tags << tag.downcase.strip
end
end
end
if @tags.nil? || @tags.size == 0
# messy effort to find ourselves some tags, mainly for del.icio.us
@tags = []
rdf_bag = FeedTools::XmlHelper.try_xpaths_all(self.root_node,
["taxo:topics/rdf:Bag/rdf:li"])
if rdf_bag != nil && rdf_bag.size > 0
for tag_node in rdf_bag
begin
tag_url = FeedTools::XmlHelper.try_xpaths(tag_node,
["@resource"],
:select_result_value => true)
tag_match = tag_url.scan(/\/(tag|tags)\/(\w+)$/)
if tag_match.size > 0
@tags << tag_match.first.last.downcase.strip
end
rescue
end
end
end
end
if @tags.nil? || @tags.size == 0
@tags = []
tag_list = FeedTools::XmlHelper.try_xpaths_all(self.root_node,
["category/text()"],
:select_result_value => true)
for tag in tag_list
@tags << tag.to_s.downcase.strip
end
end
if @tags.nil? || @tags.size == 0
@tags = []
tag_list = FeedTools::XmlHelper.try_xpaths_all(self.root_node,
["dc:subject/text()"],
:select_result_value => true)
for tag in tag_list
@tags << tag.to_s.downcase.strip
end
end
if @tags.blank?
begin
itunes_keywords_string =
FeedTools::XmlHelper.try_xpaths(self.root_node, [
"itunes:keywords/text()"
], :select_result_value => true)
unless itunes_keywords_string.blank?
@tags = itunes_keywords_string.downcase.split(",")
if @tags.size == 1
@tags = itunes_keywords_string.downcase.split(" ")
@tags = @tags.map { |tag| tag.chomp(",") }
end
if @tags.size == 1
@tags = itunes_keywords_string.downcase.split(",")
end
@tags = @tags.map { |tag| tag.strip }
end
rescue
@tags = []
end
end
if @tags.nil?
@tags = []
end
@tags.uniq!
end
return @tags
end
# Sets the feed item tags
def tags=(new_tags)
@tags = new_tags
end
# Returns true if this feed item contains explicit material. If the whole
# feed has been marked as explicit, this will return true even if the item
# isn't explicitly marked as explicit.
def explicit?
if @explicit.nil?
explicit_string = FeedTools::XmlHelper.try_xpaths(self.root_node, [
"media:adult/text()",
"itunes:explicit/text()"
], :select_result_value => true)
parent_feed = self.feed
if explicit_string == "true" || explicit_string == "yes"
@explicit = true
elsif parent_feed != nil && parent_feed.explicit?
@explicit = true
else
@explicit = false
end
end
return @explicit
end
# Sets whether or not the feed contains explicit material
def explicit=(new_explicit)
@explicit = (new_explicit ? true : false)
end
# A hook method that is called during the feed generation process.
# Overriding this method will enable additional content to be inserted
# into the feed.
def build_xml_hook(feed_type, version, xml_builder)
return nil
end
# Generates xml based on the content of the feed item
def build_xml(feed_type=(self.feed.feed_type or "atom"), version=nil,
xml_builder=Builder::XmlMarkup.new(
:indent => 2, :escape_attrs => false))
parent_feed = self.feed
if parent_feed.find_node(
"access:restriction/@relationship").to_s == "deny"
raise StandardError,
"Operation not permitted. This feed denies redistribution."
elsif parent_feed.find_node("@indexing:index").to_s == "no"
raise StandardError,
"Operation not permitted. This feed denies redistribution."
end
if self.find_node(
"access:restriction/@relationship").to_s == "deny"
raise StandardError,
"Operation not permitted. This feed item denies redistribution."
end
self.full_parse()
if feed_type == "rss" && (version == nil || version == 0.0)
version = 1.0
elsif feed_type == "atom" && (version == nil || version == 0.0)
version = 1.0
end
if feed_type == "rss" &&
(version == 0.9 || version == 1.0 || version == 1.1)
# RDF-based rss format
if link.nil?
raise "Cannot generate an rdf-based feed item with a " +
"nil link field."
end
return xml_builder.item("rdf:about" =>
FeedTools::HtmlHelper.escape_entities(link)) do
unless self.title.blank?
xml_builder.title(
FeedTools::HtmlHelper.strip_html_tags(self.title))
else
xml_builder.title
end
unless self.link.blank?
xml_builder.link(self.link)
else
xml_builder.link
end
unless self.author.nil? || self.author.name.nil?
xml_builder.tag!("dc:creator", self.author.name)
end
unless self.summary.blank?
xml_builder.description(self.summary)
else
xml_builder.description
end
unless self.content.blank?
xml_builder.tag!("content:encoded") do
xml_builder.cdata!(self.content)
end
end
unless time.nil?
xml_builder.tag!("dc:date", time.iso8601)
end
unless self.rights.blank?
xml_builder.tag!("dc:rights", self.rights)
end
unless tags.nil? || tags.size == 0
for tag in tags
xml_builder.tag!("dc:subject", tag)
end
if self.feed.podcast?
xml_builder.tag!("itunes:keywords", tags.join(", "))
end
end
build_xml_hook(feed_type, version, xml_builder)
end
elsif feed_type == "rss"
# normal rss format
return xml_builder.item do
unless self.title.blank?
xml_builder.title(
FeedTools::HtmlHelper.strip_html_tags(self.title))
end
unless self.link.blank?
xml_builder.link(self.link)
end
unless self.author.nil? || self.author.name.nil?
xml_builder.tag!("dc:creator", self.author.name)
end
unless self.author.nil? || self.author.email.nil? ||
self.author.name.nil?
xml_builder.author("#{self.author.email} (#{self.author.name})")
end
unless self.summary.blank?
xml_builder.description(self.summary)
end
unless self.content.blank?
xml_builder.tag!("content:encoded") do
xml_builder.cdata!(self.content)
end
end
if !self.published.nil?
xml_builder.pubDate(self.published.rfc822)
elsif !self.time.nil?
xml_builder.pubDate(self.time.rfc822)
end
unless self.rights.blank?
xml_builder.tag!("dc:rights", self.rights)
end
unless self.guid.blank?
if FeedTools::UriHelper.is_uri?(self.guid) &&
(self.guid =~ /^http/)
xml_builder.guid(self.guid, "isPermaLink" => "true")
else
xml_builder.guid(self.guid, "isPermaLink" => "false")
end
else
unless self.link.blank?
xml_builder.guid(self.link, "isPermaLink" => "true")
end
end
unless tags.nil? || tags.size == 0
for tag in tags
xml_builder.tag!("category", tag)
end
if self.feed.podcast?
xml_builder.tag!("itunes:keywords", tags.join(", "))
end
end
unless self.enclosures.blank? || self.enclosures.size == 0
for enclosure in self.enclosures
attribute_hash = {}
next if enclosure.url.blank?
begin
if enclosure.file_size.blank? || enclosure.file_size.to_i == 0
# We can't use this enclosure because it's missing the
# required file size. Check alternate versions for
# file_size.
if !enclosure.versions.blank? && enclosure.versions.size > 0
for alternate in enclosure.versions
if alternate.file_size != nil &&
alternate.file_size.to_i > 0
enclosure = alternate
break
end
end
end
end
rescue
end
attribute_hash["url"] =
FeedTools::UriHelper.normalize_url(enclosure.url)
if enclosure.type != nil
attribute_hash["type"] = enclosure.type
end
if enclosure.file_size != nil && enclosure.file_size.to_i > 0
attribute_hash["length"] = enclosure.file_size.to_s
else
# We couldn't find an alternate and the problem is still
# there. Give up and go on.
xml_builder.comment!(
"*** Enclosure failed to include file size. Ignoring. ***")
next
end
xml_builder.enclosure(attribute_hash)
end
end
build_xml_hook(feed_type, version, xml_builder)
end
elsif feed_type == "atom" && version == 0.3
raise "Atom 0.3 is obsolete."
elsif feed_type == "atom" && version == 1.0
# normal atom format
return xml_builder.entry("xmlns" =>
FEED_TOOLS_NAMESPACES['atom10']) do
unless title.nil? || title == ""
xml_builder.title(
FeedTools::HtmlHelper.strip_html_tags(self.title),
"type" => "html")
end
xml_builder.author do
unless self.author.nil? || self.author.name.nil?
xml_builder.name(self.author.name)
else
xml_builder.name("n/a")
end
unless self.author.nil? || self.author.email.nil?
xml_builder.email(self.author.email)
end
unless self.author.nil? || self.author.url.nil?
xml_builder.uri(self.author.url)
end
end
unless link.nil? || link == ""
xml_builder.link(
"href" =>
FeedTools::HtmlHelper.escape_entities(self.link),
"rel" => "alternate")
end
if !self.content.blank?
xml_builder.content(self.content,
"type" => "html")
end
if !self.summary.blank?
xml_builder.summary(self.summary,
"type" => "html")
end
if self.updated != nil
xml_builder.updated(self.updated.iso8601)
elsif self.time != nil
# Not technically correct, but a heck of a lot better
# than the Time.now fall-back.
xml_builder.updated(self.time.iso8601)
else
xml_builder.updated(Time.now.gmtime.iso8601)
end
unless self.published.nil?
xml_builder.published(self.published.iso8601)
end
unless self.rights.blank?
xml_builder.rights(self.rights)
end
if self.id != nil
unless FeedTools::UriHelper.is_uri? self.id
if self.time != nil && self.link != nil
xml_builder.id(FeedTools::UriHelper.build_tag_uri(
self.link, self.time))
elsif self.link != nil
xml_builder.id(FeedTools.build_urn_uuid_uri(self.link))
else
raise "The unique id must be a URI. " +
"(Attempted to generate id, but failed.)"
end
else
xml_builder.id(self.id)
end
elsif self.time != nil && self.link != nil
xml_builder.id(FeedTools::UriHelper.build_tag_uri(
self.link, self.time))
else
raise "Cannot build feed, missing feed unique id."
end
unless self.tags.nil? || self.tags.size == 0
for tag in self.tags
xml_builder.category("term" => tag)
end
end
unless self.enclosures.blank? || self.enclosures.size == 0
for enclosure in self.enclosures
attribute_hash = {}
next if enclosure.url.blank?
attribute_hash["rel"] = "enclosure"
attribute_hash["href"] =
FeedTools::UriHelper.normalize_url(enclosure.url)
if enclosure.type != nil
attribute_hash["type"] = enclosure.type
end
if enclosure.file_size != nil && enclosure.file_size.to_i > 0
attribute_hash["length"] = enclosure.file_size.to_s
end
xml_builder.link(attribute_hash)
end
end
build_xml_hook(feed_type, version, xml_builder)
end
else
raise "Unsupported feed format/version."
end
end
alias_method :abstract, :summary
alias_method :abstract=, :summary=
alias_method :description, :summary
alias_method :description=, :summary=
alias_method :copyright, :rights
alias_method :copyright=, :rights=
alias_method :guid, :id
alias_method :guid=, :id=
# Returns a simple representation of the feed item object's state.
def inspect
return "#<FeedTools::FeedItem:0x#{self.object_id.to_s(16)} " +
"LINK:#{self.link}>"
end
end
end
|