rflak 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,59 @@
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
50
+
51
+ Add comments to some entry:
52
+
53
+ entry = Rflak::Flaker.fetch("show") { |f| f.entry_id '2725807' ; f.comments true }.first
54
+ user = Rflak::User.auth('my_login','my_api_key')
55
+ entry.comment(user,'my comment here')
56
+ # or
57
+ Comment.create(entry, user, 'my comment here')
58
+ # or
59
+ Comment.create(entry.id, user, 'my comment here')
data/lib/comment.rb ADDED
@@ -0,0 +1,56 @@
1
+ module Rflak
2
+ # Class represents single comment assigned to 'flak'
3
+ class Comment < DummyEntry
4
+ ATTR_LIST = [:user, :permalink, :timestamp, :time, :text, :datetime, :source, :data, :subsource,
5
+ :url, :related_id
6
+ ]
7
+
8
+ # define attribute methods public getters and private setters
9
+ ATTR_LIST.each do |attr|
10
+ attr_reader attr
11
+
12
+ # user= method from dummy entry
13
+ unless attr == :user
14
+ attr_writer(attr)
15
+ protected attr.to_s + '='
16
+ end
17
+ end
18
+
19
+
20
+ def initialize(options = {})
21
+ options.each_pair do |key, value|
22
+ send("#{ key }=", value)
23
+ end
24
+ end
25
+
26
+
27
+ # Create new comment entry. Method raises NotAuthorized exception when passed user is not
28
+ # authorized. As entry parameter method takes <tt>Entry</tt> object or its <tt>id</tt> value.
29
+ # Content is passed as simply text message. When new comment is created <tt>true</tt> is returned
30
+ # otherwise returns <tt>false</tt>
31
+ #
32
+ # entry:: Entry || Fixnum
33
+ #
34
+ # user:: User
35
+ #
36
+ # returns:: Boolean
37
+ def self.create(entry, user, content)
38
+ raise NotAuthorized.new('Not authorized') unless user.authorized?
39
+
40
+ url = URI.parse('http://api.flaker.pl/api/type:submit')
41
+ post = Net::HTTP::Post.new(url.path)
42
+ post.basic_auth(user.login, user.api_key)
43
+
44
+ if entry.kind_of?(Entry)
45
+ post.set_form_data('text' => "@#{ entry.id } #{ content }")
46
+ else
47
+ post.set_form_data('text' => "@#{ entry } #{ content }")
48
+ end
49
+
50
+ response = Net::HTTP.start(url.host,url.port) do |http|
51
+ http.request(post)
52
+ end
53
+ response.code.to_i == 200
54
+ end
55
+ end
56
+ end
data/lib/entry.rb ADDED
@@ -0,0 +1,146 @@
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 < DummyEntry
7
+ ATTR_LIST = [:user, :permalink, :timestamp, :comments, :time, :text, :title, :has_video, :id,
8
+ :has_photo, :link, :has_link, :datetime, :source, :data, :subsource, :comments
9
+ ]
10
+
11
+ # define attribute methods public getters and private setters
12
+ ATTR_LIST.each do |attr|
13
+ attr_reader attr
14
+
15
+ # user= method from dummy entry
16
+ unless attr == :user
17
+ attr_writer(attr)
18
+ protected attr.to_s + '='
19
+ end
20
+ end
21
+
22
+ def initialize(options = {})
23
+ options.each_pair do |key, value|
24
+ send("#{ key }=", value)
25
+ end
26
+ end
27
+
28
+
29
+ # Create new entry. Method raises NotAuthorized exception when passed user is not authorized. If
30
+ # operation is successfull new entry will be returned.
31
+ #
32
+ # Rflak::Entry.create(User.auth('good_login','good_api_key', { 'text' => 'some test content')
33
+ #
34
+ # user:: User
35
+ #
36
+ # content:: Hash
37
+ #
38
+ # returns:: Entry
39
+ def self.create(user, content)
40
+ raise NotAuthorized.new('Not authorized') unless user.authorized?
41
+
42
+ url = URI.parse('http://api.flaker.pl/api/type:submit')
43
+ post = Net::HTTP::Post.new(url.path)
44
+ post.basic_auth(user.login, user.api_key)
45
+ post.set_form_data(content)
46
+
47
+ response = Net::HTTP.start(url.host,url.port) do |http|
48
+ http.request(post)
49
+ end
50
+ entry_id = JSON.parse(response.body)['status']['info'].split('/').last
51
+ Flaker.fetch('show') { |f| f.entry_id(entry_id) }.first
52
+ end
53
+
54
+
55
+ # Mark entry as good "fajne!"
56
+ #
57
+ # user:: User
58
+ def good(user)
59
+ Entry.create(user, { 'text' => "@#{ self.id } fajne" })
60
+ end
61
+
62
+
63
+ # Mark entry as not good "niefajne!"
64
+ #
65
+ # user:: User
66
+ def not_good(user)
67
+ Entry.create(user, { 'text' => "@#{ self.id } niefajne" })
68
+ end
69
+
70
+ # Mark entry as lans "lans!"
71
+ #
72
+ # user:: User
73
+ def lans(user)
74
+ Entry.create(user, { 'text' => "@#{ self.id } lans" })
75
+ end
76
+
77
+
78
+ # Mark entry as stupid "głupie!"
79
+ #
80
+ # user:: User
81
+ def stupid(user)
82
+ Entry.create(user, { 'text' => "@#{ self.id } głupie" })
83
+ end
84
+
85
+
86
+ # Mark entry as bookmark
87
+ #
88
+ # user:: Rflak::User
89
+ def bookmark(user)
90
+ resp = Flaker.auth_connection(user) do |connection|
91
+ connection.get("/type:bookmark/action:set/entry_id:#{ self.id }")
92
+ end
93
+
94
+ if resp['status']['code'].to_i == 200
95
+ user.bookmarks
96
+ end
97
+ end
98
+
99
+
100
+ # Unmark entry as bookmark
101
+ #
102
+ # user:: Rflak::User
103
+ def unbookmark(user)
104
+ resp = Flaker.auth_connection(user) do |connection|
105
+ connection.get("/type:bookmark/action:unset/entry_id:#{ self.id }")
106
+ end
107
+
108
+ if resp['status']['code'].to_i == 200
109
+ user.bookmarks
110
+ end
111
+ end
112
+
113
+
114
+ # Add comment to entry. <tt>NotAuthorized</tt> exception will be raised if passed user is not
115
+ # authorized.
116
+ #
117
+ # user:: User
118
+ #
119
+ # content:: String
120
+ #
121
+ # returns:: Entry
122
+ def comment(user, content)
123
+ if Comment.create(self.id, user, content)
124
+ entry = Flaker.fetch("show") { |f| f.entry_id(self.id) ; f.comments(true) }.first
125
+ self.comments = entry.comments
126
+ return entry
127
+ else
128
+ self
129
+ end
130
+ end
131
+
132
+
133
+ private
134
+
135
+
136
+ # Set the comments collection based on passed value
137
+ #
138
+ # options:: Array of Hashes, default []
139
+ def comments=(collection = [])
140
+ @comments = [] and return if collection.empty?
141
+ @comments = collection.map do |comment_hash|
142
+ comment_hash.kind_of?(Rflak::Comment) ? comment_hash : Comment.new(comment_hash)
143
+ end
144
+ end
145
+ end
146
+ 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,9 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'not_authorized'
4
+ require 'user'
5
+ require 'dummy_entry'
6
+ require 'entry'
7
+ require 'comment'
8
+ require 'traker'
9
+ 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,68 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+ require "test/unit"
3
+ require 'rflak'
4
+ require 'flexmock/test_unit'
5
+
6
+ class CommentTest < Test::Unit::TestCase
7
+ def test_create_unauthorized_user
8
+ user = flexmock('user')
9
+ user.should_receive(:authorized?).and_return(false)
10
+
11
+ assert_raise(Rflak::NotAuthorized) do
12
+ Rflak::Comment.create(12345, user, 'test content')
13
+ end
14
+ end
15
+
16
+
17
+ def test_create_new_comment_with_entry_id
18
+ mock_post_request_to_api("200")
19
+
20
+ new_comment = Rflak::Comment.create(123456, mock_authorized_user, "My comment body.")
21
+ assert_equal true, new_comment
22
+ end
23
+
24
+
25
+ def test_create_new_comment_with_entry_object
26
+ mock_post_request_to_api("200")
27
+ entry = Rflak::Entry.new
28
+
29
+ new_comment = Rflak::Comment.create(entry, mock_authorized_user, "My comment body.")
30
+ assert_equal true, new_comment
31
+ end
32
+
33
+
34
+ def test_fail_create_new_comment_with_entry_id
35
+ mock_post_request_to_api("409")
36
+
37
+ new_comment = Rflak::Comment.create(123456, mock_authorized_user, "My comment body.")
38
+ assert_equal false, new_comment
39
+ end
40
+
41
+
42
+ def test_fail_create_new_comment_with_entry_object
43
+ mock_post_request_to_api("409")
44
+ entry = Rflak::Entry.new
45
+
46
+ new_comment = Rflak::Comment.create(entry, mock_authorized_user, "My comment body.")
47
+ assert_equal false, new_comment
48
+ end
49
+
50
+
51
+ protected
52
+
53
+
54
+ def mock_authorized_user
55
+ user = flexmock('user')
56
+ user.should_receive(:authorized?).and_return(true)
57
+ user.should_receive(:login).and_return('testuser')
58
+ user.should_receive(:api_key).and_return('test_api_key')
59
+ return user
60
+ end
61
+
62
+
63
+ def mock_post_request_to_api(status)
64
+ response = flexmock('response')
65
+ response.should_receive(:code).once.and_return(status)
66
+ flexmock(Net::HTTP).should_receive(:start).once.and_return(response)
67
+ end
68
+ end
@@ -0,0 +1,163 @@
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
+ "time"=>"Thu, 18 Jun 2009 11:35:09 +0200",
16
+ "text"=>"Jakiś fajny tekst wrzucony na flakerka. Przepraszam oryginalnego autora - prawnik10 za zmianę ;)",
17
+ "source"=>"flaker",
18
+ "data"=>[],
19
+ "comments" => [
20
+ { "text" => "pi\u0119knie.... niez\u0142a kasa, bardzo niez\u0142a.",
21
+ "time" => "Fri, 18 Sep 2009 10:38:53 +0200",
22
+ "datetime" => "2009-09-18 10:38:53",
23
+ "timestamp" => "1253263133",
24
+ "permalink" => "http://flaker.pl/f/2605378-sicamp-wygral-projekt-roadar-ktory-bedzie-rowniez-realizowany-przez-polakow",
25
+ "user" => {
26
+ "login" =>"az",
27
+ "avatar" => "http:\static0.flaker.pl/static/images/flaker/user_generated/u_55_1238970606.jpg_50.jpeg",
28
+ "url" => "http://flaker.pl/az",
29
+ "sex" => "m" },
30
+ "related_id" => "2605378",
31
+ "subsource" => "internal_comment"}
32
+ ]
33
+ }
34
+
35
+ @entry = Rflak::Entry.new(@hash)
36
+ end
37
+
38
+
39
+ def test_respond_to_attribute_getters
40
+ [:user, :permalink, :timestamp, :comments, :time, :text, :title, :has_video, :id,
41
+ :has_photo, :link, :has_link, :datetime, :source, :data
42
+ ].each do |attribute|
43
+ assert @entry.respond_to?(attribute)
44
+ end
45
+ end
46
+
47
+
48
+ def test_new_object
49
+ assert_kind_of(Rflak::User, @entry.user)
50
+ assert_kind_of(Array, @entry.comments)
51
+ @entry.comments.each do |comment|
52
+ assert_kind_of(Rflak::Comment, comment)
53
+ end
54
+ end
55
+
56
+
57
+ def test_create_new_entry_unauthorized_user
58
+ user = flexmock('user')
59
+ user.should_receive(:authorized?).and_return(false)
60
+
61
+ assert_raise(Rflak::NotAuthorized) do
62
+ Rflak::Entry.create(user, 'text' => 'test content')
63
+ end
64
+ end
65
+
66
+
67
+ def test_create_new_entry_authorized_user
68
+ user = flexmock('user')
69
+ user.should_receive(:authorized?).and_return(true)
70
+ user.should_receive(:login).and_return('testuser')
71
+ user.should_receive(:api_key).and_return('test_api_key')
72
+ response_body = "\r\n\r\n{\"status\":{\"code\":\"200\",\"text\":\"OK\",\"info\":\"http:\/\/flaker.pl\/f\/2077989\"}}"
73
+ response = flexmock('response')
74
+ response.should_receive(:body).and_return(response_body)
75
+ flexmock(Net::HTTP).should_receive(:start).and_return(response)
76
+ flexmock(Rflak::Flaker).should_receive(:fetch).and_return([Rflak::Entry.new])
77
+
78
+ response = Rflak::Entry.create(user, 'text' => 'test content')
79
+ assert_kind_of(Rflak::Entry, response)
80
+ end
81
+
82
+
83
+ def test_bookmark_authorized_user
84
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
85
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
86
+
87
+ assert_equal false, user.auth
88
+ assert_raise(Rflak::NotAuthorized) { @entry.bookmark(user) }
89
+ end
90
+
91
+
92
+ def test_bookmark_authorized_user
93
+ bookmark_response = {"status"=>{"text"=>"OK", "code"=>"200", "info"=>"bookmark created"}}
94
+ bookmark_list_response = { 'bookmarks' => %w(1 2 3) }
95
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
96
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:bookmark/action:set/entry_id:#{ @entry.id }").times(1).and_return(bookmark_response)
97
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:list/source:bookmarks/login:login').times(1).and_return(bookmark_list_response)
98
+ flexmock(Rflak::Flaker).should_receive(:fetch).with("show", Proc).times(3).and_return(Rflak::Entry.new)
99
+
100
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
101
+
102
+ resp = @entry.bookmark(user)
103
+ assert_kind_of(Array, resp)
104
+ resp.each { |r| assert_kind_of(Rflak::Entry, r) }
105
+ end
106
+
107
+
108
+ def test_unbookmark_not_authorized_user
109
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').and_return(auth_bad_credentials)
110
+ user = Rflak::User.new(:login => 'login', :api_key => 'bad_key')
111
+
112
+ assert_equal false, user.auth
113
+ assert_raise(Rflak::NotAuthorized) { @entry.unbookmark(user) }
114
+ end
115
+
116
+
117
+ def test_unbookmark_authorized_user
118
+ bookmark_response = {"status"=>{"text"=>"OK", "code"=>"200", "info"=>"i dont't know"}}
119
+ bookmark_list_response = { 'bookmarks' => %w(1 2 3) }
120
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:auth').once.and_return(auth_good_credentials)
121
+ flexmock(Rflak::Flaker).should_receive(:get).with("/type:bookmark/action:unset/entry_id:#{ @entry.id }").times(1).and_return(bookmark_response)
122
+ flexmock(Rflak::Flaker).should_receive(:get).with('/type:list/source:bookmarks/login:login').times(1).and_return(bookmark_list_response)
123
+ flexmock(Rflak::Flaker).should_receive(:fetch).with("show", Proc).times(3).and_return(Rflak::Entry.new)
124
+
125
+ user = Rflak::User.new(:login => 'login', :api_key => 'good_key')
126
+
127
+ resp = @entry.unbookmark(user)
128
+ assert_kind_of(Array, resp)
129
+ resp.each { |r| assert_kind_of(Rflak::Entry, r) }
130
+ end
131
+
132
+
133
+ def test_comment
134
+ user = flexmock('user')
135
+ user.should_receive(:authorized?).and_return(true)
136
+ user.should_receive(:login).and_return('testuser')
137
+ user.should_receive(:api_key).and_return('test_api_key')
138
+
139
+ response = flexmock('response')
140
+ response.should_receive(:code).once.and_return("200")
141
+ flexmock(Net::HTTP).should_receive(:start).once.and_return(response)
142
+
143
+ entry_with_new_comment = @entry.clone
144
+ entry_with_new_comment.comments << Rflak::Comment.new(:text => "My comment")
145
+ flexmock(Rflak::Flaker).should_receive(:fetch).with("show", Proc).and_return([entry_with_new_comment])
146
+
147
+ resp = @entry.comment(user, "My comment")
148
+ assert_kind_of(Rflak::Entry, resp)
149
+ end
150
+
151
+
152
+ protected
153
+
154
+
155
+ def auth_bad_credentials
156
+ {"status" => {"code" => "401","text" => "Unauthorized","info" => "authorization is required"}}
157
+ end
158
+
159
+
160
+ def auth_good_credentials
161
+ { "status" => {"code" => "200","text" => "OK","info" =>"authorization successful"}}
162
+ end
163
+ 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,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rflak
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.3"
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Nowak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-07 00:00:00 +01: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: |
26
+ Simple Ruby wrapper for Flaker service. Flaker is service collecting your network activities.
27
+ For more details go to http://flaker.pl
28
+
29
+ email: sebastian.nowak@gmail.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files:
35
+ - README
36
+ files:
37
+ - lib/flaker.rb
38
+ - lib/comment.rb
39
+ - lib/not_authorized.rb
40
+ - lib/rflak.rb
41
+ - lib/traker.rb
42
+ - lib/entry.rb
43
+ - lib/user.rb
44
+ - test/entry_test.rb
45
+ - test/comment_test.rb
46
+ - test/user_test.rb
47
+ - test/traker_test.rb
48
+ - test/flaker_test.rb
49
+ - README
50
+ has_rdoc: true
51
+ homepage: http://github.com/seban/rflak/tree/master
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Simple wraper for http://flaker.pl API
78
+ test_files: []
79
+