lib/wondercroc/client.rb in ardekantur-wondercroc-0.0.4 vs lib/wondercroc/client.rb in ardekantur-wondercroc-0.0.5

- old
+ new

@@ -1,94 +1,114 @@ require 'rubygems' require 'rest_client' require 'rexml/document' require 'rss/2.0' - -# NewsGator is a Ruby layer for interacting with the -# NewsGator API. module WonderCroc + VERSION = [ 0, 0, 5 ].join '.' + class Client - VERSION = [ 0, 0, 4 ].join '.' SERVICE = "http://services.newsgator.com/ngws/svc" BULLETIN_TOKEN = { "X-NGAPITOKEN" => "A8BBE03745F2439287D9425AB4BFFC30", - "User-Agent" => "wondercroc/#{VERSION}", + "User-Agent" => "wondercroc/#{WonderCroc::VERSION}", "Accept-Encoding" => "compress,gzip" } attr_accessor :response attr_accessor :folders # Initializes an instance of a NewsGator Client. - # +config_obj+ is a hash consisting of the keys - # +:name+, +:password+, and +:location+, if you do - # not want WonderCroc auto-generating a location name. - def initialize config_obj - @config = config_obj + # +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'. + def initialize config_options + @config = WonderCroc::Configuration.new config_options @folder_unread_counts = {} - self.clear_locations + + get_locations + @location = @locations.find { |l| l[:title] == @config.location } + raise "no location found for name #{@config.location}" unless @location + end def get_feed id, unread = true new_connection_to "/Feed.aspx/#{id}?unread=#{unread.to_s.capitalize}" send_request :get end - def get_random_feed - raise "no feeds" unless @subscriptions - get_feed @subscriptions[(rand * @subscriptions.length).floor].id, false - end - def parse_stories p = RSS::Parser.new @response p.do_validate = false p.parse end - def get_unread_counts ui = nil - ui.update_status 'Getting subscription counts...' if ui + 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) } - ui.update_status '' end private def new_connection_to url - @request = RestClient::Resource.new(SERVICE + url, @config[:user], @config[:password]) + @request = RestClient::Resource.new(SERVICE + url, @config.user, @config.password) end def send_request type, headers = {} raise ArgumentError, "invalid send request type #{type}" unless [ :get, :delete ].include? type - @response = @request.send type, BULLETIN_TOKEN.merge(headers) + + begin + @response = @request.send type, BULLETIN_TOKEN.merge(headers) + rescue RestClient::Unauthorized + raise IncorrectLoginError, "#{@config.user} was not able to login" + end + + log_request_and_response end def send_data type, payload, headers = {} raise ArgumentError, "invalid send data request type #{type}" unless [ :put, :post ].include? type @response = @request.send type, payload, BULLETIN_TOKEN.merge(headers) + + log_request_and_response end + + def log_request_and_response + File.open(File.join(ENV['HOME'], 'wondercroc_logs'), 'a') do |f| + f.write "REQUEST TO: #{@request.inspect}\n" + f.write "RESPONSE: #{@response}" + f.write "\n\n\n" + end + end end + + class IncorrectLoginError < RuntimeError + end + end class RSS::Rss::Channel::Item [ ["read", :boolean ], ["id", :integer ], ["postId", :integer ], ["avgRating", :float ], ["clipped", :boolean ], - ["token", :text ], - ["folderId", :text ] + ["token", :text ] ].each do |elem, type| install_text_element "ng:#{elem}", "http://newsgator.com/schema/extensions", '?', "#{elem}", type, "ng:#{elem}" RSS::BaseListener.install_get_text_element "http://newsgator.com/schema/extensions", "#{elem}", "#{elem}=" end end