spruce 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Peter Hellberg
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,69 @@
1
+ = Spruce ↟
2
+
3
+ Spruce is a Ruby wrapper around the {Forrst API}[http://forrst.com/apidocs.html].
4
+ The Forrst API is currently a <em>moving target</em> and the plan is to develop Spruce
5
+ alongside it, as close as possible. Spruce will support authentication via API tokens.
6
+
7
+ == Installation
8
+
9
+ gem install spruce
10
+
11
+ == Dependencies
12
+
13
+ The {YAJL C Bindings for Ruby}[http://github.com/brianmario/yajl-ruby] are so
14
+ full of *AWESOME* that it is almost impossible to not use them everywhere JSON
15
+ is involved, so I didn’t.
16
+
17
+ I’m also temporarily using the {REST Client}[http://github.com/archiloque/rest-client]
18
+ gem until the Forrst API returns the correct MIME type and status codes.
19
+
20
+ == Getting started
21
+
22
+ require 'rubygems'
23
+ require 'forrst' # or require 'spruce'
24
+
25
+ # Retrieve a user by username
26
+ kyle = Forrst::User.find 'kyle'
27
+
28
+ # Kyles latest PUBLIC posts
29
+ kyle.public_posts
30
+
31
+ # The type of Kyles latest PUBLIC post
32
+ kyle.public_posts[0].post_type
33
+
34
+ # Kyles PUBLIC posts before post_id 5000
35
+ kyle.public_posts_before(5000)
36
+
37
+ # Retrieve a user by id
38
+ peter = Forrst::User.find 5079
39
+
40
+ == Links
41
+
42
+ * {Forrst API}[http://forrst.com/apidocs.html]
43
+ * {YAJL C Bindings for Ruby}[http://github.com/brianmario/yajl-ruby]
44
+
45
+ == TODO
46
+
47
+ Implement the different post types by subclassing *Forrst::Post*
48
+
49
+ == LICENCE
50
+
51
+ Copyright (c) 2010 Peter Hellberg
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining a copy
54
+ of this software and associated documentation files (the "Software"), to deal
55
+ in the Software without restriction, including without limitation the rights
56
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
57
+ copies of the Software, and to permit persons to whom the Software is
58
+ furnished to do so, subject to the following conditions:
59
+
60
+ The above copyright notice and this permission notice shall be included in
61
+ all copies or substantial portions of the Software.
62
+
63
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
64
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
65
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
66
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
67
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
68
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
69
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ gem 'rdoc' # Required on Mac OS X to get a “working” rdoc version
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Generate RDoc documentation for the spruce gem.'
6
+ Rake::RDocTask.new(:rdoc) do |rdoc|
7
+ rdoc.rdoc_files.include('README.rdoc').
8
+ include('lib/**/*.rb').
9
+ exclude('lib/leaf/finders/sequel.rb').
10
+ exclude('lib/leaf/view_helpers/sinatra.rb').
11
+ exclude('lib/leaf/core_ext.rb').
12
+ exclude('lib/leaf/version.rb')
13
+
14
+ rdoc.main = "README.rdoc" # page to start on
15
+ rdoc.title = "leaf documentation"
16
+
17
+ rdoc.rdoc_dir = 'doc' # rdoc output folder
18
+ rdoc.options << '--inline-source' << '--charset=UTF-8' << '--format=darkfish'
19
+ rdoc.options << '--main=README.rdoc'
20
+ rdoc.options << '--webcvs=http://github.com/c7/spruce/tree/master/'
21
+ end
data/lib/forrst.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'spruce'
2
+
3
+ module Forrst
4
+ API_URI = "http://api.forrst.com/api/"
5
+ API_VERSION = 'v1'
6
+
7
+ ## Sections of the Forrst API
8
+ require 'forrst/user'
9
+
10
+ ## Exceptions
11
+ module Exceptions
12
+ class InvalidUserId < ArgumentError
13
+ def to_s
14
+ 'Invalid User ID, must be an Integer'
15
+ end
16
+ end
17
+
18
+ class InvalidPostId < ArgumentError
19
+ def to_s
20
+ 'Invalid Post ID, must be an Integer'
21
+ end
22
+ end
23
+
24
+ class InvalidUsername < ArgumentError
25
+ def to_s
26
+ 'Invalid username, must be a String'
27
+ end
28
+ end
29
+
30
+ class UserNotFound < ArgumentError
31
+ def to_s
32
+ 'User not found on Forrst'
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ require 'forrst/post'
2
+
3
+ module Forrst
4
+ class ListOfPosts < Array
5
+ attr_accessor :status, :raw_data
6
+
7
+ def add_post(post)
8
+ self << post
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,50 @@
1
+ require 'time'
2
+
3
+ module Forrst
4
+ class Post
5
+ attr_accessor :raw_data
6
+
7
+ def initialize(data)
8
+ populate(data)
9
+ end
10
+
11
+ def populate(data)
12
+ # Save the raw data hash
13
+ @raw_data = data
14
+
15
+ # Standard fields
16
+ standard_fields = [
17
+ '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',
20
+ 'in_reply_to_post_id', 'post_url', 'phrase', 'formatted_description',
21
+ 'tag_objs', 'tags', 'images', 'snap_content_type',
22
+ ]
23
+
24
+ # Numeric fields such as like count and number of views
25
+ numeric_fields = [
26
+ 'like_count', 'views', 'is_public', 'id', 'comment_count',
27
+ 'short_url_redirects', 'user_id', 'snap_file_size'
28
+ ]
29
+
30
+ # Date fields
31
+ date_fields = [
32
+ 'created_at', 'updated_at', 'snap_updated_at'
33
+ ]
34
+
35
+ standard_fields.map { |f| instance_variable_set("@#{f}", data[f]) }
36
+ numeric_fields.map { |f| instance_variable_set("@#{f}", data[f].to_i) }
37
+ date_fields.map { |f| instance_variable_set("@#{f}", f.match(/\d\d\d\d/) ? Time.parse(data[f]) : nil) }
38
+
39
+ # Define accessor methods
40
+ (class << self; self; end).class_eval do
41
+ # Handle the standard fields
42
+ (standard_fields + numeric_fields + date_fields).each do |field|
43
+ define_method "#{field}" do
44
+ instance_variable_get("@#{field}")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,53 @@
1
+ require 'uri'
2
+ require 'yajl'
3
+ require 'rest_client'
4
+
5
+ require 'forrst/user/public_posts'
6
+
7
+ module Forrst
8
+ class User
9
+ INFO_RESOURCE = "#{API_URI}#{API_VERSION}/users/info"
10
+
11
+ attr_reader :username, :id, :raw_data
12
+
13
+ def self.find(id)
14
+ (id.is_a? Fixnum) ? self.find_by_user_id(id) : self.find_by_username(id)
15
+ end
16
+
17
+ def initialize(uri)
18
+ begin
19
+ response = RestClient.get uri
20
+
21
+ if response.code == 200
22
+ @raw_data = Yajl::Parser.parse(response.body)['resp']['user']
23
+ @username = @raw_data['username']
24
+ @id = @raw_data['id'].to_i # Should be a Fixnum from the start
25
+ end
26
+
27
+ rescue RestClient::ResourceNotFound
28
+ raise Exceptions::UserNotFound
29
+ end
30
+ end
31
+
32
+ def public_posts
33
+ @posts ||= PublicPosts.new(username)
34
+ @posts.latest
35
+ end
36
+
37
+ def public_posts_before(post_id)
38
+ @posts ||= PublicPosts.new(username)
39
+ @posts.before(post_id)
40
+ end
41
+
42
+ private
43
+ def self.find_by_user_id(user_id)
44
+ raise Exceptions::InvalidId unless user_id.is_a? Fixnum
45
+ self.new("#{INFO_RESOURCE}?id=#{user_id}")
46
+ end
47
+
48
+ def self.find_by_username(username)
49
+ raise Exceptions::InvalidUsername unless username.is_a? String
50
+ self.new("#{INFO_RESOURCE}?username=#{username}")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,51 @@
1
+ require 'forrst/list_of_posts'
2
+
3
+ module Forrst
4
+ class User
5
+ class PublicPosts
6
+ RESOURCE = "#{API_URI}#{API_VERSION}/users/posts"
7
+
8
+ def latest
9
+ return @posts unless @posts.nil?
10
+ response = get("#{RESOURCE}?username=#{@username}")
11
+
12
+ @posts = ListOfPosts.new
13
+ response['resp']['posts'].map { |p|
14
+ @posts.add_post(Forrst::Post.new(p))
15
+ }
16
+ @posts.status = response['resp']['stat']
17
+ @posts.raw_data = response
18
+
19
+ @posts
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
+
36
+ def initialize(username)
37
+ raise Exceptions::InvalidUsername unless username.is_a? String
38
+ @username = username
39
+ end
40
+ private
41
+ def get(uri)
42
+ begin
43
+ response = RestClient.get uri
44
+ Yajl::Parser.parse(response.body) if response.code == 200
45
+ rescue RestClient::ResourceNotFound
46
+ raise Exceptions::UserNotFound
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
data/lib/spruce.rb ADDED
@@ -0,0 +1,9 @@
1
+ # = Spruce - a Ruby wrapper around the Forrst API
2
+ #
3
+ # Happy Forrsting!
4
+
5
+ require 'forrst'
6
+
7
+ module Spruce
8
+ require 'spruce/version'
9
+ end
@@ -0,0 +1,9 @@
1
+ module Spruce
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spruce
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
11
+ platform: ruby
12
+ authors:
13
+ - Peter Hellberg
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-26 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yajl-ruby
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 0
32
+ - 7
33
+ - 7
34
+ version: 0.7.7
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rest-client
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 1
48
+ - 6
49
+ - 0
50
+ version: 1.6.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ 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
+ email: peter@c7.se
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README.rdoc
61
+ - MIT-LICENSE
62
+ files:
63
+ - lib/forrst/list_of_posts.rb
64
+ - lib/forrst/post.rb
65
+ - lib/forrst/user/public_posts.rb
66
+ - lib/forrst/user.rb
67
+ - lib/forrst.rb
68
+ - lib/spruce/version.rb
69
+ - lib/spruce.rb
70
+ - MIT-LICENSE
71
+ - Rakefile
72
+ - README.rdoc
73
+ has_rdoc: true
74
+ homepage: http://c7.github.com/spruce
75
+ licenses:
76
+ - MIT-LICENSE
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --main
80
+ - README.rdoc
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project: spruce
105
+ rubygems_version: 1.3.7
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Spruce is a Ruby wrapper around the Forrst API
109
+ test_files: []
110
+