Class: Tumblr4r::XMLParser
- Inherits:
-
Object
- Object
- Tumblr4r::XMLParser
- Defined in:
- lib/tumblr4r.rb
Overview
Tumblr XML API
Instance Method Summary collapse
- #answer(post, rexml_post) ⇒ Object
- #audio(post, rexml_post) ⇒ Object
- #chat(post, rexml_post) ⇒ Object
- #link(post, rexml_post) ⇒ Object
- #photo(post, rexml_post) ⇒ Object
-
#post(post, rexml_post) ⇒ Object
TODO: この辺りの設計についてはもう少し考慮の余地がある? みんな同じような構造(まずはpost(post, rexml_post)呼んでその後独自処理)してるし、 引数にpostとrexml_postをもらってくるってのもなんかイケてない気がする。.
-
#posts(xml) ⇒ Array<Post>, Integer
XMLをパースしてオブジェクトのArrayを作る.
- #quote(post, rexml_post) ⇒ Object
- #regular(post, rexml_post) ⇒ Object
- #siteinfo(site, xml) ⇒ Object
- #video(post, rexml_post) ⇒ Object
Instance Method Details
#answer(post, rexml_post) ⇒ Object
628 629 630 631 632 633 |
# File 'lib/tumblr4r.rb', line 628 def answer(post, rexml_post) post = self.post(post, rexml_post) post.question = rexml_post.elements["question"].try(:text) || "" post.answer = rexml_post.elements["answer"].try(:text) || "" post end |
#audio(post, rexml_post) ⇒ Object
612 613 614 615 616 617 618 |
# File 'lib/tumblr4r.rb', line 612 def audio(post, rexml_post) post = self.post(post, rexml_post) post.audio_plays = (rexml_post.attributes["audio-plays"] == "1") post. = rexml_post.elements["audio-caption"].try(:text) || "" post.audio_player = rexml_post.elements["audio-player"].try(:text) || "" post end |
#chat(post, rexml_post) ⇒ Object
605 606 607 608 609 610 |
# File 'lib/tumblr4r.rb', line 605 def chat(post, rexml_post) post = self.post(post, rexml_post) post.conversation_title = rexml_post.elements["conversation-title"].try(:text) || "" post.conversation_text = rexml_post.elements["conversation-text"].try(:text) || "" post end |
#link(post, rexml_post) ⇒ Object
597 598 599 600 601 602 603 |
# File 'lib/tumblr4r.rb', line 597 def link(post, rexml_post) post = self.post(post, rexml_post) post.link_text = rexml_post.elements["link-text"].try(:text) || "" post.link_url = rexml_post.elements["link-url"].try(:text) || "" post.link_description = rexml_post.elements["link-description"].try(:text) || "" post end |
#photo(post, rexml_post) ⇒ Object
577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/tumblr4r.rb', line 577 def photo(post, rexml_post) post = self.post(post, rexml_post) post.type post. = rexml_post.elements["photo-caption"].try(:text) || "" post.photo_link_url = rexml_post.elements["photo-link-url"].try(:text) || "" post.photo_url = rexml_post.elements["photo-url"].try(:text) || "" post.photoset = [] rexml_post.elements.each("photoset/photo") do |photo| post.photoset.push(photo.elements["photo-url"].try(:text) || "") end post end |
#post(post, rexml_post) ⇒ Object
TODO: この辺りの設計についてはもう少し考慮の余地がある? みんな同じような構造(まずはpost(post, rexml_post)呼んでその後独自処理)してるし、 引数にpostとrexml_postをもらってくるってのもなんかイケてない気がする。
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/tumblr4r.rb', line 553 def post(post, rexml_post) post.post_id = rexml_post.attributes["id"].to_i post.url = rexml_post.attributes["url"] post.url_with_slug = rexml_post.attributes["url-with-slug"] post.liked = (rexml_post.attributes["liked"] == "true") post.reblog_key = rexml_post.attributes["reblog-key"] post.type = rexml_post.attributes["type"] # TODO: time 関係の型をStringじゃなくTimeとかにする? post.date_gmt = rexml_post.attributes["date-gmt"] post.date = rexml_post.attributes["date"] post. = rexml_post.attributes["unix-timestamp"].to_i post.format = rexml_post.attributes["format"] post. = rexml_post.get_elements("tag").map(&:text) post.bookmarklet = (rexml_post.attributes["bookmarklet"] == "true") post end |
#posts(xml) ⇒ Array<Post>, Integer
XMLをパースしてオブジェクトのArrayを作る
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 |
# File 'lib/tumblr4r.rb', line 516 def posts(xml) rexml_doc = REXML::Document.new(xml) rexml_posts = REXML::XPath.first(rexml_doc, "//tumblr/posts") start = rexml_posts.attributes["start"] total = rexml_posts.attributes["total"] posts = [] rexml_posts.elements.each("//posts/post") do |rexml_post| post_type = rexml_post.attributes["type"] post = nil case post_type when POST_TYPE::REGULAR post = self.regular(Regular.new, rexml_post) when POST_TYPE::PHOTO post = self.photo(Photo.new, rexml_post) when POST_TYPE::QUOTE post = self.quote(Quote.new, rexml_post) when POST_TYPE::LINK post = self.link(Link.new, rexml_post) when POST_TYPE::CHAT post = self.chat(Chat.new, rexml_post) when POST_TYPE::AUDIO post = self.audio(Audio.new, rexml_post) when POST_TYPE::VIDEO post = self.video(Video.new, rexml_post) when POST_TYPE::ANSWER post = self.answer(Answer.new, rexml_post) else raise TumblrError.new("unknown post type #{post_type}") end posts << post end return posts, start.to_i, total.to_i end |
#quote(post, rexml_post) ⇒ Object
590 591 592 593 594 595 |
# File 'lib/tumblr4r.rb', line 590 def quote(post, rexml_post) post = self.post(post, rexml_post) post.quote_text = rexml_post.elements["quote-text"].try(:text) || "" post.quote_source = rexml_post.elements["quote-source"].try(:text) || "" post end |
#regular(post, rexml_post) ⇒ Object
570 571 572 573 574 575 |
# File 'lib/tumblr4r.rb', line 570 def regular(post, rexml_post) post = self.post(post, rexml_post) post.regular_title = rexml_post.elements["regular-title"].try(:text) || "" post.regular_body = rexml_post.elements["regular-body"].try(:text) || "" post end |
#siteinfo(site, xml) ⇒ Object
500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/tumblr4r.rb', line 500 def siteinfo(site, xml) xml_doc = REXML::Document.new(xml) tumblelog = REXML::XPath.first(xml_doc, "//tumblr/tumblelog") site.name = tumblelog.attributes["name"] site.timezone = tumblelog.attributes["timezone"] site.title = tumblelog.attributes["title"] site.cname = tumblelog.attributes["cname"] site.description = tumblelog.text # tumblelog.elements["/feeds"]} # TODO: feeds は後回し return site end |
#video(post, rexml_post) ⇒ Object
620 621 622 623 624 625 626 |
# File 'lib/tumblr4r.rb', line 620 def video(post, rexml_post) post = self.post(post, rexml_post) post. = rexml_post.elements["video-caption"].try(:text) || "" post.video_source = rexml_post.elements["video-source"].try(:text) || "" post.video_player = rexml_post.elements["video-player"].try(:text) || "" post end |