Class: Atompub::Client

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

Overview

Atompub::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Initializer

  • auth

  • cache



1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
# File 'lib/atomutil.rb', line 1316

def initialize(params={})
  unless params.has_key?(:auth)
    throw ArgumentError.new("Atompub::Client needs :auth as argument for constructor.")
  end
  @auth  = params.has_key?(:auth) && params[:auth].kind_of?(Auth::Abstract) ? params[:auth] : Auth::Abstract.new
  @cache = params.has_key?(:cache) && params[:cache].kind_of?(AbstractCache) ? params[:cache] : AbstractCache.instance
  @service_info = params.has_key?(:info) && params[:info].kind_of?(ServiceInfoStorage) ? params[:info] : ServiceInfoStorage.instance
  @http_class = Net::HTTP
  @agent = "Atompub::Client/#{AtomUtil::VERSION}"
end

Instance Attribute Details

#agentObject

user agent



1301
1302
1303
# File 'lib/atomutil.rb', line 1301

def agent
  @agent
end

#rcObject (readonly) Also known as: resource

resource object for current networking context



1309
1310
1311
# File 'lib/atomutil.rb', line 1309

def rc
  @rc
end

#reqObject (readonly) Also known as: request

request object for current networking context



1303
1304
1305
# File 'lib/atomutil.rb', line 1303

def req
  @req
end

#resObject (readonly) Also known as: response

response object for current networking context



1306
1307
1308
# File 'lib/atomutil.rb', line 1306

def res
  @res
end

Instance Method Details

#create_entry(post_uri, entry, slug = nil) ⇒ Object

Create new entry

Example:

entry = Atom::Entry.new
entry.title = 'foo'
author = Atom::Author.new
author.name = 'Lyo Kato'
author.email = '[email protected]'
entry.author = author
entry_uri = client.create_entry(post_uri, entry)


1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
# File 'lib/atomutil.rb', line 1429

def create_entry(post_uri, entry, slug=nil)
  unless entry.kind_of?(Atom::Entry)
    entry = Atom::Entry.new :stream => entry
  end
  service = @service_info.get(post_uri)
  unless entry.categories.all?{ |c| service.allows_category?(c) }
    raise RequestError, "Forbidden Category"
  end
  create_resource(post_uri, entry.to_s, Atom::MediaType::ENTRY.to_s, slug)
  @res['Location']
end

#create_media(media_uri, file_path, content_type, slug = nil) ⇒ Object

Create new media resource

Example:

media_uri = client.create_media(post_media_uri, 'myimage.jpg', 'image/jpeg')


1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
# File 'lib/atomutil.rb', line 1446

def create_media(media_uri, file_path, content_type, slug=nil)
  file_path = Pathname.new(file_path) unless file_path.is_a?(Pathname)
  stream = file_path.open { |f| f.binmode; f.read }
  service = @service_info.get(media_uri)
  if service.nil?
    raise RequestError, "Service information not found. Get service document before you do create_media."
  end
  unless service.accepts_media_type?(content_type)
    raise RequestError, "Unsupported Media Type: #{content_type}"
  end
  create_resource(media_uri, stream, content_type, slug)
  @res['Location']
end

#delete_entry(edit_uri) ⇒ Object

Delete entry

Example:

entry = client.get_entry(resource_uri)
client.delete_entry(entry.edit_link)


1492
1493
1494
# File 'lib/atomutil.rb', line 1492

def delete_entry(edit_uri)
  delete_resource(edit_uri) 
end

#delete_media(media_uri) ⇒ Object

Delete media

Example:

entry = client.get_entry(resource_uri)
client.delete_media(entry.edit_media_link)


1502
1503
1504
# File 'lib/atomutil.rb', line 1502

def delete_media(media_uri)
  delete_resource(media_uri)
end

#get_categories(categories_uri) ⇒ Object

Get categories This returns Atom::Categories object. see the document of Atom::Categories in detail.

Example:



1369
1370
1371
1372
1373
1374
1375
# File 'lib/atomutil.rb', line 1369

def get_categories(categories_uri)
  get_contents_except_resources(categories_uri) do |res|
    #warn "Bad Content Type" unless Atom::MediaType::CATEGORIES.is_a?(@res['Content-Type'])
    @rc = Atom::Categories.new :stream => @res.body
  end
  @rc
end

#get_entry(entry_uri) ⇒ Object

Get entry

Example:

entry = client.get_entry(entry_uri)
puts entry.id
puts entry.title


1397
1398
1399
1400
1401
1402
1403
# File 'lib/atomutil.rb', line 1397

def get_entry(entry_uri)
  get_resource(entry_uri)
  unless @rc.instance_of?(Atom::Entry)
    raise ResponseError, "Response is not Atom Entry"
  end
  @rc
end

#get_feed(feed_uri) ⇒ Object

Get feed This returns Atom::Feed object. see the document of Atom::Feed in detail.

Example:



1382
1383
1384
1385
1386
1387
1388
# File 'lib/atomutil.rb', line 1382

def get_feed(feed_uri)
  get_contents_except_resources(feed_uri) do |res|
    #warn "Bad Content Type" unless Atom::MediaType::FEED.is_a?(@res['Content-Type'])
    @rc = Atom::Feed.new :stream => res.body
  end
  @rc
end

#get_media(media_uri) ⇒ Object

Get media resource

Example:

resource, content_type = client.get_media(media_uri)


1410
1411
1412
1413
1414
1415
1416
# File 'lib/atomutil.rb', line 1410

def get_media(media_uri)
  get_resource(media_uri)
  if @rc.instance_of?(Atom::Entry)
    raise ResponseError, "Response is not Media Resource"
  end
  return @rc, @res.content_type
end

#get_service(service_uri) ⇒ Object

Get service document This returns Atom::Service object. see the document of Atom::Service in detail.

Example:

service = client.get_service(service_uri)
service.workspaces.each do |w|
  w.collections.each do |c|
    puts c.href
  end
end


1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
# File 'lib/atomutil.rb', line 1349

def get_service(service_uri)
  get_contents_except_resources(service_uri) do |res|
    #warn "Bad Content Type" unless Atom::MediaType::SERVICE.is_a?(@res['Content-Type'])
    @rc = Atom::Service.new :stream => @res.body
    @rc.workspaces.each do |workspace|
      workspace.collections.each do |collection|
        #@service_info.put(collection.href, collection, self) 
        @service_info.put(collection.href, collection) 
      end
    end
  end
  @rc
end

#update_entry(edit_uri, entry) ⇒ Object

Update entry

Example:

entry = client.get_entry(resource_uri)
entry.summary = "Changed Summary!"
client.update_entry(entry)


1467
1468
1469
1470
1471
1472
# File 'lib/atomutil.rb', line 1467

def update_entry(edit_uri, entry)
  unless entry.kind_of?(Atom::Entry)
    entry = Atom::Entry.new :stream => entry
  end
  update_resource(edit_uri, entry.to_s, Atom::MediaType::ENTRY.to_s)
end

#update_media(media_uri, file_path, content_type) ⇒ Object

Update media resource

Example:

entry = client.get_entry(media_link_uri)
client.update_media(entry.edit_media_link, 'newimage.jpg', 'image/jpeg')


1480
1481
1482
1483
1484
# File 'lib/atomutil.rb', line 1480

def update_media(media_uri, file_path, content_type)
  file_path = Pathname.new(file_path) unless file_path.is_a?(Pathname)
  stream = file_path.open { |f| f.binmode; f.read }
  update_resource(media_uri, stream, content_type)
end

#use_proxy(uri, port, user = nil, pass = nil) ⇒ Object

Set proxy if you need.

Example:

client.use_proxy('http://myproxy/', 8080)
client.use_proxy('http://myproxy/', 8080, 'myusername', 'mypassword')


1333
1334
1335
# File 'lib/atomutil.rb', line 1333

def use_proxy(uri, port, user=nil, pass=nil)
  @http_class = Net::HTTP::Proxy(uri, port, user, pass)
end