rawapi 0.1.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rawapi.rb DELETED
@@ -1,132 +0,0 @@
1
- Dir[File.join(File.dirname(__FILE__), 'rawapi/**/*.rb')].sort.each { |lib| require lib }
2
-
3
- require 'open-uri'
4
- require 'rexml/document'
5
- include REXML
6
-
7
- # Class to provide the interface to the Awareness API
8
- #
9
- # dates:
10
- # * Individual dates always use this format: YYYY-MM-DD
11
- # * Date ranges are expressed with a comma: YYYY-MM-D1, YYYY-MM-D7 means all dates between and including D1 and D7
12
- # * If a range is specified, the second date must always be later than the first date
13
- # * A single date will be interpreted as a range of one date: YYYY-MM-D1 = YYYY-MM-D1, YYYY-MM-D1
14
- # * Discrete ranges are separated by a slash: YYYY-MM-D1/YYYY-MM-D5/YYYY-MM-D7,YYYY-MM-D14 means D1 and D5 and all dates between and including D7 and D14. Multiple discrete ranges may also be provided by using multiple date parameters in a single GET request.
15
- # * If no date is specified, Yesterday's date is assumed. "Current" is always yesterday's data. "Live" daily data is not yet available.
16
- # * An individual date starts at 12am CDT (GMT -5) and ends 12am CDT the next day. Custom timezone support is not yet available.
17
- class AwarenessApi
18
- ROOT_API_URL = 'http://api.feedburner.com/awareness/1.0/'
19
-
20
- # Get Basic Feed Awareness Data
21
- #
22
- # options:
23
- # * uri: The URI of the feed (same as http://feeds.feedburner.com/<feeduri>) Must be used if id is not specified
24
- # * id: The FeedBurner id of the feed (visible when editing a feed in your account, e.g., http://www.feedburner.com/fb/a/optimize?id=<id>). May be used instead if uri is not specified.
25
- # * dates: Dates are used to specify the period for which data is need (see note on dates above)
26
- def get_feed_data(options)
27
- option_string = parse_options(options)
28
-
29
- response_xml = open("#{ROOT_API_URL}GetFeedData?#{option_string}").read
30
- return parse_xml(response_xml)
31
- end
32
-
33
- # Get Individual Item Awareness Data (TotalStats pro)
34
- #
35
- # options:
36
- # * uri: The URI of the feed (same as http://feeds.feedburner.com/<feeduri>) Must be used if id is not specified
37
- # * id: The FeedBurner id of the feed (visible when editing a feed in your account, e.g., http://www.feedburner.com/fb/a/optimize?id=<id>). May be used instead if uri is not specified.
38
- # * itemurl: The source URL of item (not the FeedBurner generated URL, but the original source URL). Multiple itemurl parameters may be provided in a single request in order to retrieve additional items.
39
- # * dates: Dates are used to specify the period for which data is need (see note on dates above)
40
- def get_item_data(options)
41
- option_string = parse_options(options)
42
-
43
- response_xml = open("#{ROOT_API_URL}GetItemData?#{option_string}").read
44
- return parse_xml(response_xml)
45
- end
46
-
47
- # Get Resyndication Feed Awareness Data (TotalStats pro)
48
- #
49
- # options:
50
- # * uri: The URI of the feed (same as http://feeds.feedburner.com/<feeduri>)
51
- # * id: The FeedBurner id of the feed (visible when editing a feed in your account, e.g., http://www.feedburner.com/fb/a/optimize?id=<id>). May be used instead if uri is not specified.
52
- # * itemurl: The source URL of item (not the FeedBurner generated URL, but the original source URL). Multiple itemurl parameters may be provided in a single request in order to retrieve additional items.
53
- # * dates: Dates are used to specify the period for which data is need (see note on dates above)
54
- def get_resyndication_data(options)
55
- option_string = parse_options(options)
56
-
57
- response_xml = open("#{ROOT_API_URL}GetResyndicationData?#{option_string}").read
58
- return parse_xml(response_xml)
59
- end
60
-
61
- private
62
- def parse_options(options)
63
- raise "options must include a feed id or uri" unless options[:id] or options[:uri]
64
-
65
- options[:uri] = nil if options[:id] and options[:uri]
66
-
67
- return options.collect {|key, value| "#{key}=#{value}" unless value.nil?}.compact.join('&')
68
- end
69
-
70
- def parse_xml(response_xml)
71
- response = Response.new
72
-
73
- xml = Document.new(response_xml)
74
- rsp_node = xml.root
75
-
76
- response.status = rsp_node.attributes['stat']
77
-
78
- if response.status == 'fail'
79
- response.code = rsp_node.elements[1].attributes['code']
80
- response.message = rsp_node.elements[1].attributes['msg']
81
- elsif response.status == 'ok'
82
- for feed_node in rsp_node.elements
83
- feed = Feed.new
84
-
85
- feed.id = feed_node.attributes['id']
86
- feed.uri = feed_node.attributes['uri']
87
-
88
- response.feeds << feed
89
-
90
- for entry_node in feed_node.elements
91
- entry = Entry.new
92
-
93
- entry.date = entry_node.attributes['date']
94
- entry.circulation = entry_node.attributes['circulation']
95
- entry.hits = entry_node.attributes['hits']
96
-
97
- feed.entries << entry
98
-
99
- for item_node in entry_node.elements
100
- item = Item.new
101
-
102
- item.title = item_node.attributes['title']
103
- item.url = item_node.attributes['url']
104
- item.itemviews = item_node.attributes['itemviews']
105
- item.clickthroughs = item_node.attributes['clickthroughs']
106
-
107
- entry.items << item
108
-
109
- for referer_node in item_node.elements
110
- referer = Referer.new
111
-
112
- referer.url = referer_node.attributes['url']
113
- referer.itemviews = referer_node.attributes['itemviews']
114
- referer.clickthroughs = referer_node.attributes['clickthroughs']
115
-
116
- item.referers << referer
117
- end
118
- end
119
- end
120
- end
121
- end
122
-
123
- return response
124
- end
125
- end
126
-
127
- # rawapi = AwarenessApi.new
128
- # opts = {:uri => 'CommonThread', :dates => '2007-03-20,2007-03-23'}
129
- #
130
- # puts rawapi.get_feed_data(opts)
131
- # puts rawapi.get_item_data(opts)
132
- # puts rawapi.get_resyndication_data(opts)