preposterous 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 0
4
- :patch: 5
3
+ :minor: 1
4
+ :patch: 0
5
5
  :build:
@@ -78,3 +78,23 @@ posterous.updatepost(fields, *files)
78
78
  #
79
79
 
80
80
  puts posterous.gettags
81
+
82
+ #
83
+ # using the object style of posting
84
+ #
85
+ Preposterous::Base.establish_client(
86
+ :client_type => Preposterous::HTTPAuth,
87
+ :username => 'fake@userna.me',
88
+ :password => 'password'
89
+ )
90
+
91
+ p = Preposterous::Post.new
92
+
93
+ p.site_id= 1234567
94
+ p.title = "Test from object poster"
95
+ p.body = "This is a test"
96
+ p.autopost = 0
97
+ p.media << File.open("examples/one.png")
98
+ p.media << File.open("examples/two.png")
99
+ # save posts the... uh... post
100
+ p.save
@@ -59,3 +59,5 @@ directory = File.expand_path(File.dirname(__FILE__))
59
59
  require File.join(directory, "preposterous", "base")
60
60
  require File.join(directory, "preposterous", "httpauth")
61
61
  require File.join(directory, "preposterous", "request")
62
+ require File.join(directory, "preposterous", "connection")
63
+ require File.join(directory, "preposterous", "post")
@@ -19,6 +19,7 @@ module Preposterous
19
19
  def newpost(fields={}, *files)
20
20
  # create options hash
21
21
  options = generate_post_options(fields, *files)
22
+ puts options
22
23
  response = perform_post("/api/newpost", options)
23
24
  # return post attrs
24
25
  response["post"] if not response.nil?
@@ -34,15 +35,12 @@ module Preposterous
34
35
 
35
36
  # this is BROKEN
36
37
  # for some reason the XML will not parse
37
- # the CDATA fields are throwing off the parser
38
+ # the CDATA fields are throwing off the parser(REXML)
38
39
  def readposts(fields={})
39
40
  response = perform_get("/api/readposts", :fields => fields)
40
41
  response["post"] if not response.nil?
41
42
  end
42
43
 
43
- # for some reason posterous does not recognize the post_id
44
- # i think there maybe something wrong with their API
45
- # at least I hope it isn't me
46
44
  def newcomment(fields={})
47
45
  response = perform_post("/api/newcomment", :fields => fields)
48
46
  response["comment"] if not response.nil?
@@ -57,12 +55,15 @@ module Preposterous
57
55
 
58
56
  def generate_post_options(fields, *files)
59
57
  # create options hash
60
- options = {:fields => fields}
61
- options = build_multipart_bodies(*files).merge(options) if files
58
+ options = {:headers => {}, :body => "", :fields => fields}
59
+ options = options.merge(build_multipart_bodies(*files))
62
60
  end
63
61
 
64
62
  CRLF = "\r\n"
65
63
  def build_multipart_bodies(*files)
64
+ # bug out early if there are no files
65
+ return {} if files.length < 1
66
+
66
67
  boundary = Time.now.to_i.to_s(16)
67
68
  body = ""
68
69
  files.each do |file|
@@ -0,0 +1,19 @@
1
+ module Preposterous
2
+ class Base
3
+ class ClientTypeNotSpecified < StandardError; end
4
+
5
+ # the following code is to allow for a
6
+ # active_record-y access to Posterous
7
+ @@client = nil
8
+
9
+ def self.client
10
+ @@client
11
+ end
12
+
13
+ def self.establish_client(config={})
14
+ unless config.key?(:client_type) then raise ClientTypeNotSpecified, "You must specify an adapter type" end
15
+ client = config[:client_type].initialize_from_hash(config)
16
+ @@client = new(client)
17
+ end
18
+ end
19
+ end
@@ -5,6 +5,8 @@ module Preposterous
5
5
 
6
6
  attr_reader :username, :password, :options
7
7
 
8
+ class CredentialsError < StandardError; end
9
+
8
10
  def initialize(username, password, options={})
9
11
  @username, @password = username, password
10
12
  @options = {:ssl => false}.merge(options)
@@ -13,6 +15,11 @@ module Preposterous
13
15
  self.class.base_uri "http#{'s' if options[:ssl]}://#{options[:api_endpoint]}"
14
16
  end
15
17
 
18
+ def self.initialize_from_hash(options={})
19
+ unless options.key?(:username) and options.key?(:password) then raise CredentialsError, "You must specify both username and password" end
20
+ new(options[:username], options[:password], options)
21
+ end
22
+
16
23
  def get(uri, headers={})
17
24
  self.class.get(uri, :headers => headers, :basic_auth => basic_auth)
18
25
  end
@@ -0,0 +1,34 @@
1
+ module Preposterous
2
+ class Post < Preposterous::Base
3
+ attr_reader :client, :attributes
4
+ attr_accessor :site_id, :post_id, :title, :body, :autopost, :private, :date, :tags, :source, :sourceLink, :media
5
+
6
+ def initialize(attributes={})
7
+ @media = []
8
+ end
9
+
10
+ def attributes
11
+ attrs = Hash[self.instance_variables.collect do |var|
12
+ key = var.to_s.gsub(/@/, '').to_sym
13
+ [key,instance_variable_get(var)]
14
+ end
15
+ ]
16
+ # got to get rid of this one to keep Posterous from borking
17
+ attrs.delete(:media)
18
+ attrs
19
+ end
20
+
21
+ def is_new?
22
+ post_id.nil?
23
+ end
24
+
25
+ def save
26
+ if self.is_new?
27
+ Preposterous::Base.client.newpost(attributes, *@media)
28
+ else
29
+ Preposterous::Base.client.updatepost(attributes, *@media)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -20,7 +20,7 @@ module Preposterous
20
20
  def uri
21
21
  @uri ||= begin
22
22
  uri = URI.parse(path)
23
- if options[:fields] && options[:fields] != {}
23
+ if not options.nil? and options.key?(:fields) and options[:fields] != {}
24
24
  uri.query = to_query(options[:fields])
25
25
  end
26
26
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{preposterous}
8
- s.version = "0.0.5"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ray Hernandez"]
@@ -26,7 +26,9 @@ Gem::Specification.new do |s|
26
26
  "examples/post.rb",
27
27
  "lib/preposterous.rb",
28
28
  "lib/preposterous/base.rb",
29
+ "lib/preposterous/connection.rb",
29
30
  "lib/preposterous/httpauth.rb",
31
+ "lib/preposterous/post.rb",
30
32
  "lib/preposterous/request.rb",
31
33
  "preposterous.gemspec"
32
34
  ]
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 5
9
- version: 0.0.5
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ray Hernandez
@@ -37,7 +37,9 @@ files:
37
37
  - examples/post.rb
38
38
  - lib/preposterous.rb
39
39
  - lib/preposterous/base.rb
40
+ - lib/preposterous/connection.rb
40
41
  - lib/preposterous/httpauth.rb
42
+ - lib/preposterous/post.rb
41
43
  - lib/preposterous/request.rb
42
44
  - preposterous.gemspec
43
45
  has_rdoc: true