Class: Bloglines::WebServices

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ WebServices

Initialize Bloglines web services. This method takes as parameters:

:user is the email address used for a Bloglines account.
:password is the Bloglines account password. It is an optional argument,
          because not every method in the Bloglines API request it.
:proxy_host is the host for a proxy to access the internet. It is an
            optional parameter.
:proxy_port is the port where the proxy host is listening for incoming
            connections. It is an optional parameter.
:proxy_user specify the proxy username to work with authorization
            enabled proxies.
:proxy_pass specify the proxy password to work with authorization
            enabled proxies.


31
32
33
34
35
36
37
38
39
# File 'lib/bloglines.rb', line 31

def initialize params
  @user = params.delete :user
  @password = params.delete :password
  if ENV['http_proxy'] =~ /http:\/\/(.*):(\d*)/
    @proxy = {:proxy_host => $1, :proxy_port => $2}.merge params
  else
    @proxy = params
  end
end

Instance Method Details

#export(user_id) ⇒ Object

Retrieve public subscription information in OPML format for a given Bloglines account. See www.bloglines.com/public/user_id, and click on Export Subscription at the bottom of the left frame for details.



95
96
97
98
99
100
101
# File 'lib/bloglines.rb', line 95

def export user_id
  response = Net::HTTP::Proxy(@proxy[:proxy_addr], @proxy[:proxy_port],
                              @proxy[:proxy_user], @proxy[:proxy_pass]).start(HOSTNAME) { |http|
    http.get "/export?id=#{user_id}"
  }
  REXML::Document.new response.body
end

#getitems(subscription_id, mark_as_read = 0, date = nil) ⇒ Object

Retrieve unread blog entries for a given subscription, optionally marking them as read. If a date in seconds since Epoch is specified, all items since the passed UTC time are downloaded. See: www.bloglines.com/services/api/getitems for details.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bloglines.rb', line 74

def getitems subscription_id, mark_as_read=0, date=nil
  query = "s=#{subscription_id}&n=#{mark_as_read}"
  query += "&d=#{date}" unless date.nil?
  response = Net::HTTP::Proxy(@proxy[:proxy_addr], @proxy[:proxy_port],
                              @proxy[:proxy_user], @proxy[:proxy_pass]).start(HOSTNAME) { |http|
    headers = {'Authorization' => "Basic #{encode64(@user + ':' + @password)}"}
    http.get "/getitems?#{query}", headers
  }

  case response.code
    when '401' then raise BloglinesException.new("Incorrect email address or password")
    when '403' then raise BloglinesException.new("Invalid or missing subscription ID")
    when '410' then raise BloglinesException.new("Subscription has been deleted")
  end

  REXML::Document.new response.body
end

#listsubsObject

Retrieve subscription information in OPML format for a given Bloglines account. See www.bloglines.com/services/api/listsubs for details.

Raises:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bloglines.rb', line 59

def listsubs
  response = Net::HTTP::Proxy(@proxy[:proxy_addr], @proxy[:proxy_port],
                              @proxy[:proxy_user], @proxy[:proxy_pass]).start(HOSTNAME) { |http|
    headers = {'Authorization' => "Basic #{encode64(@user + ':' + @password)}"}
    http.get "/listsubs", headers    
  }

  raise BloglinesException.new("Incorrect email address or password") if response.code == '401'

  REXML::Document.new response.body
end

#update(version = 1) ⇒ Object

Retrieve an unread count for a given Bloglines account. See www.bloglines.com/services/api/notifier for details.

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bloglines.rb', line 43

def update version=1
  response = Net::HTTP::Proxy(@proxy[:proxy_addr], @proxy[:proxy_port],
                              @proxy[:proxy_user], @proxy[:proxy_pass]).start(HOSTNAME) { |http|
    http.get "/update?user=#{@user}&ver=#{version}"
  }
  raise BloglinesException.new unless response.code == '200'
  raise BloglinesException.new('Unexpected response pattern') unless response.body =~ /\|(\-{0,1}[0-9]+)\|([^|]*)\|/
  raise BloglinesException.new('User does not exist') if $1 == '-1'
  
  $stderr.puts "Please upgrade to a new notifier at #{$2}" unless $2.empty?
  
  $1.to_i
end