ardekantur-wondercroc 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +0 -0
- data/lib/client.rb +92 -0
- data/lib/folder.rb +57 -0
- data/lib/location.rb +72 -0
- data/lib/subscription.rb +36 -0
- data/spec/folder_spec.rb +7 -0
- data/wondercroc.gemspec +21 -0
- metadata +66 -0
data/README
ADDED
File without changes
|
data/lib/client.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rest_client'
|
3
|
+
require 'rexml/document'
|
4
|
+
require 'rss/2.0'
|
5
|
+
|
6
|
+
# NewsGator is a Ruby layer for interacting with the
|
7
|
+
# NewsGator API.
|
8
|
+
|
9
|
+
module WonderCroc
|
10
|
+
class NewsGatorClient
|
11
|
+
|
12
|
+
VERSION = [ 0, 0, 3 ].join '.'
|
13
|
+
SERVICE = "http://services.newsgator.com/ngws/svc"
|
14
|
+
|
15
|
+
BULLETIN_TOKEN = {
|
16
|
+
"X-NGAPITOKEN" => "A8BBE03745F2439287D9425AB4BFFC30",
|
17
|
+
"User-Agent" => "bulletin/#{VERSION}",
|
18
|
+
"Accept-Encoding" => "compress,gzip"
|
19
|
+
}
|
20
|
+
|
21
|
+
attr_accessor :response
|
22
|
+
attr_accessor :folders
|
23
|
+
|
24
|
+
# Initializes an instance of a NewsGator connection
|
25
|
+
# with the provided +Configuration+ object.
|
26
|
+
def initialize config_obj
|
27
|
+
@config = config_obj
|
28
|
+
@folder_unread_counts = {}
|
29
|
+
self.clear_locations
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_feed id, unread = true
|
33
|
+
new_connection_to "/Feed.aspx/#{id}?unread=#{unread.to_s.capitalize}"
|
34
|
+
send_request :get
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_random_feed
|
38
|
+
raise "no feeds" unless @subscriptions
|
39
|
+
get_feed @subscriptions[(rand * @subscriptions.length).floor].id, false
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_stories
|
43
|
+
p = RSS::Parser.new @response
|
44
|
+
p.do_validate = false
|
45
|
+
p.parse
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_unread_counts ui = nil
|
49
|
+
ui.update_status 'Getting subscription counts...' if ui
|
50
|
+
new_connection_to "/Subscription.aspx/#{@location[:id]}/subscriptionCounts"
|
51
|
+
send_request :get
|
52
|
+
doc = REXML::Document.new @response
|
53
|
+
REXML::XPath.each(doc, "descendant-or-self::*") do |e|
|
54
|
+
@folder_unread_counts[e.attributes['ng:folderId']] = @folder_unread_counts.fetch(e.attributes['ng:folderId'], 0) + e.attributes['ng:unreadCount'].to_i
|
55
|
+
end
|
56
|
+
@folders.flatten.each { |f| f.unread_count = @folder_unread_counts.fetch(f.id, 0) }
|
57
|
+
ui.update_status ''
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def new_connection_to url
|
63
|
+
@request = RestClient::Resource.new(SERVICE + url, @config.user, @config.password)
|
64
|
+
end
|
65
|
+
|
66
|
+
def send_request type, headers = {}
|
67
|
+
raise ArgumentError, "invalid send request type #{type}" unless [ :get, :delete ].include? type
|
68
|
+
@response = @request.send type, BULLETIN_TOKEN.merge(headers)
|
69
|
+
end
|
70
|
+
|
71
|
+
def send_data type, payload, headers = {}
|
72
|
+
raise ArgumentError, "invalid send data request type #{type}" unless [ :put, :post ].include? type
|
73
|
+
@response = @request.send type, payload, BULLETIN_TOKEN.merge(headers)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class RSS::Rss::Channel::Item
|
80
|
+
[
|
81
|
+
["read", :boolean ],
|
82
|
+
["id", :integer ],
|
83
|
+
["postId", :integer ],
|
84
|
+
["avgRating", :float ],
|
85
|
+
["clipped", :boolean ],
|
86
|
+
["token", :text ],
|
87
|
+
["folderId", :text ]
|
88
|
+
].each do |elem, type|
|
89
|
+
install_text_element "ng:#{elem}", "http://newsgator.com/schema/extensions", '?', "#{elem}", type, "ng:#{elem}"
|
90
|
+
RSS::BaseListener.install_get_text_element "http://newsgator.com/schema/extensions", "#{elem}", "#{elem}="
|
91
|
+
end
|
92
|
+
end
|
data/lib/folder.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# A folder is a folder that resides in a NewsGator location.
|
2
|
+
# It can contain subscriptions and other folders.
|
3
|
+
|
4
|
+
module WonderCroc
|
5
|
+
class Folder
|
6
|
+
|
7
|
+
attr_accessor :id, :name, :unread_count
|
8
|
+
|
9
|
+
def initialize name, id
|
10
|
+
@name = name
|
11
|
+
@id = id
|
12
|
+
@unread_count = 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_str
|
16
|
+
return @name if @unread_count == 0
|
17
|
+
return "#{@name} (#{@unread_count})"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
class NewsGatorClient
|
23
|
+
|
24
|
+
def folders_from_xml xml
|
25
|
+
folders = []
|
26
|
+
doc = REXML::Document.new xml
|
27
|
+
doc.elements.each('opml/body/outline') do |e|
|
28
|
+
dir = [ Folder.new(e.attributes['title'], e.attributes['ng:id']) ]
|
29
|
+
dir << subfolders(e)
|
30
|
+
folders << dir.compact
|
31
|
+
end
|
32
|
+
return folders.compact
|
33
|
+
end
|
34
|
+
|
35
|
+
def subfolders element
|
36
|
+
return nil if element.elements.size == 0
|
37
|
+
folders = []
|
38
|
+
element.each do |e|
|
39
|
+
folders << Folder.new(e.attributes['title'], e.attributes['ng:id'])
|
40
|
+
folders << subfolders(e)
|
41
|
+
end
|
42
|
+
|
43
|
+
return folders.compact
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# Return a list of all folders in the user's subscription.
|
49
|
+
def get_folders
|
50
|
+
new_connection_to "/Folder.aspx"
|
51
|
+
send_request :get
|
52
|
+
@folders = folders_from_xml(@response)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/lib/location.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module WonderCroc
|
2
|
+
class NewsGatorClient
|
3
|
+
|
4
|
+
# Emptys the location cache.
|
5
|
+
def clear_locations
|
6
|
+
@locations = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def set_location_from_config
|
10
|
+
get_locations
|
11
|
+
@location = @locations.find { |l| l[:title] == @config.location }
|
12
|
+
raise "no location found for name #{@config.location}" unless @location
|
13
|
+
end
|
14
|
+
|
15
|
+
# Retrieves a list of all of the user's locations.
|
16
|
+
def get_locations
|
17
|
+
clear_locations
|
18
|
+
new_connection_to '/Location.aspx'
|
19
|
+
send_request :get
|
20
|
+
@locations = locations_from_xml(@response)
|
21
|
+
end
|
22
|
+
|
23
|
+
def location_info id
|
24
|
+
return @locations.find { |l| l[:id] == id }
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete_location id
|
29
|
+
new_connection_to "/Location.aspx/#{id}"
|
30
|
+
send_request :delete
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_subscriptions_from_location id = @location[:id]
|
34
|
+
new_connection_to "/Subscription.aspx/#{id}"
|
35
|
+
send_request :get
|
36
|
+
@subscriptions = subscriptions_from_xml(@response)
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_location name, add_subscriptions = true
|
40
|
+
new_connection_to "/Location.aspx"
|
41
|
+
payload = <<-OPML
|
42
|
+
<opml xmlns:ng="http://newsgator.com/schema/opml">
|
43
|
+
<head />
|
44
|
+
<body>
|
45
|
+
<outline text="#{name}" ng:autoAddSubs="#{add_subscriptions.to_s.capitalize}" />
|
46
|
+
</body>
|
47
|
+
</opml>
|
48
|
+
OPML
|
49
|
+
send_data :post, payload, :content_type => 'application/xml'
|
50
|
+
puts @response
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def locations_from_xml xml
|
56
|
+
locations = []
|
57
|
+
doc = REXML::Document.new xml
|
58
|
+
doc.elements.each('opml/body/outline') do |e|
|
59
|
+
l = {
|
60
|
+
:id => e.attributes['ng:id'].to_i,
|
61
|
+
:title => e.attributes['text'],
|
62
|
+
:url => e.attributes['url'],
|
63
|
+
:auto_add_subscriptions => e.attributes['ng:autoAddSubs'] == "True" ? true : false,
|
64
|
+
:is_public => e.attributes['ng:isPublic'] == "True" ? true : false
|
65
|
+
}
|
66
|
+
locations << l
|
67
|
+
end
|
68
|
+
return locations
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
data/lib/subscription.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module WonderCroc
|
2
|
+
|
3
|
+
# A subscription is a single RSS feed that is subscribed to by
|
4
|
+
# NewsGator.
|
5
|
+
class Subscription
|
6
|
+
attr_accessor :title, :title, :id, :xml_url, :html_url, :sync_xml_url
|
7
|
+
attr_accessor :folder_id, :description, :subscriber_count, :unseen
|
8
|
+
attr_accessor :unread, :updated, :use_default_credentials
|
9
|
+
end
|
10
|
+
|
11
|
+
class NewsGatorClient
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def subscriptions_from_xml xml
|
16
|
+
subscriptions = []
|
17
|
+
doc = REXML::Document.new xml
|
18
|
+
doc.elements.each('opml/body/outline') do |e|
|
19
|
+
s = Subscription.new
|
20
|
+
s.title = e.attributes['title']
|
21
|
+
s.id = e.attributes['ng:id'].to_i
|
22
|
+
s.xml_url = e.attributes['xmlUrl']
|
23
|
+
s.html_url = e.attributes['htmlUrl']
|
24
|
+
s.sync_xml_url = e.attributes['ng:syncXmlUrl']
|
25
|
+
s.subscriber_count = e.attributes['ng:subscriberCount'].to_i
|
26
|
+
s.unseen = e.attributes['ng:unseen'] == "True" ? true : false
|
27
|
+
s.unread = e.attributes['ng:unread'] == "True" ? true : false
|
28
|
+
s.updated = e.attributes['ng:updated'] == "True" ? true : false
|
29
|
+
s.folder_id = e.attributes['ng:folderId']
|
30
|
+
subscriptions << s
|
31
|
+
end
|
32
|
+
return subscriptions
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/spec/folder_spec.rb
ADDED
data/wondercroc.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "wondercroc"
|
3
|
+
s.version = "0.0.3"
|
4
|
+
s.date = "2008-06-24"
|
5
|
+
s.summary = "a ruby api for interacting with the newsgator feed synchronization service."
|
6
|
+
s.email = "greystone@ardekantur.com"
|
7
|
+
s.homepage = "http://github.com/ardekantur/wondercroc"
|
8
|
+
s.description = ""
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ["Ardekantur"]
|
11
|
+
s.require_path = "lib"
|
12
|
+
s.files = [ "wondercroc.gemspec", "README", "lib/client.rb", "lib/folder.rb", "lib/location.rb", "lib/subscription.rb"]
|
13
|
+
s.test_files = ["spec/folder_spec.rb"]
|
14
|
+
s.add_dependency('rest_client')
|
15
|
+
=begin
|
16
|
+
s.test_files = ["test/test_actor.rb", "test/test_blob.rb", "test/test_commit.rb", "test/test_config.rb", "test/test_diff.rb", "test/test_git.rb", "test/test_head.rb", "test/test_real.rb", "test/test_reality.rb", "test/test_repo.rb", "test/test_tag.rb", "test/test_tree.rb"]
|
17
|
+
s.rdoc_options = ["--main", "README.txt"]
|
18
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
19
|
+
s.add_dependency("mime-types", ["> 0.0.0"])
|
20
|
+
=end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ardekantur-wondercroc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ardekantur
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-24 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rest_client
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: ""
|
25
|
+
email: greystone@ardekantur.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- wondercroc.gemspec
|
34
|
+
- README
|
35
|
+
- lib/client.rb
|
36
|
+
- lib/folder.rb
|
37
|
+
- lib/location.rb
|
38
|
+
- lib/subscription.rb
|
39
|
+
has_rdoc: false
|
40
|
+
homepage: http://github.com/ardekantur/wondercroc
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.0.1
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: a ruby api for interacting with the newsgator feed synchronization service.
|
65
|
+
test_files:
|
66
|
+
- spec/folder_spec.rb
|