Class: WonderCroc::Client
- Inherits:
-
Object
- Object
- WonderCroc::Client
- Defined in:
- lib/wondercroc/client.rb,
lib/wondercroc/folder.rb,
lib/wondercroc/folder.rb,
lib/wondercroc/location.rb,
lib/wondercroc/subscription.rb
Constant Summary collapse
- SERVICE =
"http://services.newsgator.com/ngws/svc"
- BULLETIN_TOKEN =
{ "X-NGAPITOKEN" => "A8BBE03745F2439287D9425AB4BFFC30", "User-Agent" => "wondercroc/#{WonderCroc::VERSION}", "Accept-Encoding" => "compress,gzip" }
Instance Attribute Summary collapse
-
#folders ⇒ Object
Returns the value of attribute folders.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
-
#add_location(name, add_subscriptions = true) ⇒ Object
Adds a location to the online service.
-
#clear_locations ⇒ Object
Emptys the location cache.
-
#delete_location(id) ⇒ Object
Deletes a location from the online service.
- #find_folder_by_id(folder_id) ⇒ Object
- #get_feed(id, unread = true) ⇒ Object
-
#get_folders ⇒ Object
Return a list of all folders in the user’s subscription.
-
#get_locations ⇒ Object
Retrieves a list of all the locations that the logged in user has available.
- #get_subscriptions_from_location(id = ) ⇒ Object
- #get_unread_counts ⇒ Object
-
#initialize(config_options) ⇒ Client
constructor
Initializes an instance of a NewsGator Client.
- #location_info(id) ⇒ Object
- #parse_stories ⇒ Object
Constructor Details
#initialize(config_options) ⇒ Client
Initializes an instance of a NewsGator Client. config_options
is a hash consisting of either all the informatio required to start a NewsGator connection (:user, :password, and :location), or the name of a file that has all those values in a YAML file (:file).
We then set the location from the configuration hash passed through at the instantsiation of the Client object. If a :location
key was not passed with that has, the location name is generated like so: ‘newsgator_account_name-MACHINE_HOSTNAME’.
32 33 34 35 36 37 38 39 40 |
# File 'lib/wondercroc/client.rb', line 32 def initialize @config = WonderCroc::Configuration.new @folder_unread_counts = {} get_locations @location = @locations.find { |l| l[:title] == @config.location } raise "no location found for name #{@config.location}" unless @location end |
Instance Attribute Details
#folders ⇒ Object
Returns the value of attribute folders.
21 22 23 |
# File 'lib/wondercroc/client.rb', line 21 def folders @folders end |
#response ⇒ Object
Returns the value of attribute response.
20 21 22 |
# File 'lib/wondercroc/client.rb', line 20 def response @response end |
Instance Method Details
#add_location(name, add_subscriptions = true) ⇒ Object
Adds a location to the online service. name
is the name of the location, add_subscriptions
is a boolean option corresponding to whether or not subscriptions are automatically added to this location whenever the user subscribes to a new feed.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/wondercroc/location.rb', line 43 def add_location name, add_subscriptions = true new_connection_to "/Location.aspx" payload = <<-OPML <opml xmlns:ng="http://newsgator.com/schema/opml"> <head /> <body> <outline text="#{name}" ng:autoAddSubs="#{add_subscriptions.to_s.capitalize}" /> </body> </opml> OPML send_data :post, payload, :content_type => 'application/xml' puts @response end |
#clear_locations ⇒ Object
Emptys the location cache.
6 7 8 |
# File 'lib/wondercroc/location.rb', line 6 def clear_locations @locations = [] end |
#delete_location(id) ⇒ Object
Deletes a location from the online service. This does not require any kind of confirmation, and is permanent for all intents and purposes.
27 28 29 30 |
# File 'lib/wondercroc/location.rb', line 27 def delete_location id new_connection_to "/Location.aspx/#{id}" send_request :delete end |
#find_folder_by_id(folder_id) ⇒ Object
6 7 8 9 |
# File 'lib/wondercroc/folder.rb', line 6 def find_folder_by_id folder_id @subfolders.each { |subfolder| return subfolder if subfolder and subfolder.find_by_id folder_id} return nil end |
#get_feed(id, unread = true) ⇒ Object
42 43 44 45 |
# File 'lib/wondercroc/client.rb', line 42 def get_feed id, unread = true new_connection_to "/Feed.aspx/#{id}?unread=#{unread.to_s.capitalize}" send_request :get end |
#get_folders ⇒ Object
Return a list of all folders in the user’s subscription.
55 56 57 58 59 |
# File 'lib/wondercroc/folder.rb', line 55 def get_folders new_connection_to "/Folder.aspx" send_request :get @folders = folders_from_xml(@response).subfolders end |
#get_locations ⇒ Object
Retrieves a list of all the locations that the logged in user has available.
12 13 14 15 16 17 |
# File 'lib/wondercroc/location.rb', line 12 def get_locations clear_locations new_connection_to '/Location.aspx' send_request :get @locations = locations_from_xml(@response) end |
#get_subscriptions_from_location(id = ) ⇒ Object
32 33 34 35 36 |
# File 'lib/wondercroc/location.rb', line 32 def get_subscriptions_from_location id = @location[:id] new_connection_to "/Subscription.aspx/#{id}" send_request :get @subscriptions = subscriptions_from_xml(@response) end |
#get_unread_counts ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/wondercroc/client.rb', line 53 def get_unread_counts new_connection_to "/Subscription.aspx/#{@location[:id]}/subscriptionCounts" send_request :get doc = REXML::Document.new @response REXML::XPath.each(doc, "descendant-or-self::*") do |e| @folder_unread_counts[e.attributes['ng:folderId']] = @folder_unread_counts.fetch(e.attributes['ng:folderId'], 0) + e.attributes['ng:unreadCount'].to_i end @folders.flatten.each { |f| f.unread_count = @folder_unread_counts.fetch(f.id, 0) } end |
#location_info(id) ⇒ Object
19 20 21 22 |
# File 'lib/wondercroc/location.rb', line 19 def location_info id return @locations.find { |l| l[:id] == id } return nil end |
#parse_stories ⇒ Object
47 48 49 50 51 |
# File 'lib/wondercroc/client.rb', line 47 def parse_stories p = RSS::Parser.new @response p.do_validate = false p.parse end |