spruce 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,7 +24,7 @@ gem until the Forrst API returns the correct MIME type and status codes.
24
24
 
25
25
  # Retrieve a user by username
26
26
  kyle = Forrst::User.find 'kyle'
27
-
27
+
28
28
  # Kyles latest PUBLIC posts
29
29
  kyle.public_posts
30
30
 
@@ -66,4 +66,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
66
66
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
67
67
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
68
68
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
69
- THE SOFTWARE.
69
+ THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,6 +1,16 @@
1
1
  require 'rubygems'
2
2
  gem 'rdoc' # Required on Mac OS X to get a “working” rdoc version
3
3
  require 'rake/rdoctask'
4
+ require 'rake/testtask'
5
+
6
+ task :default => :test
7
+
8
+ desc "Test spruce"
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.test_files = FileList['spec/*_spec.rb']
11
+ t.ruby_opts = ['-rubygems'] if defined? Gem
12
+ t.ruby_opts << '-I.'
13
+ end
4
14
 
5
15
  desc 'Generate RDoc documentation for the spruce gem.'
6
16
  Rake::RDocTask.new(:rdoc) do |rdoc|
@@ -10,10 +20,10 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
10
20
  exclude('lib/leaf/view_helpers/sinatra.rb').
11
21
  exclude('lib/leaf/core_ext.rb').
12
22
  exclude('lib/leaf/version.rb')
13
-
23
+
14
24
  rdoc.main = "README.rdoc" # page to start on
15
25
  rdoc.title = "leaf documentation"
16
-
26
+
17
27
  rdoc.rdoc_dir = 'doc' # rdoc output folder
18
28
  rdoc.options << '--inline-source' << '--charset=UTF-8' << '--format=darkfish'
19
29
  rdoc.options << '--main=README.rdoc'
@@ -1,12 +1,13 @@
1
+ # encoding: utf-8
1
2
  require 'spruce'
2
3
 
3
4
  module Forrst
4
5
  API_URI = "http://api.forrst.com/api/"
5
- API_VERSION = 'v1'
6
-
6
+ API_VERSION = 'v2'
7
+
7
8
  ## Sections of the Forrst API
8
9
  require 'forrst/user'
9
-
10
+
10
11
  ## Exceptions
11
12
  module Exceptions
12
13
  class InvalidUserId < ArgumentError
@@ -14,23 +15,23 @@ module Forrst
14
15
  'Invalid User ID, must be an Integer'
15
16
  end
16
17
  end
17
-
18
+
18
19
  class InvalidPostId < ArgumentError
19
20
  def to_s
20
21
  'Invalid Post ID, must be an Integer'
21
22
  end
22
23
  end
23
-
24
+
24
25
  class InvalidUsername < ArgumentError
25
26
  def to_s
26
27
  'Invalid username, must be a String'
27
28
  end
28
29
  end
29
-
30
+
30
31
  class UserNotFound < ArgumentError
31
32
  def to_s
32
33
  'User not found on Forrst'
33
34
  end
34
35
  end
35
36
  end
36
- end
37
+ end
@@ -1,11 +1,21 @@
1
+ # encoding: utf-8
1
2
  require 'forrst/post'
2
3
 
3
4
  module Forrst
4
5
  class ListOfPosts < Array
5
- attr_accessor :status, :raw_data
6
-
6
+ attr_reader :status, :in, :authenticated, :authenticated_as, :environment, :raw_data
7
+
8
+ def initialize(data)
9
+ @status = data['stat']
10
+ @in = data['in']
11
+ @authenticated = data['authed']
12
+ @authenticated_as = data['authed_as']
13
+ @environment = data['env']
14
+ @raw_data = data
15
+ end
16
+
7
17
  def add_post(post)
8
18
  self << post
9
19
  end
10
20
  end
11
- end
21
+ end
@@ -1,41 +1,42 @@
1
+ # encoding: utf-8
1
2
  require 'time'
2
3
 
3
4
  module Forrst
4
5
  class Post
5
6
  attr_accessor :raw_data
6
-
7
+
7
8
  def initialize(data)
8
9
  populate(data)
9
10
  end
10
-
11
+
11
12
  def populate(data)
12
13
  # Save the raw data hash
13
14
  @raw_data = data
14
-
15
+
15
16
  # Standard fields
16
17
  standard_fields = [
17
18
  'slug', 'tag_string', 'tiny_id', 'faved_by_current_user', 'title',
18
- 'markdown_description', 'markdown_content', 'reply_key', 'reply_url',
19
- 'current_user_likes_this', 'url', 'url_with_wbr', 'post_type',
19
+ 'markdown_description', 'markdown_content', 'reply_key', 'reply_url',
20
+ 'current_user_likes_this', 'url', 'url_with_wbr', 'post_type',
20
21
  'in_reply_to_post_id', 'post_url', 'phrase', 'formatted_description',
21
22
  'tag_objs', 'tags', 'images', 'snap_content_type',
22
23
  ]
23
-
24
+
24
25
  # Numeric fields such as like count and number of views
25
26
  numeric_fields = [
26
- 'like_count', 'views', 'is_public', 'id', 'comment_count',
27
+ 'like_count', 'views', 'is_public', 'id', 'comment_count',
27
28
  'short_url_redirects', 'user_id', 'snap_file_size'
28
29
  ]
29
-
30
+
30
31
  # Date fields
31
32
  date_fields = [
32
33
  'created_at', 'updated_at', 'snap_updated_at'
33
34
  ]
34
-
35
+
35
36
  standard_fields.map { |f| instance_variable_set("@#{f}", data[f]) }
36
37
  numeric_fields.map { |f| instance_variable_set("@#{f}", data[f].to_i) }
37
38
  date_fields.map { |f| instance_variable_set("@#{f}", f.match(/\d\d\d\d/) ? Time.parse(data[f]) : nil) }
38
-
39
+
39
40
  # Define accessor methods
40
41
  (class << self; self; end).class_eval do
41
42
  # Handle the standard fields
@@ -47,4 +48,4 @@ module Forrst
47
48
  end
48
49
  end
49
50
  end
50
- end
51
+ end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'uri'
2
3
  require 'yajl'
3
4
  require 'rest_client'
@@ -7,47 +8,41 @@ require 'forrst/user/public_posts'
7
8
  module Forrst
8
9
  class User
9
10
  INFO_RESOURCE = "#{API_URI}#{API_VERSION}/users/info"
10
-
11
+
11
12
  attr_reader :username, :id, :raw_data
12
-
13
+
13
14
  def self.find(id)
14
15
  (id.is_a? Fixnum) ? self.find_by_user_id(id) : self.find_by_username(id)
15
16
  end
16
-
17
+
17
18
  def initialize(uri)
18
19
  begin
19
20
  response = RestClient.get uri
20
-
21
+
21
22
  if response.code == 200
22
- @raw_data = Yajl::Parser.parse(response.body)['resp']['user']
23
+ @raw_data = Yajl::Parser.parse(response.body)['resp']
23
24
  @username = @raw_data['username']
24
- @id = @raw_data['id'].to_i # Should be a Fixnum from the start
25
+ @id = @raw_data['id'].to_i # Should be a Fixnum from the start
25
26
  end
26
-
27
27
  rescue RestClient::ResourceNotFound
28
28
  raise Exceptions::UserNotFound
29
29
  end
30
30
  end
31
-
31
+
32
32
  def public_posts
33
33
  @posts ||= PublicPosts.new(username)
34
- @posts.latest
34
+ @posts.retrieve
35
35
  end
36
-
37
- def public_posts_before(post_id)
38
- @posts ||= PublicPosts.new(username)
39
- @posts.before(post_id)
40
- end
41
-
36
+
42
37
  private
43
38
  def self.find_by_user_id(user_id)
44
39
  raise Exceptions::InvalidId unless user_id.is_a? Fixnum
45
40
  self.new("#{INFO_RESOURCE}?id=#{user_id}")
46
41
  end
47
-
42
+
48
43
  def self.find_by_username(username)
49
44
  raise Exceptions::InvalidUsername unless username.is_a? String
50
45
  self.new("#{INFO_RESOURCE}?username=#{username}")
51
46
  end
52
47
  end
53
- end
48
+ end
@@ -1,38 +1,24 @@
1
+ # encoding: utf-8
1
2
  require 'forrst/list_of_posts'
2
3
 
3
4
  module Forrst
4
5
  class User
5
6
  class PublicPosts
7
+ attr_reader :posts, :username
6
8
  RESOURCE = "#{API_URI}#{API_VERSION}/users/posts"
7
-
8
- def latest
9
- return @posts unless @posts.nil?
9
+
10
+ def retrieve
11
+ return posts unless posts.nil?
10
12
  response = get("#{RESOURCE}?username=#{@username}")
11
-
12
- @posts = ListOfPosts.new
13
- response['resp']['posts'].map { |p|
14
- @posts.add_post(Forrst::Post.new(p))
13
+
14
+ @posts = ListOfPosts.new(response)
15
+
16
+ response['resp'].map { |p|
17
+ @posts.add_post(Forrst::Post.new(p))
15
18
  }
16
- @posts.status = response['resp']['stat']
17
- @posts.raw_data = response
18
-
19
19
  @posts
20
20
  end
21
-
22
- def before(post_id)
23
- raise Exceptions::InvalidPostId unless post_id.is_a? Fixnum
24
- response = get("#{RESOURCE}?username=#{@username}&last_id=#{post_id}")
25
-
26
- posts = ListOfPosts.new
27
- response['resp']['posts'].map { |p|
28
- posts.add_post(Forrst::Post.new(p))
29
- }
30
- posts.status = response['resp']['stat']
31
- posts.raw_data = response
32
-
33
- posts
34
- end
35
-
21
+
36
22
  def initialize(username)
37
23
  raise Exceptions::InvalidUsername unless username.is_a? String
38
24
  @username = username
@@ -48,4 +34,4 @@ module Forrst
48
34
  end
49
35
  end
50
36
  end
51
- end
37
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  # = Spruce - a Ruby wrapper around the Forrst API
2
4
  #
3
5
  # Happy Forrsting!
@@ -6,4 +8,4 @@ require 'forrst'
6
8
 
7
9
  module Spruce
8
10
  require 'spruce/version'
9
- end
11
+ end
@@ -1,9 +1,11 @@
1
+ # encoding: utf-8
2
+
1
3
  module Spruce
2
4
  module VERSION
3
5
  MAJOR = 0
4
6
  MINOR = 0
5
- TINY = 1
6
-
7
+ TINY = 2
8
+
7
9
  STRING = [MAJOR, MINOR, TINY].join('.')
8
10
  end
9
- end
11
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ gem 'minitest'
4
+
5
+ require 'minitest/pride'
6
+ require 'minitest/autorun'
7
+ require 'minitest/spec'
8
+
9
+ require File.expand_path('../../lib/spruce.rb', __FILE__)
10
+
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../spec_helper.rb', __FILE__)
3
+
4
+ describe "spruce" do
5
+ it "should find a user by username" do
6
+ user = Forrst::User.find('peterhellberg')
7
+ user.id.must_equal 5079
8
+ end
9
+
10
+ it "should find a user by id" do
11
+ user = Forrst::User.find(1)
12
+ user.username.must_equal 'kyle'
13
+ end
14
+
15
+ it "should find a users public posts" do
16
+ user = Forrst::User.find('peterhellberg')
17
+
18
+ user.public_posts.status.must_equal 'ok'
19
+ user.public_posts.environment.must_equal 'prod'
20
+ user.public_posts.authenticated.must_equal false
21
+
22
+ user.public_posts.each do |post|
23
+ if post.tiny_id == 'yNS'
24
+ post.post_type.must_equal 'code'
25
+ break
26
+ end
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spruce
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
4
+ prerelease:
5
+ version: 0.0.2
11
6
  platform: ruby
12
7
  authors:
13
8
  - Peter Hellberg
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-08-26 00:00:00 +02:00
13
+ date: 2011-03-23 00:00:00 +01:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,11 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 0
32
- - 7
33
- - 7
34
24
  version: 0.7.7
35
25
  type: :runtime
36
26
  version_requirements: *id001
@@ -42,14 +32,20 @@ dependencies:
42
32
  requirements:
43
33
  - - ">="
44
34
  - !ruby/object:Gem::Version
45
- hash: 15
46
- segments:
47
- - 1
48
- - 6
49
- - 0
50
35
  version: 1.6.0
51
36
  type: :runtime
52
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: minitest
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
53
49
  description: Spruce is a Ruby wrapper around the Forrst API. The Forrst API is currently a _moving target_ and the plan is to develop Spruce alongside it, as close as possible.
54
50
  email: peter@c7.se
55
51
  executables: []
@@ -67,6 +63,8 @@ files:
67
63
  - lib/forrst.rb
68
64
  - lib/spruce/version.rb
69
65
  - lib/spruce.rb
66
+ - spec/spec_helper.rb
67
+ - spec/spruce_spec.rb
70
68
  - MIT-LICENSE
71
69
  - Rakefile
72
70
  - README.rdoc
@@ -86,23 +84,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
84
  requirements:
87
85
  - - ">="
88
86
  - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
87
  version: "0"
93
88
  required_rubygems_version: !ruby/object:Gem::Requirement
94
89
  none: false
95
90
  requirements:
96
91
  - - ">="
97
92
  - !ruby/object:Gem::Version
98
- hash: 3
99
- segments:
100
- - 0
101
93
  version: "0"
102
94
  requirements: []
103
95
 
104
96
  rubyforge_project: spruce
105
- rubygems_version: 1.3.7
97
+ rubygems_version: 1.6.1
106
98
  signing_key:
107
99
  specification_version: 3
108
100
  summary: Spruce is a Ruby wrapper around the Forrst API