Class: Atom::Pub::Collection

Inherits:
Object
  • Object
show all
Includes:
Xml::Parseable
Defined in:
lib/atom/pub.rb

Instance Method Summary collapse

Methods included from Xml::Parseable

#==, #accessor_name, #current_node_is?, included, #next_node_is?, #parse, #to_xml

Constructor Details

#initialize(o = nil) {|_self| ... } ⇒ Collection

Returns a new instance of Collection.

Yields:

  • (_self)

Yield Parameters:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/atom/pub.rb', line 103

def initialize(o = nil)
  @accepts = []
  case o
  when Nokogiri::XML::Reader
    # do it once to get the attributes
    parse(o, :once => true)
    # now step into the element and the sub tree
    o.read
    parse(o)
  when Hash
    o.each do |k, v|
      self.send("#{k}=", v)
    end
  end

  yield(self) if block_given?
end

Instance Method Details

#feed(opts = {}) ⇒ Object



121
122
123
124
125
# File 'lib/atom/pub.rb', line 121

def feed(opts = {})
  if href
    Atom::Feed.load_feed(URI.parse(href), opts)
  end
end

#publish(entry, opts = {}) ⇒ Object



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
# File 'lib/atom/pub.rb', line 127

def publish(entry, opts = {})
  uri = URI.parse(href)
  response = nil
  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Post.new(uri.request_uri, headers)
    if opts[:user] && opts[:pass]
      request.basic_auth(opts[:user], opts[:pass])
    elsif opts[:hmac_access_id] && opts[:hmac_secret_key]
      if Atom::Configuration.auth_hmac_enabled?
        AuthHMAC.sign!(request, opts[:hmac_access_id], opts[:hmac_secret_key])
      else
        raise ArgumentError, "AuthHMAC credentials provides by auth-hmac gem is not installed"
      end
    end
    response = http.request(request, entry.to_xml.to_s)
  end

  case response
  when Net::HTTPCreated
    published = begin
      Atom::Entry.load_entry(response.body)
    rescue ArgumentError
      entry
    end

    if response['Location']
      if published.edit_link
        published.edit_link.href = response['Location']
      else
        published.links << Atom::Link.new(:rel => 'edit', :href => response['Location'])
      end
    end

    published
  else
    raise Atom::Pub::ProtocolError, response
  end
end