preposterous 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 @@
1
+ pkg/*
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Ray Hernandez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = preposterous
2
+
3
+ I'm hoping that I will stick to this and it will become a very functional Ruby based API for the Posterous(http://posterous.com) service.
4
+
5
+ = what works?
6
+
7
+ Right now it can get a list of posterous sites and post to posterous sites.
8
+ Media uploads are working for posting.
9
+
10
+ == examples
11
+
12
+ See the post.rb example in the examples directory.
13
+
14
+ http://github.com/hernan43/preposterous/tree/master/examples/post.rb
15
+
16
+ == copyright?
17
+
18
+ Since I've never really done anything like this, I am going to use John Nunemaker's(http://github.com/jnunemaker) Twitter gem(http://github.com/jnunemaker/twitter) design as an influence.
19
+
20
+ Also when I say "influence" I mean this is basically a really jacked up fork of the jnunemaker twitter gem.
21
+
22
+ Copyright (c) 2010 Ray Hernandez. See LICENSE for details.
23
+
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "preposterous"
8
+ gem.summary = %Q{a wrapper library for the Posterous API}
9
+ gem.description = %Q{This gem supports posting and listing sites on Posterous}
10
+ gem.email = "hernan43@gmail.com"
11
+ gem.homepage = "http://github.com/hernan43/preposterous"
12
+ gem.authors = ["Ray Hernandez"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 2
5
+ :build:
@@ -0,0 +1,5 @@
1
+ dont_git_me.rb
2
+ *.jpg
3
+ *.png
4
+ *.ppt
5
+ *.xls
data/examples/post.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'preposterous')
3
+
4
+ #
5
+ # creating a posterous object
6
+ #
7
+
8
+ # all requests need to be authenticated
9
+ # this creates your client object and sets up
10
+ # your posterous object for actions
11
+ client = Preposterous::HTTPAuth.new("my@email.addre.ss", "password")
12
+ posterous = Preposterous::Base.new(client)
13
+
14
+ #
15
+ # get and print a list of your posterous sites
16
+ #
17
+ posterous.getsites.each do |site|
18
+ puts "#{site['id']} #{site['url']}"
19
+ end
20
+
21
+ #
22
+ # standard post w/no files
23
+ #
24
+
25
+ # initialize the fields
26
+ # field names are here: http://posterous.com/api/posting
27
+ fields = {
28
+ :site_id => 99999999,
29
+ :title => "This is my post",
30
+ :body => "This is some boring text for my autogenerated post."
31
+ }
32
+
33
+ # perform the actual post
34
+ posterous.newpost(fields, *files)
35
+
36
+ #
37
+ # standard post w/files
38
+ #
39
+
40
+ # initialize array of files
41
+ files = [
42
+ File.open("file_one.png"),
43
+ File.open("file_two.png")
44
+ ]
45
+
46
+ # initialize fields
47
+ fields = {
48
+ :site_id => 1313830,
49
+ :title => "Check out these sweet pics",
50
+ :body => "I love these pics."
51
+ }
52
+
53
+ # perform the actual post
54
+ posterous.newpost(fields, *files)
@@ -0,0 +1,82 @@
1
+ module Preposterous
2
+ class Base
3
+ extend Forwardable
4
+ def_delegators :client, :get, :post
5
+
6
+ attr_reader :client
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ def getsites
13
+ response = perform_get("/api/getsites")
14
+ # parse out sites? not sure how I want the API to look
15
+ # for this. I have a couple of competing ideas...
16
+ response["site"] if not response.nil?
17
+ end
18
+
19
+ def newpost(fields={}, *files)
20
+ # create options hash
21
+ options = {:fields => fields}
22
+ options = build_multipart_bodies(*files).merge(options) if files
23
+
24
+ response = perform_post("/api/newpost", options)
25
+ # return post attrs
26
+ response["post"] if not response.nil?
27
+ end
28
+
29
+ # TODO: write readposts method
30
+ def readposts
31
+ end
32
+
33
+ # TODO: write update method
34
+ def updatepost
35
+ end
36
+
37
+ # TODO: write comment method
38
+ def newcomment
39
+ end
40
+
41
+ # TODO: write gettags method
42
+ def gettags
43
+ end
44
+
45
+ protected
46
+
47
+ CRLF = "\r\n"
48
+ def build_multipart_bodies(*files)
49
+ boundary = Time.now.to_i.to_s(16)
50
+ body = ""
51
+ files.each do |file|
52
+ esc_key = "media[]"
53
+ body << "--#{boundary}#{CRLF}"
54
+ if file.respond_to?(:read)
55
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(file.path)}\"#{CRLF}"
56
+ body << "Content-Type: #{MIME::Types.type_for(file.path)[0].to_s}#{CRLF*2}"
57
+ body << file.read
58
+ else
59
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{file}"
60
+ end
61
+ body << CRLF
62
+ end
63
+ body << "--#{boundary}--#{CRLF*2}"
64
+ {
65
+ :body => body,
66
+ :headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"}
67
+ }
68
+ end
69
+
70
+
71
+ private
72
+
73
+ def perform_get(path, options={})
74
+ Preposterous::Request.get(self, path, options)
75
+ end
76
+
77
+ def perform_post(path, options={})
78
+ Preposterous::Request.post(self, path, options)
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,32 @@
1
+ module Preposterous
2
+ class HTTPAuth
3
+ include HTTParty
4
+ format :xml
5
+
6
+ attr_reader :username, :password, :options
7
+
8
+ def initialize(username, password, options={})
9
+ @username, @password = username, password
10
+ @options = {:ssl => false}.merge(options)
11
+ # posterous' API URL is http://posterous.com/api
12
+ options[:api_endpoint] ||= "posterous.com"
13
+ self.class.base_uri "http#{'s' if options[:ssl]}://#{options[:api_endpoint]}"
14
+ end
15
+
16
+ def get(uri, headers={})
17
+ self.class.get(uri, :headers => headers, :basic_auth => basic_auth)
18
+ end
19
+
20
+ def post(uri, body={}, headers={})
21
+ self.class.post(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
22
+ end
23
+
24
+ private
25
+
26
+ def basic_auth
27
+ {:username => @username, :password => @password}
28
+ end
29
+
30
+ end
31
+ end
32
+
@@ -0,0 +1,54 @@
1
+ module Preposterous
2
+ class Request
3
+ extend Forwardable
4
+ def_delegators :client, :get, :post
5
+
6
+ attr_reader :client, :method, :path, :options
7
+
8
+ def self.get(client, path, options={})
9
+ new(client, :get, path, options).perform
10
+ end
11
+
12
+ def self.post(client, path, options={})
13
+ new(client, :post, path, options).perform
14
+ end
15
+
16
+ def initialize(client, method, path, options={})
17
+ @client, @method, @path, @options = client, method, path, options
18
+ end
19
+
20
+ def uri
21
+ @uri ||= begin
22
+ uri = URI.parse(path)
23
+ if options[:fields] && options[:fields] != {}
24
+ uri.query = to_query(options[:fields])
25
+ end
26
+
27
+ uri.to_s
28
+ end
29
+ end
30
+
31
+ def perform
32
+ Preposterous.make_friendly(send("perform_#{method}"))
33
+ end
34
+
35
+ private
36
+
37
+ def perform_get
38
+ get(uri, options[:headers])
39
+ end
40
+
41
+ def perform_post
42
+ post(uri, options[:body], options[:headers])
43
+ end
44
+
45
+ def to_query(options)
46
+ options.inject([]) do |collection, opt|
47
+ collection << "#{CGI.escape opt[0].to_s}=#{CGI.escape opt[1].to_s}"
48
+ collection
49
+ end * '&'
50
+ end
51
+
52
+ end
53
+ end
54
+
@@ -0,0 +1,20 @@
1
+ require 'httparty'
2
+ require 'forwardable'
3
+ require 'cgi'
4
+ require 'mime/types'
5
+
6
+ module Preposterous
7
+
8
+ # TODO: Add exceptions based on errors listed at: http://posterous.com/api/posting
9
+
10
+ def self.make_friendly(response)
11
+ Crack::XML.parse(response.body)["rsp"] if not response.nil?
12
+ end
13
+
14
+ end
15
+
16
+ # bring in all of the files in the lib dir
17
+ directory = File.expand_path(File.dirname(__FILE__))
18
+ require File.join(directory, "preposterous", "base")
19
+ require File.join(directory, "preposterous", "httpauth")
20
+ require File.join(directory, "preposterous", "request")
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{preposterous}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ray Hernandez"]
12
+ s.date = %q{2010-05-07}
13
+ s.description = %q{This gem supports posting and listing sites on Posterous}
14
+ s.email = %q{hernan43@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION.yml",
25
+ "examples/.gitignore",
26
+ "examples/post.rb",
27
+ "lib/preposterous.rb",
28
+ "lib/preposterous/base.rb",
29
+ "lib/preposterous/httpauth.rb",
30
+ "lib/preposterous/request.rb",
31
+ "preposterous.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/hernan43/preposterous}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.6}
37
+ s.summary = %q{a wrapper library for the Posterous API}
38
+ s.test_files = [
39
+ "examples/post.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: preposterous
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Ray Hernandez
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-07 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: This gem supports posting and listing sites on Posterous
22
+ email: hernan43@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .gitignore
32
+ - LICENSE
33
+ - README.rdoc
34
+ - Rakefile
35
+ - VERSION.yml
36
+ - examples/.gitignore
37
+ - examples/post.rb
38
+ - lib/preposterous.rb
39
+ - lib/preposterous/base.rb
40
+ - lib/preposterous/httpauth.rb
41
+ - lib/preposterous/request.rb
42
+ - preposterous.gemspec
43
+ has_rdoc: true
44
+ homepage: http://github.com/hernan43/preposterous
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.6
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: a wrapper library for the Posterous API
73
+ test_files:
74
+ - examples/post.rb