Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Parsing of Randypot.member call

  • Loading branch information...
commit 1daac6257d8d5e93c3b2c9edcf158893baee204a 1 parent b3ccc71
Fernando García Samblas authored
24 README
View
@@ -91,11 +91,13 @@
To get a CSV list with the current Randypot members and their kandypot sizes you can call:
- Randypot.members
+ ranking = Randypot.members
- Randypot will store the results in local cache files to optimize its bandwith needs, avoiding data transfers if Kandypot confirms that no modification has been made since the last request.
+ You'll obtain an raw Randypot::Response instance and the CVS list will be accesible through its "body" method (ranking.body).
- Randypot will also parse the CVS response storing the result within the #parsed accessor of the response (Randypot.members.parsed).
+ Randypot'll store the results in local cache files to optimize its bandwith needs, avoiding data transfers if Kandypot confirms that no modification has been made since the last request.
+
+ Randypot'll also parse the CVS response storing the result within the #parsed accessor of the response (Randypot.members.parsed).
The parsed members is an array of Struct objects that responds to #hash, #candies, #updated_at and so on. To list each member with its candies we can do something like this:
@@ -103,3 +105,19 @@
puts User.find_by_hash(member.hash).name + ': ' + member.candies
end
+== Randypot member ==
+
+ To get the current data of a member in JSON format you can call the class method "member" with her or his email as argument. As with Randypot.members, you'll get an Randypot::Responce instance:
+
+ randys_json = Randypot.member("randy@example.com").body
+
+ Randypot'll store the user's data in a local cache file to use it if Kandypot comfirms no modifications on it.
+
+ Randypot'll parse the JSON data for us and it'll be accesible through the #hash, #candies, #updated_at:
+
+ randy = Randypot.member("email@example.com").parsed
+ puts "Randy's hash: " + randy.hash
+ puts "Randy's candies: " + randy.candies.to_s
+ puts "Randy's last update: " + randy.updated_at.format('long')
+
+
29 lib/randypot.rb
View
@@ -11,6 +11,8 @@
require 'randypot/parsed_member'
require 'randypot/cache'
+require 'json'
+
class Randypot
attr_accessor :member
@@ -77,19 +79,22 @@ def relationship(base_params = nil)
def members
cached_request(members_url) do |response|
- if response.success?
- response.parse do |body|
- body.split("\n").map do |line|
- hash, candies, updated_at = line.split(',')
- ParsedMember.new(hash, candies.to_i, Time.parse(updated_at))
- end
+ response.parse do |body|
+ body.split("\n").map do |line|
+ hash, candies, updated_at = line.split(',')
+ ParsedMember.new(hash, candies.to_i, Time.parse(updated_at))
end
end
end
end
def member(id)
- cached_request member_url(id)
+ cached_request(member_url(id)) do |response|
+ response.parse do |body|
+ h = JSON.parse(body)
+ ParsedMember.new(h['member_token'], h['kandies_count'], Time.parse(h['updated_at']))
+ end
+ end
end
def hash_for(str)
@@ -116,13 +121,11 @@ def cached_request(url, &block)
response = connection.get(url, cache)
if response.not_modified?
cache
- else
+ elsif response.success?
yield response if block_given?
- if response.success?
- Randypot::Cache.put(url, response)
- else
- response
- end
+ Randypot::Cache.put(url, response)
+ else
+ response
end
end
end
6 lib/randypot/connection.rb
View
@@ -27,7 +27,11 @@ def response(r)
end
def etag(headers)
- headers['etag'] && headers['etag'].first
+ if headers['etag'] && raw = headers['etag'].first
+ # ETag quotes cleaning:
+ # "\"5d42b5fbf5aa3064d\"" => "5d42b5fbf5aa3064d"
+ raw[1..-2]
+ end
end
end
end
8 spec/lib/randypot_connection_spec.rb
View
@@ -20,7 +20,7 @@
@client_response = mock('HTTPClient', :status => 201,
:contenttype => "application/json; charset=utf-8",
:content => "{\"activity\":{\"content_type\":\"item\"}}",
- :header => {'etag' => ['254ce7c5']})
+ :header => {'etag' => ['"254ce7c5"']})
end
it '#post should do the post and return a Randypot Response' do
@@ -39,11 +39,11 @@
res.status.should == @client_response.status
res.body.should == @client_response.content
res.content_type.should == @client_response.contenttype
- res.etag.should == @client_response.header['etag'][0]
+ res.etag.should == @client_response.header['etag'][0][1..-2]
end
it '#get should do the get with the If-None-Match header if cache is not nil' do
- cache = mock('cache', :etag => 'd543fa38f')
+ cache = mock('cache', :etag => '"d543fa38f"') #HTTPCLIENT give us etag between quotes
@client.should_receive(:get).with(
@url, nil, {'If-None-Match' => cache.etag}).and_return(@client_response)
res = @connection.get(@url, cache)
@@ -51,7 +51,7 @@
res.status.should == @client_response.status
res.body.should == @client_response.content
res.content_type.should == @client_response.contenttype
- res.etag.should == @client_response.header['etag'][0]
+ res.etag.should == @client_response.header['etag'][0][1..-2]
end
end
end
15 spec/lib/randypot_spec.rb
View
@@ -322,7 +322,7 @@ def set_expectations_for(activity_type, base_params)
it 'should not cache the response if is not valid' do
@response.should_receive(:not_modified?).and_return(false)
- @response.should_receive(:success?).twice.and_return(false)
+ @response.should_receive(:success?).and_return(false)
Randypot::Cache.should_not_receive(:put)
Randypot.members
end
@@ -330,7 +330,9 @@ def set_expectations_for(activity_type, base_params)
describe '.member' do
before do
- @response = mock('response')
+ @response = Randypot::Response.new(:status => 200, :body => <<MEMBER_BODY)
+{"member_token":"422ef9125be6bf8a733fba46e0d02892","updated_at":"2010-02-01T19:52:56Z","kandies_count":31}
+MEMBER_BODY
cache, conn = mock('cache'), mock('connection')
@email = 'member@example.com'
@url = Randypot.member_url(@email)
@@ -339,19 +341,18 @@ def set_expectations_for(activity_type, base_params)
conn.should_receive(:get).with(@url, cache).and_return(@response)
end
- after do
- Randypot.member @email
- end
-
it 'should request members_url and cache response if response is good and no cache is present' do
@response.should_receive(:not_modified?).and_return(false)
@response.should_receive(:success?).and_return(true)
- Randypot::Cache.should_receive(:put).with(@url, @response)
+ Randypot::Cache.should_receive(:put).with(@url, @response).and_return(@response)
+ member = Randypot.member(@email)
+ member.parsed.candies.should == 31
end
it 'should request members_url and do not cache response if cache is valid' do
@response.should_receive(:not_modified?).and_return(true)
Randypot::Cache.should_not_receive(:put)
+ Randypot.member(@email)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.