seban-rflak 0.2

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 ADDED
@@ -0,0 +1,49 @@
1
+ == rflak
2
+
3
+ rflak is simple gem to use flaker.pl API. flaker.pl is polish microblogging website. For more information
4
+ please visit http://flaker.pl.
5
+
6
+ You can simply user two classes Flaker and Traker. Flaker class can fetch all entries scoped with
7
+ conditions described in flaker's API. You can check API http://blog.flaker.pl/api described in polish.
8
+
9
+ Flaker.fetch('user') do |flak|
10
+ flak.login 'seban'
11
+ flak.limit 2
12
+ flak.tag 'ruby'
13
+ end
14
+
15
+ With Traker class it is possible to fetch entries from traker
16
+ service. You can visit http://flaker.pl/traker to see traker in your browser.
17
+
18
+ Traker.fetch do |traker|
19
+ traker.url 'http://blog.sebastiannowak.net'
20
+ traker.limit 20
21
+ end
22
+
23
+ User operations
24
+
25
+ Authenticate user by login and API key.
26
+
27
+ user = User.auth('my_login', 'my_api_key')
28
+ # user authenticated
29
+ user.nil? == false
30
+ # user not authenticated
31
+ user.nil? == true
32
+
33
+ Create new entry, only authenticated user can post new entry
34
+
35
+ user = User.auth('my_login', 'my_api_key')
36
+ Entry.create(user, { :text => "My super text" }
37
+
38
+ Get user's tags, bookmarks, followers and following users:
39
+
40
+ user.tags
41
+ user.bookmarks
42
+ user.followers
43
+ user.following
44
+
45
+ Bookmark entry. Only authorized user can bookmark entry.
46
+
47
+ entry = Flaker.fetch('show' { |f| f.entry_id(123456) }
48
+ user = User.auth('my_login', 'my_api_key')
49
+ entry.bookmark(user) if user
data/lib/comment.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Rflak
2
+ # Class represents single comment assigned to 'flak'
3
+ class Comment
4
+ def initialize(options = {})
5
+ end
6
+ end
7
+ end
data/lib/entry.rb ADDED
@@ -0,0 +1,129 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ module Rflak
4
+ # Class represents single entry in flaker.pl website. Entry is assigned with user to wich it belongs
5
+ # and can have many comments assigned to it.
6
+ class Entry
7
+ ATTR_LIST = [:user, :permalink, :timestamp, :comments, :time, :text, :title, :has_video, :id,
8
+ :has_photo, :link, :has_link, :datetime, :source, :data, :subsource
9
+ ]
10
+
11
+ # define attribute methods public getters and private setters
12
+ ATTR_LIST.each do |attr|
13
+ attr_reader attr
14
+ attr_writer(attr)
15
+ private attr.to_s + '='
16
+ end
17
+
18
+ def initialize(options = {})
19
+ options.each_pair do |key, value|
20
+ send("#{ key }=", value)
21
+ end
22
+ end
23
+
24
+
25
+ # Create new entry. Method raises NotAuthorized exception when passed user is not authorized. If
26
+ # operation is successfull new entry will be returned.
27
+ #
28
+ # Rflak::Entry.create(User.auth('good_login','good_api_key', { 'text' => 'some test content')
29
+ #
30
+ # user:: User
31
+ #
32
+ # content:: Hash
33
+ #
34
+ # returns:: Entry
35
+ def self.create(user, content)
36
+ raise NotAuthorized.new('Not authorized') unless user.authorized?
37
+
38
+ url = URI.parse('http://api.flaker.pl/api/type:submit')
39
+ post = Net::HTTP::Post.new(url.path)
40
+ post.basic_auth(user.login, user.api_key)
41
+ post.set_form_data(content)
42
+
43
+ response = Net::HTTP.start(url.host,url.port) do |http|
44
+ http.request(post)
45
+ end
46
+ entry_id = JSON.parse(response.body)['status']['info'].split('/').last
47
+ Flaker.fetch('show') { |f| f.entry_id(entry_id) }.first
48
+ end
49
+
50
+
51
+ # Mark entry as good "fajne!"
52
+ #
53
+ # user:: User
54
+ def good(user)
55
+ Entry.create(user, { 'text' => "@#{ self.id } fajne" })
56
+ end
57
+
58
+
59
+ # Mark entry as not good "niefajne!"
60
+ #
61
+ # user:: User
62
+ def not_good(user)
63
+ Entry.create(user, { 'text' => "@#{ self.id } niefajne" })
64
+ end
65
+
66
+ # Mark entry as lans "lans!"
67
+ #
68
+ # user:: User
69
+ def lans(user)
70
+ Entry.create(user, { 'text' => "@#{ self.id } lans" })
71
+ end
72
+
73
+
74
+ # Mark entry as stupid "głupie!"
75
+ #
76
+ # user:: User
77
+ def stupid(user)
78
+ Entry.create(user, { 'text' => "@#{ self.id } głupie" })
79
+ end
80
+
81
+
82
+ # Mark entry as bookmark
83
+ #
84
+ # user:: Rflak::User
85
+ def bookmark(user)
86
+ resp = Flaker.auth_connection(user) do |connection|
87
+ connection.get("/type:bookmark/action:set/entry_id:#{ self.id }")
88
+ end
89
+
90
+ if resp['status']['code'].to_i == 200
91
+ user.bookmarks
92
+ end
93
+ end
94
+
95
+
96
+ # Unmark entry as bookmark
97
+ #
98
+ # user:: Rflak::User
99
+ def unbookmark(user)
100
+ resp = Flaker.auth_connection(user) do |connection|
101
+ connection.get("/type:bookmark/action:unset/entry_id:#{ self.id }")
102
+ end
103
+
104
+ if resp['status']['code'].to_i == 200
105
+ user.bookmarks
106
+ end
107
+ end
108
+
109
+
110
+ private
111
+
112
+
113
+ # Set the user based on passed values
114
+ #
115
+ # options:: Hash, default empty Hash
116
+ def user=(options = {})
117
+ @user = User.new(options)
118
+ end
119
+
120
+
121
+ # Set the comments collection based on passed value
122
+ #
123
+ # options:: Array of Hashes, default []
124
+ def comments=(collection = [])
125
+ return collection if collection.empty?
126
+ @comments = collection.map { |comment_hash| Comment.new(comment_hash) }
127
+ end
128
+ end
129
+ end
data/lib/flaker.rb ADDED
@@ -0,0 +1,120 @@
1
+ module Rflak
2
+ # Utility class for fetching 'flak's from the web.
3
+ #
4
+ # Example:
5
+ # Flaker.fetch('user') do |flak|
6
+ # flak.login 'seban'
7
+ # flak.limit 2
8
+ # flak.tag 'ruby'
9
+ # end
10
+ class Flaker
11
+ include HTTParty
12
+
13
+ FLAK_API_URL = 'http://api.flaker.pl/api'
14
+ base_uri FLAK_API_URL
15
+
16
+ attr_reader :perform_url
17
+
18
+ def initialize(type)
19
+ @perform_url = FLAK_API_URL + "/type:#{ type }"
20
+ end
21
+
22
+
23
+ def self.fetch(type)
24
+ flak = Flaker.new(type)
25
+ yield(flak) if block_given?
26
+ parse_response get(flak.perform_url)
27
+ end
28
+
29
+
30
+ # Authorize connetion by user's credentials (login and api key) and perform instruction passed in
31
+ # block. Raises NotAuthorize exception when user has bad login or api key.
32
+ #
33
+ # user:: User
34
+ def self.auth_connection(user)
35
+ user.auth unless user.authorized?
36
+ raise NotAuthorized.new('Not authorized') unless user.authorized?
37
+ Flaker.basic_auth(user.login, user.api_key)
38
+ response = yield(Flaker)
39
+ Flaker.basic_auth('','')
40
+ return response
41
+ end
42
+
43
+
44
+ def self.parse_response(response)
45
+ response['entries'].map do |entry|
46
+ Entry.new(entry)
47
+ end
48
+ end
49
+
50
+
51
+ def login(value)
52
+ @perform_url += "/login:#{ value }"
53
+ end
54
+
55
+
56
+ def url(value)
57
+ @perform_url += "/url:#{ value }"
58
+ end
59
+
60
+
61
+ def entry_id(value)
62
+ @perform_url += "/entry_id:#{ value }"
63
+ end
64
+
65
+
66
+ def story(story_id)
67
+ @perform_url += "/story:#{ story_id }"
68
+ end
69
+
70
+
71
+ def source(value)
72
+ @perform_url += "/source:#{ value }"
73
+ end
74
+
75
+
76
+ def tag(value)
77
+ @perform_url += "/tag:#{ value }"
78
+ end
79
+
80
+
81
+ def mode(value)
82
+ @perform_url += "/mode:#{ value }"
83
+ end
84
+
85
+
86
+ def avatars(value)
87
+ @perform_url += "/avatars:#{ value }"
88
+ end
89
+
90
+
91
+ def limit(value)
92
+ @perform_url += "/limit:#{ value }"
93
+ end
94
+
95
+
96
+ def from(value)
97
+ @perform_url += "/from:#{ value }"
98
+ end
99
+
100
+
101
+ def start(value)
102
+ @perform_url += "/start:#{ value }"
103
+ end
104
+
105
+
106
+ def since(value)
107
+ @perform_url += "/since:#{ value }"
108
+ end
109
+
110
+
111
+ def sort(value)
112
+ @perform_url += "/sort:#{ value }"
113
+ end
114
+
115
+
116
+ def comments(value)
117
+ @perform_url += "/comments:#{ value }"
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,4 @@
1
+ module Rflak
2
+ class NotAuthorized < RuntimeError
3
+ end
4
+ end
data/lib/rflak.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'not_authorized'
4
+ require 'comment'
5
+ require 'user'
6
+ require 'entry'
7
+ require 'traker'
8
+ require 'flaker'
data/lib/traker.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+
4
+ module Rflak
5
+ # Utility class to fetch entries from flaker.pl traker service
6
+ #
7
+ # Example:
8
+ # Traker.fetch do |traker|
9
+ # traker.url 'http://blog.sebastiannowak.net'
10
+ # traker.limit 20
11
+ # end
12
+ class Traker
13
+ include HTTParty
14
+
15
+ FLAK_API_URL = 'http://api.flaker.pl/api/type:traker'
16
+
17
+ attr_reader :perform_url
18
+
19
+ def initialize
20
+ @perform_url = FLAK_API_URL
21
+ end
22
+
23
+
24
+ def self.fetch
25
+ traker = Traker.new
26
+ yield(traker) if block_given?
27
+ parse_response get(traker.perform_url)
28
+ end
29
+
30
+
31
+ def url(url)
32
+ @perform_url += "/url:#{ url }"
33
+ end
34
+
35
+
36
+ def format(format)
37
+ @perform_url += "/format:#{ format }"
38
+ end
39
+
40
+
41
+ def avatars(size)
42
+ @perform_url += "/avatars:#{ size }"
43
+ end
44
+
45
+
46
+ def limit(value)
47
+ @perform_url += "/limit:#{ value }"
48
+ end
49
+
50
+
51
+ protected
52
+
53
+
54
+ def self.parse_response(response)
55
+ response['users'].map do |user_hash|
56
+ User.new(user_hash)
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/user.rb ADDED
@@ -0,0 +1,98 @@
1
+ module Rflak
2
+ # Class represents single user of flaker.pl.
3
+ class User
4
+ ATTR_LIST = [:action, :url, :sex, :avatar, :login, :api_key]
5
+
6
+ # define attribute methods public getters and private setters
7
+ ATTR_LIST.each do |attr|
8
+ attr_reader attr
9
+ attr_writer(attr)
10
+ private attr.to_s + '='
11
+ end
12
+
13
+ def initialize(options = {})
14
+ options.each_pair do |key, value|
15
+ send("#{ key }=", value)
16
+ end
17
+ end
18
+
19
+
20
+ # Try to authorize user with login and api_key. Method returns User object for good credentails
21
+ # or nil for bad.
22
+ #
23
+ # login:: String
24
+ #
25
+ # api_key:: String
26
+ def self.auth(login, api_key)
27
+ user = User.new(:login => login, :api_key => api_key)
28
+ user.auth
29
+ user.authorized? ? user : nil
30
+ end
31
+
32
+
33
+ # Authorize User instance with login and api_key. Returns true for valid credentaials of false
34
+ # if not.
35
+ #
36
+ # returns:: True of False
37
+ def auth
38
+ Flaker.basic_auth(@login, @api_key)
39
+ resp = Flaker.get('/type:auth')
40
+ Flaker.basic_auth('', '')
41
+ @authorized = (resp['status']['code'] == "200")
42
+ end
43
+
44
+
45
+ # Returns true if User instance is authorized
46
+ #
47
+ # returns:: True or False
48
+ def authorized?
49
+ @authorized || false
50
+ end
51
+
52
+
53
+ # Returns array of watched tags. Raises Rflak::NotAuthorized if not authorized user
54
+ #
55
+ # returns:: Array
56
+ def tags
57
+ resp = Flaker.auth_connection(self) do |connection|
58
+ connection.get("/type:list/source:tags/login:#{ @login }")
59
+ end
60
+ return resp['tags']
61
+ end
62
+
63
+
64
+ # Returns array of favourited entries. Raises Rflak::NotAuthorized if not authorized user
65
+ #
66
+ # returns:: Array
67
+ def bookmarks
68
+ resp = Flaker.auth_connection(self) do |connection|
69
+ connection.get("/type:list/source:bookmarks/login:#{ @login }")
70
+ end
71
+ resp['bookmarks'].map do |id|
72
+ Flaker.fetch('show') { |flak| flak.entry_id(id) }
73
+ end
74
+ end
75
+
76
+
77
+ # Return login list of users that follow user
78
+ #
79
+ # returns:: Array
80
+ def followers
81
+ resp = Flaker.auth_connection(self) do |connection|
82
+ connection.get("/type:list/source:followedby/login:#{ @login }")
83
+ end
84
+ return resp['followedby']
85
+ end
86
+
87
+
88
+ # Returns login list of users following by user
89
+ #
90
+ # returns:: Array
91
+ def following
92
+ resp = Flaker.auth_connection(self) do |connection|
93
+ connection.get("/type:list/source:following/login:#{ @login }")
94
+ end
95
+ return resp['following']
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,9 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+ require "test/unit"
3
+ require 'comment'
4
+
5
+ class CommentTest < Test::Unit::TestCase
6
+ def test_true
7
+ assert true
8
+ end
9
+ end
@@ -0,0 +1,127 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'rflak'
5
+ require 'flexmock/test_unit'
6
+
7
+ class EntryTest < Test::Unit::TestCase
8
+ def setup
9
+ @hash = {
10
+ "user"=>{"action"=>"wrzuci\305\202 link na Flakera", "url"=>"http://flaker.pl/prawnik10",
11
+ "sex"=>"m", "avatar"=>"http://static0.flaker.pl/static/images/flaker/default_avatar_50.jpg",
12
+ "login"=>"prawnik10" },
13
+ "permalink"=>"http://flaker.pl/f/1899329",
14
+ "timestamp"=>"1245317709",
15
+ "comments"=>[],
16
+ "time"=>"Thu, 18 Jun 2009 11:35:09 +0200",
17
+ "text"=>"Jakiś fajny tekst wrzucony na flakerka. Przepraszam oryginalnego autora - prawnik10 za zmianę ;)",
18
+ "source"=>"flaker",
19
+ "data"=>[]
20
+ }
21
+
22
+ @entry = Rflak::Entry.new(@hash)
23
+ end
24
+
25
+
26
+ def test_respond_to_attribute_getters
27
+ [:user, :permalink, :timestamp, :comments, :time, :text, :title, :has_video, :id,
28
+ :has_photo, :link, :has_link, :datetime, :source, :data
29
+ ].each do |attribute|
30
+ assert @entry.respond_to?(attribute)
31
+ end
32
+ end
33
+
34
+
35
+ def test_new_object
36
+ assert_kind_of(Rflak::User, @entry.user)
37
+ end
38
+
39
+
40
+ def test_create_new_entry_unauthorized_user
41
+ user = flexmock('user')
42
+ user.should_receive(:authorized?).and_return(false)
43
+
44
+ assert_raise(Rflak::NotAuthorized) do
45
+ Rflak::Entry.create(user, 'text' => 'test content')
46
+ end
47
+ end
48
+
49
+
50
+ def test_create_new_entry_authorized_user
51
+ user = flexmock('user')
52
+ user.should_receive(:authorized?).and_return(true)
53
+ user.should_receive(:login).and_return('testuser')
54
+ user.should_receive(:api_key).and_return('test_api_key')
55
+ response_body = "\r\n\r\n{\"status\":{\"code\":\"200\",\"text\":\"OK\",\"info\":\"http:\\/\\/flaker.pl\\/f\\/2077989\"}}"
56
+ response = flexmock('response')
57
+ response.should_receive(:body).and_return(response_body)
58
+ flexmock(Net::HTTP).should_receive(:start).and_return(response)
59
+ flexmock(Rflak::Flaker).should_receive(:fetch).and_return([Rflak::Entry.new])
60
+
61
+ response = Rflak::Entry.create(user, 'text' => 'test content')
62
+ assert_kind_of(Rflak::Entry, response)
63
+ end
64
+
65
+
66
+ def test_bookmark_authorized_user
67
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
68
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
69
+
70
+ assert_equal false, user.auth
71
+ assert_raise(Rflak::NotAuthorized) { @entry.bookmark(user) }
72
+ end
73
+
74
+
75
+ def test_bookmark_authorized_user
76
+ bookmark_response = {"status"=>{"text"=>"OK", "code"=>"200", "info"=>"bookmark created"}}
77
+ bookmark_list_response = { 'bookmarks' => %w(1 2 3) }
78
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
79
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:bookmark/action:set/entry_id:#{ @entry.id }").times(1).and_return(bookmark_response)
80
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:list/source:bookmarks/login:login').times(1).and_return(bookmark_list_response)
81
+ flexmock(Rflak::Flaker).should_receive(:fetch).with("show", Proc).times(3).and_return(Rflak::Entry.new)
82
+
83
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
84
+
85
+ resp = @entry.bookmark(user)
86
+ assert_kind_of(Array, resp)
87
+ resp.each { |r| assert_kind_of(Rflak::Entry, r) }
88
+ end
89
+
90
+
91
+ def test_unbookmark_not_authorized_user
92
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
93
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
94
+
95
+ assert_equal false, user.auth
96
+ assert_raise(Rflak::NotAuthorized) { @entry.unbookmark(user) }
97
+ end
98
+
99
+
100
+ def test_unbookmark_authorized_user
101
+ bookmark_response = {"status"=>{"text"=>"OK", "code"=>"200", "info"=>"i dont't know"}}
102
+ bookmark_list_response = { 'bookmarks' => %w(1 2 3) }
103
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
104
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:bookmark/action:unset/entry_id:#{ @entry.id }").times(1).and_return(bookmark_response)
105
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:list/source:bookmarks/login:login').times(1).and_return(bookmark_list_response)
106
+ flexmock(Rflak::Flaker).should_receive(:fetch).with("show", Proc).times(3).and_return(Rflak::Entry.new)
107
+
108
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
109
+
110
+ resp = @entry.unbookmark(user)
111
+ assert_kind_of(Array, resp)
112
+ resp.each { |r| assert_kind_of(Rflak::Entry, r) }
113
+ end
114
+
115
+
116
+ protected
117
+
118
+
119
+ def auth_bad_credentials
120
+ {"status" => {"code" => "401","text" => "Unauthorized","info" => "authorization is required"}}
121
+ end
122
+
123
+
124
+ def auth_good_credentials
125
+ { "status" => {"code" => "200","text" => "OK","info" =>"authorization successful"}}
126
+ end
127
+ end
@@ -0,0 +1,139 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'flaker'
5
+ require 'rubygems'
6
+ require 'flexmock/test_unit'
7
+
8
+ class FlakerTest < Test::Unit::TestCase
9
+ def setup
10
+ @flak_connection = Rflak::Flaker.new('user')
11
+ end
12
+
13
+
14
+ def test_fetch
15
+ flexmock(Rflak::Flaker).should_receive(:get).and_return(flaker_fake_response).times(1)
16
+ response = Rflak::Flaker.fetch('user') do |conn|
17
+ conn.login 'dstranz'
18
+ conn.limit 2
19
+ end
20
+ assert_kind_of(Array, response)
21
+ response.each do |entry|
22
+ assert_kind_of(Rflak::Entry, entry)
23
+ assert_kind_of(Rflak::User, entry.user)
24
+ end
25
+ end
26
+
27
+
28
+ def test_login
29
+ assert_equal @flak_connection.perform_url + '/login:seban', @flak_connection.login('seban')
30
+ end
31
+
32
+
33
+ def test_url
34
+ assert_equal @flak_connection.perform_url + '/url:http://google.pl', @flak_connection.url('http://google.pl')
35
+ end
36
+
37
+
38
+ def test_entry_id
39
+ assert_equal @flak_connection.perform_url + '/entry_id:1', @flak_connection.entry_id(1)
40
+ end
41
+
42
+
43
+ def test_story
44
+ assert_equal @flak_connection.perform_url + '/story:1', @flak_connection.story(1)
45
+ end
46
+
47
+
48
+ def test_source
49
+ assert_equal @flak_connection.perform_url + '/source:photos', @flak_connection.source('photos')
50
+ end
51
+
52
+
53
+ def test_tag
54
+ assert_equal @flak_connection.perform_url + '/tag:ruby', @flak_connection.tag('ruby')
55
+ end
56
+
57
+
58
+ def test_mode
59
+ assert_equal @flak_connection.perform_url + '/mode:raw', @flak_connection.mode('raw')
60
+ end
61
+
62
+
63
+ def test_avatars
64
+ assert_equal @flak_connection.perform_url + '/avatars:small', @flak_connection.avatars('small')
65
+ end
66
+
67
+
68
+ def test_limit
69
+ assert_equal @flak_connection.perform_url + '/limit:50', @flak_connection.limit('50')
70
+ end
71
+
72
+
73
+ def test_from
74
+ assert_equal @flak_connection.perform_url + '/from:100', @flak_connection.from('100')
75
+ end
76
+
77
+
78
+ def test_start
79
+ assert_equal @flak_connection.perform_url + '/start:12345', @flak_connection.start('12345')
80
+ end
81
+
82
+
83
+ def test_sort
84
+ assert_equal @flak_connection.perform_url + '/sort:desc', @flak_connection.sort('desc')
85
+ end
86
+
87
+
88
+ def test_comments
89
+ assert_equal @flak_connection.perform_url + '/comments:false', @flak_connection.comments(false)
90
+ end
91
+
92
+
93
+ def test_since
94
+ assert_equal @flak_connection.perform_url + '/since:1234567', @flak_connection.since(1234567)
95
+ end
96
+
97
+
98
+ protected
99
+
100
+
101
+ def flaker_fake_response
102
+ # hard
103
+ { "user"=>{
104
+ "url" => "http://flaker.pl/dstranz",
105
+ "sex"=>"m",
106
+ "avatar"=>"http://static0.flaker.pl/static/images/flaker/user_generated/u_139_1227549010.jpg_50.jpeg",
107
+ "login"=>"dstranz"},
108
+ "entries"=>[
109
+ {"user" => {
110
+ "action" => "wrzuci\305\202 link na Flakera",
111
+ "url" => "http://flaker.pl/dstranz",
112
+ "sex"=> "m",
113
+ "avatar" => "http://static0.flaker.pl/static/images/flaker/user_generated/u_139_1227549010.jpg_50.jpeg",
114
+ "login"=>"dstranz" },
115
+ "permalink" => "http://flaker.pl/f/1909570",
116
+ "timestamp" => "1245366699",
117
+ "comments" => [],
118
+ "time"=> "Fri, 19 Jun 2009 01:11:39 +0200",
119
+ "text" => "tekst tekst tekst",
120
+ "title"=>"<!--deprecated-->",
121
+ "has_video"=>"0",
122
+ "id"=>"1909570",
123
+ "has_photo"=>"1",
124
+ "link"=>"",
125
+ "has_link"=>"1",
126
+ "datetime"=>"2009-06-19 01:11:39",
127
+ "source"=>"flaker",
128
+ "data" => {
129
+ "images"=>[
130
+ { "small"=>"f_139_1245369384_0_75.jpeg",
131
+ "big"=>"f_139_1245369384_0_495.jpeg",
132
+ "medium"=>"f_139_1245369384_0_285.jpeg",
133
+ "alt"=>"alt alt"
134
+ }]}
135
+ }
136
+ ]
137
+ }
138
+ end
139
+ end
@@ -0,0 +1,66 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'traker'
5
+ require 'rubygems'
6
+ require 'flexmock/test_unit'
7
+
8
+ class TrakerTest < Test::Unit::TestCase
9
+ def setup
10
+ @traker = Rflak::Traker.new
11
+ end
12
+
13
+
14
+ def test_fetch
15
+ flexmock(Rflak::Traker).new_instances.should_receive(:get).and_return(flaker_fake_response)
16
+ response = Rflak::Traker.fetch do |traker|
17
+ traker.url 'netguru.pl'
18
+ traker.limit 2
19
+ end
20
+ assert_kind_of(Array, response)
21
+ response.each do |user|
22
+ assert_kind_of(Rflak::User, user)
23
+ end
24
+ end
25
+
26
+
27
+ def test_url
28
+ assert_equal Rflak::Traker::FLAK_API_URL + "/url:http://flaker.pl", @traker.url('http://flaker.pl')
29
+ end
30
+
31
+
32
+ def test_format
33
+ assert_equal Rflak::Traker::FLAK_API_URL + '/format:raw', @traker.format('raw')
34
+ end
35
+
36
+
37
+ def test_avatars
38
+ assert_equal Rflak::Traker::FLAK_API_URL + '/avatars:medium', @traker.avatars('medium')
39
+ end
40
+
41
+
42
+ def test_limit
43
+ assert_equal Rflak::Traker::FLAK_API_URL + '/limit:10', @traker.limit(10)
44
+ end
45
+
46
+
47
+ protected
48
+
49
+
50
+ def flaker_fake_response
51
+ {
52
+ "site"=>{
53
+ "site_url"=>"http://netguru.pl",
54
+ "site_id"=>"2",
55
+ "site_createdon"=>"1205953764",
56
+ "site_deleted"=>"0",
57
+ "site_user_id"=>"55",
58
+ "site_name"=>"NETGURU" },
59
+ "users"=> [
60
+ { "url" => "http://flaker.pl/zuziak",
61
+ "avatar" => "http://static0.flaker.pl/static/images/flaker/user_generated/u_814_1244208247.jpg_50.jpeg",
62
+ "login"=>"zuziak" }
63
+ ]
64
+ }
65
+ end
66
+ end
data/test/user_test.rb ADDED
@@ -0,0 +1,140 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'rflak'
5
+ require 'rubygems'
6
+ require 'flexmock/test_unit'
7
+
8
+ class UserTest < Test::Unit::TestCase
9
+ def setup
10
+ @user = Rflak::User.new
11
+ end
12
+
13
+
14
+ def test_respond_to_attribute_getters
15
+ [:action, :url, :sex, :avatar, :login].each do |attribute|
16
+ assert @user.respond_to?(attribute)
17
+ end
18
+ end
19
+
20
+
21
+ def test_auth_user_with_bad_credentials
22
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
23
+ resp = Rflak::User.auth('login','bad_auth')
24
+ assert_nil(resp)
25
+ end
26
+
27
+
28
+ def test_auth_user_with_good_credentials
29
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_good_credentials)
30
+ resp = Rflak::User.auth('login', 'good_auth')
31
+ assert_kind_of(Rflak::User, resp)
32
+ assert_equal 'login', resp.login
33
+ assert_equal 'good_auth', resp.api_key
34
+ end
35
+
36
+
37
+ def test_get_tags_not_authorized
38
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
39
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
40
+ assert_equal false, user.auth
41
+ exception = assert_raise(Rflak::NotAuthorized) { user.tags }
42
+ assert_equal 'Not authorized', exception.message
43
+ end
44
+
45
+
46
+ def test_get_tags_authorized
47
+ tag_list_response = { 'tags' => %w(tag1 tag2 tag3) }
48
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
49
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:list/source:tags/login:login").once.and_return(tag_list_response).times(1)
50
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
51
+ assert user.auth
52
+ tags = user.tags
53
+ assert_equal tag_list_response['tags'], tags
54
+ end
55
+
56
+
57
+ def test_get_bookmarks_not_authorized
58
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
59
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
60
+ assert_equal false, user.auth
61
+ exception = assert_raise(Rflak::NotAuthorized) { user.bookmarks }
62
+ assert_equal 'Not authorized', exception.message
63
+ end
64
+
65
+
66
+ def test_get_bookmarks_authorized
67
+ bookmark_list_response = { 'bookmarks' => %w(1 2 3) }
68
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
69
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:list/source:bookmarks/login:login").once.and_return(bookmark_list_response).times(1)
70
+ flexmock(Rflak::Flaker).should_receive(:fetch).with("show", Proc).times(3).and_return(Rflak::Entry.new)
71
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
72
+ assert user.auth
73
+ bookmarks = user.bookmarks
74
+ assert_equal 3, bookmarks.size
75
+ bookmarks.each do |bookmark|
76
+ assert_kind_of(Rflak::Entry, bookmark)
77
+ end
78
+ end
79
+
80
+
81
+ def test_get_followers_not_authorized
82
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
83
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
84
+ assert_equal false, user.auth
85
+ exception = assert_raise(Rflak::NotAuthorized) { user.followers }
86
+ assert_equal 'Not authorized', exception.message
87
+ end
88
+
89
+
90
+ def test_get_followers_authorized
91
+ followers_list_response = { 'followedby' => %w(seban) }
92
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
93
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:list/source:followedby/login:login").once.and_return(followers_list_response).times(1)
94
+
95
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
96
+ assert user.auth
97
+ followers = user.followers
98
+
99
+ assert_kind_of(Array, followers)
100
+ assert_equal 1, followers.size
101
+ followers.each { |f| assert_kind_of(String, f) }
102
+ end
103
+
104
+
105
+ def test_get_following_not_authorized
106
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
107
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
108
+ assert_equal false, user.auth
109
+ exception = assert_raise(Rflak::NotAuthorized) { user.following }
110
+ assert_equal 'Not authorized', exception.message
111
+ end
112
+
113
+
114
+ def test_get_following_authorized
115
+ following_list_response = { 'following' => %w(seban) }
116
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
117
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:list/source:following/login:login").once.and_return(following_list_response).times(1)
118
+
119
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
120
+ assert user.auth
121
+ following = user.following
122
+
123
+ assert_kind_of(Array, following)
124
+ assert_equal 1, following.size
125
+ following.each { |f| assert_kind_of(String, f) }
126
+ end
127
+
128
+
129
+ protected
130
+
131
+
132
+ def auth_bad_credentials
133
+ {"status" => {"code" => "401","text" => "Unauthorized","info" => "authorization is required"}}
134
+ end
135
+
136
+
137
+ def auth_good_credentials
138
+ { "status" => {"code" => "200","text" => "OK","info" =>"authorization successful"}}
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seban-rflak
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Nowak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Simple Ruby wrapper for Flaker service. Flaker is service collecting your network activities. For more details go to http://flaker.pl
26
+ email: sebastian.nowak@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - lib/flaker.rb
35
+ - lib/comment.rb
36
+ - lib/not_authorized.rb
37
+ - lib/rflak.rb
38
+ - lib/traker.rb
39
+ - lib/entry.rb
40
+ - lib/user.rb
41
+ - test/entry_test.rb
42
+ - test/comment_test.rb
43
+ - test/user_test.rb
44
+ - test/traker_test.rb
45
+ - test/flaker_test.rb
46
+ - README
47
+ has_rdoc: false
48
+ homepage: http://github.com/seban/rflak/tree/master
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Simple wraper for http://flaker.pl API
73
+ test_files: []
74
+