reagent-fleakr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,38 @@
1
+ # Fleakr
2
+
3
+ ## Description
4
+
5
+ A teeny tiny gem to interface with Flickr photostreams
6
+
7
+ ## Installation
8
+
9
+ sudo gem install fleakr
10
+
11
+ ## Usage
12
+
13
+ require 'fleakr'
14
+
15
+ ## License
16
+
17
+ Copyright (c) 2008 Patrick Reagan (reaganpr@gmail.com)
18
+
19
+ Permission is hereby granted, free of charge, to any person
20
+ obtaining a copy of this software and associated documentation
21
+ files (the "Software"), to deal in the Software without
22
+ restriction, including without limitation the rights to use,
23
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
24
+ copies of the Software, and to permit persons to whom the
25
+ Software is furnished to do so, subject to the following
26
+ conditions:
27
+
28
+ The above copyright notice and this permission notice shall be
29
+ included in all copies or substantial portions of the Software.
30
+
31
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
33
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
35
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
36
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
37
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
38
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/fleakr/version'
6
+
7
+ task :default => :test
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'fleakr'
11
+ s.version = Fleakr::Version.to_s
12
+ s.has_rdoc = true
13
+ s.extra_rdoc_files = %w(README.markdown)
14
+ s.summary = "A teeny tiny gem to interface with Flickr photostreams"
15
+ s.author = 'Patrick Reagan'
16
+ s.email = 'reaganpr@gmail.com'
17
+ s.homepage = 'http://sneaq.net'
18
+ s.files = %w(README.markdown Rakefile) + Dir.glob("{lib,test}/**/*")
19
+ # s.executables = ['fleakr']
20
+
21
+ s.add_dependency('hpricot', '~> 0.6.0')
22
+ end
23
+
24
+ Rake::GemPackageTask.new(spec) do |pkg|
25
+ pkg.gem_spec = spec
26
+ end
27
+
28
+ Rake::TestTask.new do |t|
29
+ t.libs << 'test'
30
+ t.test_files = FileList["test/**/*_test.rb"]
31
+ t.verbose = true
32
+ end
33
+
34
+ desc 'Generate the gemspec to serve this Gem from Github'
35
+ task :github do
36
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
37
+ File.open(file, 'w') {|f| f << spec.to_ruby }
38
+ puts "Created gemspec: #{file}"
39
+ end
data/lib/fleakr.rb ADDED
@@ -0,0 +1,7 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'hpricot'
6
+
7
+ Dir.glob(File.dirname(__FILE__) + '/**/*').each {|f| require f }
@@ -0,0 +1,34 @@
1
+ module Fleakr
2
+ class Request
3
+
4
+ def self.api_key=(key)
5
+ @api_key = key
6
+ end
7
+
8
+ def self.api_key
9
+ @api_key
10
+ end
11
+
12
+ def endpoint_uri
13
+ uri = URI.parse('http://api.flickr.com/services/rest/')
14
+ uri.query = self.query_parameters
15
+ uri
16
+ end
17
+
18
+ def query_parameters
19
+ @parameters.map {|key,value| "#{key}=#{value}" }.join('&')
20
+ end
21
+
22
+ def initialize(method, additional_parameters = {})
23
+ method = method.sub(/^(flickr\.)?/, 'flickr.')
24
+
25
+ default_parameters = {:api_key => self.class.api_key, :method => method}
26
+ @parameters = default_parameters.merge(additional_parameters)
27
+ end
28
+
29
+ def send
30
+ Response.new(Net::HTTP.get(self.endpoint_uri))
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,17 @@
1
+ module Fleakr
2
+ class Response
3
+
4
+ def initialize(response_xml)
5
+ @response_xml = response_xml
6
+ end
7
+
8
+ def body
9
+ @body ||= Hpricot.XML(@response_xml)
10
+ end
11
+
12
+ def error?
13
+ (self.body/'rsp').attr('stat') != 'ok'
14
+ end
15
+
16
+ end
17
+ end
data/lib/fleakr/set.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Fleakr
2
+ class Set
3
+
4
+ attr_accessor :title, :description
5
+
6
+ def self.find_all_by_user_id(user_id)
7
+ response = Request.new('photosets.getList', :user_id => user_id).send
8
+
9
+ (response.body/'photosets/photoset').map do |flickr_set|
10
+ set = Set.new
11
+ set.title = (flickr_set/'title').inner_text
12
+ set.description = (flickr_set/'description').inner_text
13
+ set
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module Fleakr
2
+ class User
3
+
4
+ attr_accessor :id, :username
5
+
6
+ def self.find_by_username(username)
7
+ response = Fleakr::Request.new('people.findByUsername', :username => username).send
8
+
9
+ user = User.new
10
+ user.id = (response.body/'rsp/user').attr('id')
11
+ user.username = (response.body/'rsp/user/username').inner_text
12
+
13
+ user
14
+ end
15
+
16
+ def sets
17
+ @set ||= Set.find_all_by_user_id(self.id)
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Fleakr
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ def self.to_s
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <rsp stat="ok">
3
+ <user id="31066442@N69" nsid="31066442@N69">
4
+ <username>frootpantz</username>
5
+ </user>
6
+ </rsp>
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <rsp stat="ok">
3
+ <photosets>
4
+ <photoset videos="0" primary="3044180117" farm="4" photos="138" id="72157609490909659" server="3012" secret="01cd1a741d">
5
+ <title>Second Set</title>
6
+ <description>This is the second set.</description>
7
+ </photoset>
8
+ <photoset videos="0" primary="2988511085" farm="4" photos="139" id="72157608538140671" server="3241" secret="a7b90926ba">
9
+ <title>First Set</title>
10
+ <description>This is the first set.</description>
11
+ </photoset>
12
+ </photosets>
13
+ </rsp>
@@ -0,0 +1,78 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ module Fleakr
4
+ class RequestTest < Test::Unit::TestCase
5
+
6
+ describe "A Request instance" do
7
+
8
+ it "should be able to set an API key" do
9
+ key = 'f00b4r'
10
+ Request.api_key = key
11
+ key.should == Request.api_key
12
+ end
13
+
14
+ context "with an API key" do
15
+
16
+ before do
17
+ @api_key = 'f00b4r'
18
+ Request.stubs(:api_key).with().returns(@api_key)
19
+ end
20
+
21
+ it "should know the full query parameters" do
22
+ request = Request.new('flickr.people.findByUsername', :username => 'foobar')
23
+
24
+ expected = [
25
+ "api_key=#{@api_key}",
26
+ "method=flickr.people.findByUsername",
27
+ "username=foobar"
28
+ ]
29
+
30
+ request.query_parameters.split('&').sort.should == expected
31
+ end
32
+
33
+ it "should translate a shorthand API call" do
34
+ request = Request.new('people.findByUsername')
35
+ request.query_parameters.split('&').include?('method=flickr.people.findByUsername').should be(true)
36
+ end
37
+
38
+ it "should know the endpoint with full parameters" do
39
+ query_parameters = 'foo=bar'
40
+
41
+ request = Request.new('people.getInfo')
42
+ request.stubs(:query_parameters).with().returns(query_parameters)
43
+
44
+ uri_mock = mock() {|m| m.expects(:query=).with(query_parameters)}
45
+
46
+ URI.expects(:parse).with("http://api.flickr.com/services/rest/").returns(uri_mock)
47
+
48
+ request.endpoint_uri.should == uri_mock
49
+ end
50
+
51
+ it "should be able to make a request" do
52
+ endpoint_uri = stub()
53
+
54
+ request = Request.new('flickr.people.findByUsername')
55
+
56
+ request.stubs(:endpoint_uri).with().returns(endpoint_uri)
57
+ Net::HTTP.expects(:get).with(endpoint_uri).returns('<xml>')
58
+
59
+ request.send
60
+ end
61
+
62
+ it "should create a response from the request" do
63
+ response_xml = '<xml>'
64
+ response_stub = stub()
65
+
66
+ request = Request.new('flickr.people.findByUsername')
67
+
68
+ Net::HTTP.stubs(:get).returns(response_xml)
69
+ Response.expects(:new).with(response_xml).returns(response_stub)
70
+
71
+ request.send.should == response_stub
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ module Fleakr
4
+ class ResponseTest < Test::Unit::TestCase
5
+
6
+ describe "An instance of Response" do
7
+
8
+ it "should provide the response body as an Hpricot element" do
9
+ response_xml = '<xml>'
10
+ hpricot_stub = stub()
11
+
12
+ Hpricot.expects(:XML).with(response_xml).returns(hpricot_stub)
13
+
14
+ response = Response.new(response_xml)
15
+ response.body.should == hpricot_stub
16
+ end
17
+
18
+ it "should memoize the Hpricot document" do
19
+ response = Response.new('<xml>')
20
+
21
+ Hpricot.expects(:XML).with(kind_of(String)).once.returns(stub())
22
+
23
+ 2.times { response.body }
24
+ end
25
+
26
+ it "should know if there are errors in the response" do
27
+ response_xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<rsp stat=\"fail\">\n\t<err code=\"1\" msg=\"User not found\" />\n</rsp>\n"
28
+ response = Response.new(response_xml)
29
+
30
+ response.error?.should be(true)
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ module Fleakr
4
+ class SetTest < Test::Unit::TestCase
5
+
6
+ def mock_request_cycle(options)
7
+ response = stub(:body => read_fixture(options[:for]))
8
+ Request.expects(:new).with(options[:for], options[:with]).returns(stub(:send => response))
9
+ end
10
+
11
+ describe "The Set class" do
12
+
13
+ context "When finding all sets for a user_id" do
14
+ before do
15
+ user_id = '31066442@N69'
16
+ mock_request_cycle :for => 'photosets.getList', :with => {:user_id => user_id}
17
+
18
+ @sets = Set.find_all_by_user_id(user_id)
19
+ end
20
+
21
+ it "should return an array with the expected number of elements" do
22
+ @sets.length.should == 2
23
+ end
24
+
25
+ it "should have the proper titles for each set in the collection" do
26
+ @sets.map {|s| s.title }.should == ["Second Set", "First Set"]
27
+ end
28
+
29
+ it "should have the proper descriptions for each set in the collection" do
30
+ @sets.map {|s| s.description }.should == ['This is the second set.', 'This is the first set.']
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ module Fleakr
4
+ class UserTest < Test::Unit::TestCase
5
+
6
+ describe "The User class" do
7
+
8
+ it "should be able to find a user by his username" do
9
+ response = stub(:body => read_fixture('people.findByUsername'))
10
+ Request.expects(:new).with('people.findByUsername', :username => 'frootpantz').returns(stub(:send => response))
11
+
12
+ user = User.find_by_username('frootpantz')
13
+
14
+ user.id.should == '31066442@N69'
15
+ user.username.should == 'frootpantz'
16
+ end
17
+
18
+ end
19
+
20
+ describe "An instance of User" do
21
+
22
+ before do
23
+ @user_id = '1'
24
+
25
+ @user = User.new
26
+ @user.stubs(:id).with().returns(@user_id)
27
+ end
28
+
29
+ it "should retrieve the sets for this user" do
30
+ sets = [stub()]
31
+ Set.expects(:find_all_by_user_id).with(@user_id).returns(sets)
32
+
33
+ @user.sets.should == sets
34
+ end
35
+
36
+ it "should memoize the results returned for this user's sets" do
37
+ Set.expects(:find_all_by_user_id).once.returns([])
38
+
39
+ 2.times { @user.sets }
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,17 @@
1
+ $:.reject! { |e| e.include? 'TextMate' }
2
+
3
+ require 'rubygems'
4
+ require 'matchy'
5
+ require 'context'
6
+ require 'mocha'
7
+
8
+ require File.dirname(__FILE__) + '/../lib/fleakr'
9
+
10
+ class Test::Unit::TestCase
11
+
12
+ def read_fixture(method_call)
13
+ fixture_path = File.dirname(__FILE__) + '/fixtures'
14
+ Hpricot.XML(File.read("#{fixture_path}/#{method_call}.xml"))
15
+ end
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reagent-fleakr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Reagan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-29 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.6.0
23
+ version:
24
+ description:
25
+ email: reaganpr@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.markdown
32
+ files:
33
+ - README.markdown
34
+ - Rakefile
35
+ - lib/fleakr
36
+ - lib/fleakr/request.rb
37
+ - lib/fleakr/response.rb
38
+ - lib/fleakr/set.rb
39
+ - lib/fleakr/user.rb
40
+ - lib/fleakr/version.rb
41
+ - lib/fleakr.rb
42
+ - test/fixtures
43
+ - test/fixtures/people.findByUsername.xml
44
+ - test/fixtures/photosets.getList.xml
45
+ - test/fleakr
46
+ - test/fleakr/request_test.rb
47
+ - test/fleakr/response_test.rb
48
+ - test/fleakr/set_test.rb
49
+ - test/fleakr/user_test.rb
50
+ - test/test_helper.rb
51
+ has_rdoc: true
52
+ homepage: http://sneaq.net
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: A teeny tiny gem to interface with Flickr photostreams
77
+ test_files: []
78
+