jamescallmebrent-rumblr 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ *.gem
data/LICENSE.rdoc CHANGED
@@ -22,4 +22,3 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
22
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
23
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
24
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
-
data/README.rdoc CHANGED
@@ -4,14 +4,10 @@ Rumblr is a Ruby client for http://www.tumblr.com XML API. It currently only pr
4
4
 
5
5
  = Overview
6
6
 
7
- Rumblr maps the Tumblr API 'authenticate' and 'read' calls. User, Tumblelog and Post models have attributes provided by the API, as well as some convenience methods for common tasks.
7
+ Rumblr maps the Tumblr API 'authenticate' and 'read' calls. User, Tumblelog and Post models have the attributes provided by the API, as well as some convenience methods for common tasks.
8
8
 
9
- Please read Tumblr's official documentation (http://tumblr.com/api/) to learn more the API.
9
+ Please read Tumblr's official documentation (http://tumblr.com/api/) to learn more about the API.
10
10
 
11
- == Dependencies
12
-
13
- * Ruby >= 1.8.6 (not tested with previous versions)
14
- * libxml-ruby >= 0.8.3
15
11
 
16
12
  == Dependencies
17
13
 
@@ -32,12 +28,13 @@ To get the latest version, simply type the following instruction into your comma
32
28
  In order to use this library you need a valid Tumblr account.
33
29
  Go to http://tumblr.com and register for a new account if you don't already have one.
34
30
 
31
+
35
32
  === Sample usage:
36
33
 
37
34
  require 'rumblr'
38
35
  include Rumblr
39
36
 
40
- user = User.login({:email => 'brent.hargrave@gmail.com', :password => 'tumb198$'})
37
+ user = User.login({:email => 'name@domain.com', :password => 'pass'})
41
38
 
42
39
  user.tumblelogs.each do |log|
43
40
  puts log.name # 'the tumblelogger'
@@ -53,22 +50,8 @@ Go to http://tumblr.com and register for a new account if you don't already have
53
50
  puts post.url # 'http://tumblelogger.tumblr.com/post/12345/my-first-tumblr-post'
54
51
  end
55
52
 
56
-
57
- == Credits
58
-
59
- Author:: {Brent Hargrave}[http://brenthargrave.info/] <brent.hargrave@gmail.com>
60
-
61
- This library was inspired by some great Ruby clients for other webservices: {WWW::Delicious}[http://github.com/weppos/www-delicious/tree/master] and {dropio_api_ruby}[http://github.com/weppos/www-delicious/tree/master]
62
-
63
-
64
- == Resources
65
-
66
- * {GitHub}[http://github.com/jamescallmebrent/rumblr/]
67
-
68
-
69
- == FeedBack and Bug reports
70
-
71
- Please email {Brent Hargrave}[mailto:brent.hargrave@gmail.com] with any questions or feedback.
53
+ post = RegularPost.new(:title => "Post Title", :body => "Just a regular post!")
54
+ Client.instance.write(post, {:email => 'name@domain.com', :password => 'pass'})
72
55
 
73
56
 
74
57
  == License
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |s|
6
+ summary = %Q{Ruby client for the Tumblr API}
7
+
8
+ s.name = "rumblr"
9
+ s.summary = summary
10
+ s.email = "brent.hargrave@gmail.com"
11
+ s.homepage = "http://github.com/jamescallmebrent/rumblr"
12
+ s.description = summary
13
+ s.authors = ["Brent Hargrave", "Benny Wong"]
14
+ s.add_dependency('libxml-ruby', '>= 0.8.3')
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ require 'rake/rdoctask'
21
+ Rake::RDocTask.new do |rdoc|
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = 'rumblr'
24
+ rdoc.options << '--line-numbers' << '--inline-source'
25
+ rdoc.rdoc_files.include('README*')
26
+ rdoc.rdoc_files.include('lib/**/*.rb')
27
+ end
28
+
29
+ require 'spec/rake/spectask'
30
+ Spec::Rake::SpecTask.new(:spec) do |t|
31
+ t.libs << 'lib' << 'spec'
32
+ t.spec_files = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ Spec::Rake::SpecTask.new(:rcov) do |t|
36
+ t.libs << 'lib' << 'spec'
37
+ t.spec_files = FileList['spec/**/*_spec.rb']
38
+ t.rcov = true
39
+ end
40
+
41
+ begin
42
+ require 'cucumber/rake/task'
43
+ Cucumber::Rake::Task.new(:features)
44
+ rescue LoadError
45
+ puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
46
+ end
47
+
48
+ task :default => :spec
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 0
4
- :patch: 1
4
+ :patch: 2
data/lib/rumblr/client.rb CHANGED
@@ -21,7 +21,9 @@ module Rumblr
21
21
  user_attributes = parse_user_attributes_from(response_body)
22
22
  user_attributes.merge!(user_credentials)
23
23
  end
24
- User.new(user_attributes)
24
+ user = User.new(user_attributes)
25
+ user.tumblelogs.each{ |tumblelog| tumblelog.user = user }
26
+ user
25
27
  end
26
28
 
27
29
  def read(options={})
@@ -35,8 +37,13 @@ module Rumblr
35
37
 
36
38
  request_url_host = URI.parse(options[:url]).host
37
39
  complete_request(request,host=request_url_host) do |response_body|
38
- parser, parser.string = XML::Parser.new, response_body
40
+ parser = XML::Parser.string(response_body)
39
41
  doc = parser.parse
42
+ # parse and map tumblelog
43
+ tumblelog_element = doc.find_first('//tumblr/tumblelog')
44
+ tumblelog_attrs = cleanup_hash(tumblelog_element.attributes.to_h)
45
+ tumblelog_attrs.merge!(:url => options[:url])
46
+ tumblelog = Tumblelog.new(tumblelog_attrs)
40
47
  # parse and map posts
41
48
  posts = doc.find('//tumblr/posts/post').inject([]) do |array, element|
42
49
  post_attrs = cleanup_hash(element.attributes.to_h)
@@ -52,29 +59,27 @@ module Rumblr
52
59
  inner_attrs.delete(:tag)
53
60
  post_attrs.merge!(inner_attrs)
54
61
  # turn attributes into proper model
55
- klass = case post_attrs[:type]
56
- when 'regular' then RegularPost
57
- when 'photo' then PhotoPost
58
- when 'quote' then QuotePost
59
- when 'link' then LinkPost
60
- when 'conversation' then ConversationPost
61
- when 'video' then VideoPost
62
- when 'audio' then AudioPost
63
- else raise 'unknown post type'
64
- end
62
+ klass = Rumblr.const_get(Post::TYPES[post_attrs[:type]])
63
+ raise 'unknown post type' unless klass
65
64
  post = klass.new(post_attrs)
65
+ post.tumblelog = tumblelog
66
66
  array << post
67
67
  array
68
68
  end
69
- # parse and map tumblelog
70
- tumblelog_element = doc.find_first('//tumblr/tumblelog')
71
- tumblelog_attrs = cleanup_hash(tumblelog_element.attributes.to_h)
72
- tumblelog_attrs.merge!(:url => options[:url])
73
- tumblelog = Tumblelog.new(tumblelog_attrs)
74
69
  end
75
70
  return tumblelog, posts
76
71
  end
77
72
 
73
+ def write(post, creds)
74
+ raise(ArgumentError) unless post && post.is_a?(Post)
75
+ raise(ArgumentError) unless creds
76
+ uri = URI::HTTP.build(:path => "/api/write")
77
+ request = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
78
+ request.set_form_data(creds.merge(:type => Post::TYPES.invert[post.class]).merge(post.attribute_hash))
79
+ request_url_host = URI.parse(uri.request_uri).host
80
+ complete_request(request)
81
+ end
82
+
78
83
 
79
84
  protected
80
85
 
@@ -111,7 +116,7 @@ module Rumblr
111
116
  private
112
117
 
113
118
  def parse_user_attributes_from(response_body)
114
- parser, parser.string = XML::Parser.new, response_body
119
+ parser = XML::Parser.string response_body
115
120
  doc = parser.parse
116
121
  tumblelogs = doc.find('//tumblr/tumblelog').inject([]) do |array, element|
117
122
  tumblelog_attrs = cleanup_hash(element.attributes.to_h)
@@ -138,4 +143,4 @@ module Rumblr
138
143
 
139
144
  end
140
145
 
141
- end
146
+ end
data/lib/rumblr/post.rb CHANGED
@@ -3,7 +3,19 @@ module Rumblr
3
3
  # for attribute details, see Tumblr's documentation:
4
4
  # http://www.tumblr.com/api
5
5
  class Post < Resource
6
+
7
+ TYPES = {
8
+ 'regular' => "RegularPost",
9
+ 'photo' => "PhotoPost",
10
+ 'quote' => "QuotePost",
11
+ 'link' => "LinkPost",
12
+ 'conversation' => "ConversationPost",
13
+ 'video' => "VideoPost",
14
+ 'audio' => "AudioPost"
15
+ }
16
+
6
17
  attr_reader :id, :url, :type, :unix_timestamp, :date_gmt, :date, :tags, :private
18
+ attr_accessor :tumblelog
7
19
 
8
20
  def initialize(attrs={})
9
21
  @private = false
@@ -18,34 +30,73 @@ module Rumblr
18
30
  !self.private?
19
31
  end
20
32
 
33
+ def attribute_hash
34
+ {:date => date}
35
+ end
36
+
21
37
  end
22
38
 
23
39
  class RegularPost < Post
24
- attr_reader :title, :body
40
+ attr_accessor :title, :body
41
+
42
+ def attribute_hash
43
+ super.merge(:title => title, :body => body)
44
+ end
45
+
25
46
  end
26
47
 
27
48
  class PhotoPost < Post
28
- attr_reader :source, :data, :caption, :click_through_url
49
+ attr_accessor :source, :data, :caption, :click_through_url
50
+
51
+ def attribute_hash
52
+ super.merge(:source => source, :data => data, :caption => caption, :click_through_url => click_through_url)
53
+ end
54
+
29
55
  end
30
56
 
31
57
  class QuotePost < Post
32
- attr_reader :quote, :source
58
+ attr_accessor :quote, :source
59
+
60
+ def attribute_hash
61
+ super.merge(:quote => quote, :source => source)
62
+ end
63
+
33
64
  end
34
65
 
35
66
  class LinkPost < Post
36
- attr_reader :name, :url, :description
67
+ attr_accessor :name, :url, :description
68
+
69
+ def attribute_hash
70
+ super.merge(:name => name, :url => url, :description => description)
71
+ end
72
+
37
73
  end
38
74
 
39
75
  class ConversationPost < Post
40
- attr_reader :title, :conversation
76
+ attr_accessor :title, :conversation
77
+
78
+ def attribute_hash
79
+ super.merge(:title => title, :conversation => conversation)
80
+ end
81
+
41
82
  end
42
83
 
43
84
  class VideoPost < Post
44
- attr_reader :embed, :data, :title, :caption
85
+ attr_accessor :embed, :data, :title, :caption
86
+
87
+ def attribute_hash
88
+ super.merge(:embed => embed, :data => data, :title => title, :caption => caption)
89
+ end
90
+
45
91
  end
46
92
 
47
93
  class AudioPost < Post
48
- attr_reader :data, :caption
94
+ attr_accessor :data, :caption
95
+
96
+ def attribute_hash
97
+ super.merge(:data => data, :caption => caption)
98
+ end
99
+
49
100
  end
50
101
 
51
- end
102
+ end
@@ -3,12 +3,17 @@ module Rumblr
3
3
  class Tumblelog < Resource
4
4
  attr_reader :name, :timezone, :cname, :title, :url, :avatar_url, :is_primary,
5
5
  :type, :private_id
6
+ attr_accessor :user
6
7
 
7
8
  def posts
8
9
  return [] unless self.url
9
10
  log, posts = Client.instance.read(:url => self.url)
10
11
  return posts
11
12
  end
13
+
14
+ def primary?
15
+ is_primary == "yes"
16
+ end
12
17
 
13
18
  class << self
14
19
 
@@ -21,4 +26,4 @@ module Rumblr
21
26
 
22
27
  end
23
28
 
24
- end
29
+ end
data/lib/rumblr/user.rb CHANGED
@@ -8,7 +8,7 @@ module Rumblr
8
8
  end
9
9
 
10
10
  def primary_tumblelog
11
- self.tumblelogs.find { |log| log.is_primary }
11
+ self.tumblelogs.find { |log| log.primary? }
12
12
  end
13
13
 
14
14
  class << self
@@ -20,4 +20,4 @@ module Rumblr
20
20
 
21
21
  end
22
22
 
23
- end
23
+ end
data/rumblr.gemspec ADDED
@@ -0,0 +1,60 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rumblr}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Brent Hargrave", "Benny Wong"]
9
+ s.date = %q{2009-05-30}
10
+ s.description = %q{Ruby client for the Tumblr API}
11
+ s.email = %q{brent.hargrave@gmail.com}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE.rdoc",
14
+ "README.rdoc"
15
+ ]
16
+ s.files = [
17
+ ".gitignore",
18
+ "LICENSE.rdoc",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "lib/rumblr.rb",
23
+ "lib/rumblr/client.rb",
24
+ "lib/rumblr/post.rb",
25
+ "lib/rumblr/resource.rb",
26
+ "lib/rumblr/tumblelog.rb",
27
+ "lib/rumblr/user.rb",
28
+ "rumblr.gemspec",
29
+ "spec/rumblr/client_spec.rb",
30
+ "spec/rumblr/post_spec.rb",
31
+ "spec/rumblr/tumblelog_spec.rb",
32
+ "spec/rumblr/user_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/jamescallmebrent/rumblr}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.3}
39
+ s.summary = %q{Ruby client for the Tumblr API}
40
+ s.test_files = [
41
+ "spec/rumblr/client_spec.rb",
42
+ "spec/rumblr/post_spec.rb",
43
+ "spec/rumblr/tumblelog_spec.rb",
44
+ "spec/rumblr/user_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<libxml-ruby>, [">= 0.8.3"])
54
+ else
55
+ s.add_dependency(%q<libxml-ruby>, [">= 0.8.3"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<libxml-ruby>, [">= 0.8.3"])
59
+ end
60
+ end
@@ -108,4 +108,38 @@ module Rumblr
108
108
 
109
109
  end
110
110
 
111
+ describe Client, "writing to a public tumblelog" do
112
+
113
+ before(:each) do
114
+ mock_successful(:authenticated_read)
115
+ @tumblelog, posts = @client.read( :url => 'http://dummylog.tumblr.com/',
116
+ :email => 'valid',
117
+ :password => 'valid' )
118
+ mock_successful(:authenticated_write)
119
+ end
120
+
121
+ it 'should raise an ArgumentError without post' do
122
+ lambda do
123
+ @client.write(nil, {:email => 'valid', :password => 'valid'})
124
+ end.should raise_error(ArgumentError)
125
+ end
126
+
127
+ it 'should raise an ArgumentError if post is incorrect class' do
128
+ lambda do
129
+ @client.write("", {:email => 'valid', :password => 'valid'})
130
+ end.should raise_error(ArgumentError)
131
+ end
132
+
133
+ it 'should raise an ArgumentError without creds' do
134
+ lambda do
135
+ @client.write(Rumblr::Post.new, nil)
136
+ end.should raise_error(ArgumentError)
137
+ end
138
+
139
+ it 'should return an id' do
140
+ @client.write(Rumblr::Post.new, {:email => 'valid', :password => 'valid'}).should == "10001"
141
+ end
142
+
143
+ end
144
+
111
145
  end
@@ -5,7 +5,7 @@ module Rumblr
5
5
  describe Post, 'instances' do
6
6
 
7
7
  it 'should have attributes shared by every Post' do
8
- Post.new.should respond_to(:id, :url, :type, :unix_timestamp, :date_gmt, :date, :tags, :private)
8
+ Post.new.should respond_to(:id, :url, :type, :unix_timestamp, :date_gmt, :date, :tags, :private, :tumblelog)
9
9
  end
10
10
 
11
11
  it 'private attribute should default to false' do
@@ -20,6 +20,10 @@ module Rumblr
20
20
  Post.new.should respond_to(:public?)
21
21
  end
22
22
 
23
+ it 'should provide a hash of attributes and values for API' do
24
+ Post.new.should respond_to(:attribute_hash)
25
+ end
26
+
23
27
  end
24
28
 
25
29
  end
@@ -35,5 +35,30 @@ module Rumblr
35
35
  end
36
36
 
37
37
  end
38
-
38
+
39
+ describe Tumblelog, 'user' do
40
+
41
+ describe 'logged in' do
42
+
43
+ before(:each) do
44
+ mock_successful(:authenticate)
45
+ @user = User.login(:email => 'valid_email', :password => 'valid_password')
46
+ end
47
+
48
+ it 'should have a user associated with it' do
49
+ @user.primary_tumblelog.user.should_not be_nil
50
+ end
51
+
52
+ end
53
+
54
+ describe 'not logged in' do
55
+
56
+ it 'should not have a user if anonymous read' do
57
+ mock_successful(:anonymous_read)
58
+ Tumblelog.find_by_url('valid_url').user.should be_nil
59
+ end
60
+
61
+ end
62
+ end
63
+
39
64
  end
@@ -61,7 +61,7 @@ module Rumblr
61
61
 
62
62
  it 'should provide a primary tumblelog' do
63
63
  primary = @user.primary_tumblelog
64
- primary.is_primary.should be_true
64
+ primary.is_primary.should == 'yes'
65
65
  end
66
66
 
67
67
  end
data/spec/spec_helper.rb CHANGED
@@ -12,8 +12,12 @@ def mock_successful(request)
12
12
  }
13
13
 
14
14
  @client = Rumblr::Client.instance
15
- # yes, this is cheating, but for now just concerned that xml is parsed right
16
- @client.stub!(:complete_request).and_yield(responses_to[request])
15
+ if request == :authenticated_write
16
+ @client.stub!(:complete_request).and_return(responses_to[request])
17
+ else
18
+ # yes, this is cheating, but for now just concerned that xml is parsed right
19
+ @client.stub!(:complete_request).and_yield(responses_to[request])
20
+ end
17
21
  end
18
22
 
19
23
  def successful_authenticate_xml
@@ -98,8 +102,5 @@ def successful_authenticated_read_xml
98
102
  end
99
103
 
100
104
  def successful_authenticated_write_xml
101
- response = <<-EOF
102
- <?xml version="1.0" encoding="UTF-8"?>
103
- EOF
104
- response.strip!
105
+ "10001"
105
106
  end
metadata CHANGED
@@ -1,48 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jamescallmebrent-rumblr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brent Hargrave
8
+ - Benny Wong
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2009-02-17 00:00:00 -08:00
13
+ date: 2009-05-30 00:00:00 -07:00
13
14
  default_executable:
14
- dependencies: []
15
-
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: libxml-ruby
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.8.3
25
+ version:
16
26
  description: Ruby client for the Tumblr API
17
27
  email: brent.hargrave@gmail.com
18
28
  executables: []
19
29
 
20
30
  extensions: []
21
31
 
22
- extra_rdoc_files: []
23
-
32
+ extra_rdoc_files:
33
+ - LICENSE.rdoc
34
+ - README.rdoc
24
35
  files:
36
+ - .gitignore
25
37
  - LICENSE.rdoc
26
38
  - README.rdoc
39
+ - Rakefile
27
40
  - VERSION.yml
28
- - lib/rumblr
41
+ - lib/rumblr.rb
29
42
  - lib/rumblr/client.rb
30
43
  - lib/rumblr/post.rb
31
44
  - lib/rumblr/resource.rb
32
45
  - lib/rumblr/tumblelog.rb
33
46
  - lib/rumblr/user.rb
34
- - lib/rumblr.rb
35
- - spec/rumblr
47
+ - rumblr.gemspec
36
48
  - spec/rumblr/client_spec.rb
37
49
  - spec/rumblr/post_spec.rb
38
50
  - spec/rumblr/tumblelog_spec.rb
39
51
  - spec/rumblr/user_spec.rb
40
52
  - spec/spec_helper.rb
41
- has_rdoc: true
53
+ has_rdoc: false
42
54
  homepage: http://github.com/jamescallmebrent/rumblr
43
55
  post_install_message:
44
56
  rdoc_options:
45
- - --inline-source
46
57
  - --charset=UTF-8
47
58
  require_paths:
48
59
  - lib
@@ -63,7 +74,11 @@ requirements: []
63
74
  rubyforge_project:
64
75
  rubygems_version: 1.2.0
65
76
  signing_key:
66
- specification_version: 2
77
+ specification_version: 3
67
78
  summary: Ruby client for the Tumblr API
68
- test_files: []
69
-
79
+ test_files:
80
+ - spec/rumblr/client_spec.rb
81
+ - spec/rumblr/post_spec.rb
82
+ - spec/rumblr/tumblelog_spec.rb
83
+ - spec/rumblr/user_spec.rb
84
+ - spec/spec_helper.rb