Class: TwitterServer::Renderer::NokogiriRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/renderers/nokogiri_renderer.rb

Overview

Base Renderer classes should possibly store schemas for the objects (status, user, etc). Do some basic ruby type conversions for the format the api expects.

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ NokogiriRenderer

Returns a new instance of NokogiriRenderer.



7
8
9
10
# File 'lib/renderers/nokogiri_renderer.rb', line 7

def initialize(root)
  @xml = Nokogiri::XML::Builder.new
  node(root) { yield self }
end

Instance Method Details

#node(key) ⇒ Object



12
13
14
15
16
17
# File 'lib/renderers/nokogiri_renderer.rb', line 12

def node(key)
  r = self
  @xml.send key do
    yield r
  end
end

#status(status) ⇒ Object

<created_at>Tue Apr 07 22:52:51 +0000 2009</created_at> <id>1472669360</id> <text>At least I can get your humor through tweets. RT @abdur: I don’t mean this in a bad way, but genetically speaking your a cul-de-sac.</text> <source>&lt;a href=“www.tweetdeck.com/”>TweetDeck&lt;/a></source> <truncated>false</truncated> <in_reply_to_status_id></in_reply_to_status_id> <in_reply_to_user_id></in_reply_to_user_id> <favorited>false</favorited> <in_reply_to_screen_name></in_reply_to_screen_name>



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/renderers/nokogiri_renderer.rb', line 70

def status(status)
  return if status.nil? || status.empty?
  [:id, :created_at, :text, :truncated, :in_reply_to_status_id, :in_reply_to_user_id, :favorited, :in_reply_to_screen_name].each do |key|
    @xml.send("#{key}_", status[key]) if status.key?(key)
  end
  if status.key?(:user)
    node :user do
      user(status[:user])
    end
  end
  if status.key?(:source)
    if status.key?(:source_href)
      node :source do
        @xml.a(status[:source], :href => status[:source_href])
      end
    else
      @xml.source status[:source]
    end
  end
end

#to_sObject



19
20
21
# File 'lib/renderers/nokogiri_renderer.rb', line 19

def to_s
  @xml.to_xml
end

#user(user) ⇒ Object

<id>1401881</id> <name>Doug Williams</name> <screen_name>dougw</screen_name> <location>San Francisco, CA</location> <description>Twitter API Support. Internet, greed, users, dougw and opportunities are my passions.</description> <profile_image_url></profile_image_url> <url>www.igudo.com</url> <protected>false</protected> <followers_count>1031</followers_count> <profile_background_color>9ae4e8</profile_background_color> <profile_text_color>000000</profile_text_color> <profile_link_color>0000ff</profile_link_color> <profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color> <profile_sidebar_border_color>87bc44</profile_sidebar_border_color> <friends_count>293</friends_count> <created_at>Sun Mar 18 06:42:26 +0000 2007</created_at> <favourites_count>0</favourites_count> <utc_offset>-18000</utc_offset> <time_zone>Eastern Time (US & Canada)</time_zone> <profile_background_image_url></profile_background_image_url> <profile_background_tile>false</profile_background_tile> <statuses_count>3390</statuses_count> <notifications>false</notifications> <following>false</following> <geo_enabled>true</geo_enabled> <!– Not yet part of the current payload. [COMING SOON] –> <verified>true</verified>



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/renderers/nokogiri_renderer.rb', line 49

def user(user)
  return if user.nil? || user.empty?
  [:id, :name, :screen_name].each do |key|
    @xml.send "#{key}_", user[key] if user.key?(key)
  end
  if user.key?(:status)
    node(:status) do
      status(user[:status])
    end
  end
end