require 'rexml/document'
require 'uri'
require 'net/http'
+require 'net/https'
require 'ldblogwriter/wsse.rb'
require 'ldblogwriter/atom_response.rb'
class AtomPubClient
attr_accessor :username, :password, :authtype
+ GOOGLE_LOGIN_URL = 'https://www.google.com/accounts/ClientLogin'
+
def initialize(username, password, authtype=nil)
@username = username
@password = password
puts res.body
return false
end
- return AtomResponse.new(res.body).collection_uri
+ return AtomResponse.new(res.body)
end
end
def auth_basic(username, password)
end
+ def auth_google(username, password)
+ return {'Authorization' => 'GoogleLogin auth='+
+ get_google_auth_token(username, password)}
+ end
+
def auth_method(type)
"auth_#{type.to_s.downcase}".intern
end
return `file -bi #{filename}`.chomp
end
end
- end
-end
-
-
-if $0 == __FILE__
- $test = true
-end
-
-if defined?($test) && $test
- require 'test/unit'
- require 'config.rb'
- require 'pp'
- class TestAtomPub < Test::Unit::TestCase
- def setup
- config_file = ENV['HOME'] + "/.ldblogwriter.conf"
- @conf = LDBlogWriter::Config.new(config_file)
- @atom = LDBlogWriter::AtomPubClient::new(@conf.username, @conf.password, :wsse)
- end
+ private
- def test_create_media
-# @atom.create_media(@conf.atom_pub_uri+"blog/" + @conf.username+"/image",
-# "../../test/test.jpg", "test_image.jpg")
+ def get_google_auth_token(username, password)
+ url = URI.parse(GOOGLE_LOGIN_URL)
+ req = Net::HTTP::Post.new(url.path)
+
+ req.form_data = {'Email' => username,
+ 'Passwd' => password,
+ 'service' => 'blogger',
+ 'accountType' => 'GOOGLE',
+ 'source' => "hoge-lbw-1"}
+ https = Net::HTTP.new(url.host, url.port)
+ https.use_ssl = true
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
+ store = OpenSSL::X509::Store.new
+ store.set_default_paths
+ https.cert_store = store
+ https.start do
+ res = https.request(req)
+ if res.body =~ /Auth=(.+)/
+ return $1
+ else
+ puts res.body
+ end
+ end
+ return nil
end
- def test_get_resource_uri
- uri_a = @atom.get_resource_uri(@conf.atom_pub_uri)
- end
end
end
+
--- /dev/null
+# -*- coding: utf-8 -*-
+require 'uri'
+require 'net/http'
+require 'cgi'
+
+require 'ldblogwriter/atompub.rb'
+require 'ldblogwriter/service.rb'
+
+module LDBlogWriter
+ class Blogger < AbstractService
+
+
+ def initialize(config)
+ super
+ @atom_client = LDBlogWriter::AtomPubClient.new(config.username, config.password, :google)
+# get_resource_uri(@atom_client, @config.atom_pub_uri)
+ @entry_uri = config.post_uri
+ end
+
+ def post_entry(content, title, category = nil)
+ return @atom_client.create_entry(@entry_uri,
+ to_xml(content, title, category))
+ end
+
+ def edit_entry
+ end
+
+ def delete_entry
+ end
+
+ def post_image(image_file_path, image_title = nil)
+ @atom_client.create_media(@image_uri, image_file_path)
+ end
+
+ def to_xml(content, title, category = nil)
+ data = "<entry xmlns='http://www.w3.org/2005/Atom'>\n"
+ data += "<title type='text'>#{title}</title>\n"
+ data += "<content type='xhtml'>\n"
+ data += "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+ data += content
+ data += "</div>\n"
+ data += "</content>\n"
+ data += "<author>\n"
+# data += "<name>#{@conf.username}</name>\n"
+# data += "<email>#{@conf.username}</email>\n"
+ data += "</author>\n"
+ data += "</entry>\n"
+ return data
+ end
+
+ private
+
+ def get_resource_uri(atom_client, atom_uri)
+ res_a = atom_client.get_resource_uri(atom_uri)
+# @entry_uri = res_a[0]
+# @image_uri = res_a[1]
+ end
+ end
+end
+
+
require 'ldblogwriter/atompub.rb'
require 'ldblogwriter/service.rb'
+require 'ldblogwriter/atom_response.rb'
module LDBlogWriter
class LiveDoor < AbstractService
private
def get_resource_uri(atom_client, atom_uri)
- res_a = atom_client.get_resource_uri(atom_uri)
- @entry_uri = res_a[0]
- @image_uri = res_a[1]
+ res = atom_client.get_resource_uri(atom_uri)
+ @entry_uri = res.collection_uri[0]
+ @image_uri = res.collection_uri[1]
end
end
--- /dev/null
+$LOAD_PATH.unshift 'lib'
+
+require 'test/unit'
+require 'mocha'
+require 'ldblogwriter/services/blogger.rb'
+require 'ldblogwriter/config.rb'
+
+
+class TestLiveDoor < Test::Unit::TestCase
+ def setup
+ config_file = ENV['HOME'] + "/.ldblogwriter.conf"
+ conf = LDBlogWriter::Config.new(config_file)
+ @ld = LDBlogWriter::Blogger::new(conf)
+ end
+
+ def test_to_xml
+ assert(@ld.to_xml("test content", "test title", "test"))
+ end
+
+ def test_post_entry
+ post_ret = Net::HTTPResponse.new("1.1", "201", "hoge")
+ post_ret['Location'] = "huga"
+ Net::HTTP.any_instance.stubs(:post).returns(post_ret)
+ ret = @ld.post_entry("test content", "this is test", "test")
+ assert_instance_of(String, ret)
+ end
+end
+
end
def test_to_xml
- assert( @ld.to_xml("test content", "test title", "test"))
+ assert(@ld.to_xml("test content", "test title", "test"))
end
def test_post_entry