rack-superfeedr 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -50,7 +50,7 @@ module Rack
50
50
  # Subscribe you to a url. id is optional but strongly recommanded has a unique identifier for this url. It will be used to help you identify which feed
51
51
  # is concerned by a notification.
52
52
  # A 3rd options argument can be supplied with
53
- # - retrive => true if you want to retrieve the previous items in the feed
53
+ # - retrieve => true if you want to retrieve the previous items in the feed
54
54
  # - format => 'json' or 'atom' to specify the format of the notifications, defaults to atom
55
55
  # - secret => a secret string used to compyte HMAC signatures so you can check that the data is coming from Superfeedr
56
56
  # - sync => true (defaults to false) if you want to perfrom a verification of intent syncrhonously
@@ -85,6 +85,42 @@ module Rack
85
85
  blk.call(response.body, opts[:async] && Integer(response.code) == 202 || Integer(response.code) == 204 || opts[:retrieve] && Integer(response.code) == 200, response) if blk
86
86
  end
87
87
 
88
+ ##
89
+ # Retrieve the content of a resource at Superfeedr
90
+ # A 2nd options argument can be supplied with
91
+ # - format => 'json' or 'atom' to specify the format of the notifications, defaults to atom
92
+ # - count => Integer (number of items to retrieve)
93
+ # - before => The id of an entry in the feed. The response will only include entries published before this one.
94
+ # - after => The id of an entry in the feed. The response will only include entries published after this one.
95
+ # It yields 3 arguments to a block (if block is supplied. If not, just returns the triplet)
96
+ # - body of the response (useful if you used the retrieve option)
97
+ # - success flag
98
+ # - response (useful to debug failed requests mostly)
99
+ def self.retrieve_by_topic_url(url, opts = {}, &blk)
100
+ endpoint = opts[:hub] || @@superfeedr_endpoint
101
+ request = prep_request(url, '', endpoint, opts)
102
+
103
+ if opts[:format] == "json"
104
+ request['format'] = "json"
105
+ end
106
+
107
+ if opts[:count]
108
+ request['count'] = opts[:count]
109
+ else
110
+ request['count'] = 10
111
+ end
112
+
113
+ request['hub.mode'] = 'retrieve'
114
+
115
+ response = http_get(endpoint, request)
116
+
117
+ if blk
118
+ blk.call(response.body, Integer(response.code) == 200, response) if blk
119
+ else
120
+ [response.body, Integer(response.code) == 200, response]
121
+ end
122
+ end
123
+
88
124
  ##
89
125
  # Unsubscribes a url. If you used an id for the susbcription, you need to use _the same_.
90
126
  # The optional block will be called to let you confirm the subscription (or not). This is not applicable for if you use params[:async] => true
@@ -168,9 +204,6 @@ module Rack
168
204
  def call(env)
169
205
  req = Rack::Request.new(env)
170
206
  if env['REQUEST_METHOD'] == 'GET' && feed_id = env['PATH_INFO'].match(/#{@@base_path}(.*)/)
171
- puts "----"
172
- puts req.params['hub.mode'], feed_id[1], req.params['hub.topic']
173
- puts "----"
174
207
  if @verification.call(req.params['hub.mode'], feed_id[1], req.params['hub.topic'], req)
175
208
  Rack::Response.new(req.params['hub.challenge'], 200).finish
176
209
  else
@@ -209,6 +242,16 @@ module Rack
209
242
  request
210
243
  end
211
244
 
245
+ def self.http_get(url, opts)
246
+ uri = URI.parse URI.encode(url)
247
+ uri.query = URI.encode_www_form opts || {}
248
+ uri.path=='/' if uri.path.empty?
249
+ http = Net::HTTP.new(uri.host, uri.port)
250
+ http.use_ssl = true
251
+ request = Net::HTTP::Get.new uri.request_uri
252
+ http.request(request)
253
+ end
254
+
212
255
  def self.http_post(url, opts)
213
256
  uri = URI.parse URI.encode(url)
214
257
  uri.path=='/' if uri.path.empty?
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rack-superfeedr"
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["julien51"]
12
- s.date = "2014-07-29"
12
+ s.date = "2015-04-15"
13
13
  s.description = "A gem that provides a rack middleware to interract with Superfeedr's API. "
14
14
  s.email = "julien@superfeedr.com"
15
15
  s.extra_rdoc_files = [
@@ -1,8 +1,9 @@
1
+ require 'json'
1
2
  require 'rack'
2
3
  require_relative 'helper.rb'
3
4
 
4
5
  # To run tests locally, we're using runscope's passageway which proxies requests inside the firewall. (make sure you bind to port 4567)
5
- HOST = '37cb3f2fe113.a.passageway.io'
6
+ HOST = '5f83728c358.b.passageway.io'
6
7
  PORT = 80
7
8
  # Also, we need superfeedr credentials.
8
9
  LOGIN = 'demo'
@@ -11,13 +12,12 @@ PASSWORD = '8ac38a53cc32f71a6445e880f76fc865'
11
12
 
12
13
  class MyRackApp
13
14
  def call(env)
14
- puts env.inspect
15
15
  [ 200, {'Content-Type' => 'text/plain'}, ['hello world'] ]
16
16
  end
17
17
  end
18
18
 
19
19
  def notified(url, feed_id, details)
20
- puts url, feed_id, details
20
+ # puts url, feed_id, details
21
21
  end
22
22
 
23
23
  # Run an app in a thread
@@ -39,7 +39,7 @@ Thread.new do
39
39
  end
40
40
 
41
41
 
42
- end, :Port => 4567)
42
+ end, :Port => 4567, Logger: WEBrick::Log.new("/dev/null"), AccessLog: [],)
43
43
  end
44
44
  sleep 3
45
45
 
@@ -61,7 +61,6 @@ class TestRackSuperfeedr < Test::Unit::TestCase
61
61
 
62
62
  should "support sync mode and call the verification callback before yielding true" do
63
63
  Rack::Superfeedr.subscribe('http://push-pub.appspot.com/feed', 'accept-subscribe', {:sync => true}) do |body, success, response|
64
- puts body
65
64
  success || flunk("Fail")
66
65
  end
67
66
  end
@@ -122,6 +121,30 @@ class TestRackSuperfeedr < Test::Unit::TestCase
122
121
  end
123
122
  end
124
123
 
124
+ context "Retrieving" do
125
+ should 'yield content from Superfeedr in Atom when asking for no specific format' do
126
+ Rack::Superfeedr.retrieve_by_topic_url('http://push-pub.appspot.com/feed') do |body, success, response|
127
+ success || flunk("Fail")
128
+ end
129
+ end
130
+
131
+ should 'yield content from Superfeedr in JSON when asking for JSON' do
132
+ Rack::Superfeedr.retrieve_by_topic_url('http://push-pub.appspot.com/feed', {:format => 'json'}) do |body, success, response|
133
+ success || flunk("Fail")
134
+ hash = JSON.parse body
135
+ hash['status'] || flunk("Not JSON")
136
+ end
137
+ end
138
+
139
+ should 'yield content from Superfeedr in JSON when asking for JSON and only yield the right number of items' do
140
+ Rack::Superfeedr.retrieve_by_topic_url('http://push-pub.appspot.com/feed', {:format => 'json', :count => 3}) do |body, success, response|
141
+ success || flunk("Fail")
142
+ hash = JSON.parse body
143
+ hash['items'].length == 3 || flunk("Not the right number of items")
144
+ end
145
+ end
146
+ end
147
+
125
148
  context "Notifications" do
126
149
  should 'handle json notifications'
127
150
  should 'handle atom notifications'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-superfeedr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-29 00:00:00.000000000 Z
12
+ date: 2015-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -159,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  segments:
161
161
  - 0
162
- hash: 1778305000862886852
162
+ hash: 3582772756696706427
163
163
  required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  none: false
165
165
  requirements: