livedoor_blog_atom_pub 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/lib/livedoor_blog_atom_pub.rb +66 -0
- data/lib/livedoor_blog_atom_pub/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b11ab963733d39d5b247af38b8263d876864983c
|
4
|
+
data.tar.gz: fc96bab6e4f8af1a5fe5e1978590e981a221acaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81f7b98ca446e0edcc397ffc08aa0ba3726cdada86ff369392d035be2aa480b9a26d13efa82e23ef7ac4927e2e51dd731623127597c12aa4f5f38499299a0303
|
7
|
+
data.tar.gz: deb71cc35f0e71c96fc39a3714d98078e05444dcd313e65afbe0dd3c75b199d6cc165e2423eb1be8cab44c78075408df7fc663e929d4d80d6f149a391fb32d6c
|
data/README.md
CHANGED
@@ -60,6 +60,22 @@ When username is different from blog id
|
|
60
60
|
@client.post_entry('Title', 'Content', draft_flag: true)
|
61
61
|
```
|
62
62
|
|
63
|
+
### Upload image
|
64
|
+
|
65
|
+
Return a image url like http://livedoor.blogimg.jp/example/imgs/0/a/abcdefg.jpg
|
66
|
+
|
67
|
+
#### Set a url
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
@client.upload_image(image_url: 'http://example.com/1.jpg')
|
71
|
+
```
|
72
|
+
|
73
|
+
#### Set a local file path
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
@client.upload_image(image_path: '/src/image/1.jpg')
|
77
|
+
```
|
78
|
+
|
63
79
|
## Development
|
64
80
|
|
65
81
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -1,7 +1,14 @@
|
|
1
1
|
require "livedoor_blog_atom_pub/version"
|
2
2
|
require 'wsse'
|
3
|
+
require 'uri'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'base64'
|
3
6
|
|
4
7
|
module LivedoorBlogAtomPub
|
8
|
+
def self.root_path
|
9
|
+
File.dirname __dir__
|
10
|
+
end
|
11
|
+
|
5
12
|
class Client
|
6
13
|
SERVER_HOST = "livedoor.blogcms.jp"
|
7
14
|
def initialize(username, api_key, blog_id: nil)
|
@@ -16,6 +23,29 @@ module LivedoorBlogAtomPub
|
|
16
23
|
response = http.post("/atom/blog/#{@blog_id}/article", xml, {'X-WSSE' => WSSE::header(@username, @api_key)})
|
17
24
|
end
|
18
25
|
|
26
|
+
# return image url as string
|
27
|
+
def upload_image(image_path: nil, image_url: nil)
|
28
|
+
if !image_path.nil? || !image_url.nil?
|
29
|
+
image = image_path || image_url
|
30
|
+
bin = read_image(image)
|
31
|
+
ext = get_image_extension(bin)
|
32
|
+
|
33
|
+
# use new AtomPub API
|
34
|
+
# it's only ssl version
|
35
|
+
https = Net::HTTP.new(SERVER_HOST, '443')
|
36
|
+
https.use_ssl = true
|
37
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
38
|
+
req = Net::HTTP::Post.new("/atompub/#{@blog_id}/image")
|
39
|
+
req['Authorization'] = 'WSSE profile="UsernameToken"'
|
40
|
+
req['X-WSSE'] = WSSE::header(@username, @api_key)
|
41
|
+
req['Content-Type'] = "image/#{ext}"
|
42
|
+
req.body = bin
|
43
|
+
response = https.request(req)
|
44
|
+
# parse the reponse of xml style and fetch an image url
|
45
|
+
url = response.body.match(/<content.+src="(.+)?"/)[1]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
19
49
|
private
|
20
50
|
def generate_entry(title, content, categories: [], draft_flag: false)
|
21
51
|
cat_name = nil
|
@@ -38,5 +68,41 @@ module LivedoorBlogAtomPub
|
|
38
68
|
out.puts "</entry>"
|
39
69
|
out.string
|
40
70
|
end
|
71
|
+
|
72
|
+
def read_image(image)
|
73
|
+
if uri?(image)
|
74
|
+
open(image).read
|
75
|
+
else
|
76
|
+
File.open(image).read
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def uri?(target)
|
81
|
+
begin
|
82
|
+
uri = URI.parse(target)
|
83
|
+
rescue URI::InvalidURIError
|
84
|
+
return false
|
85
|
+
end
|
86
|
+
uri.scheme =~ /^http/
|
87
|
+
end
|
88
|
+
|
89
|
+
# get file extension from binary
|
90
|
+
# referenced from http://stackoverflow.com/a/16635245
|
91
|
+
def get_image_extension(binary)
|
92
|
+
png = Regexp.new("\x89PNG".force_encoding("binary"))
|
93
|
+
jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
|
94
|
+
jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
|
95
|
+
# if image read with File.open, the binary is utf-8 from Ruby 2.0
|
96
|
+
case binary.force_encoding("binary")
|
97
|
+
when /^GIF8/
|
98
|
+
'gif'
|
99
|
+
when /^#{png}/
|
100
|
+
'png'
|
101
|
+
when /^#{jpg}/
|
102
|
+
'jpg'
|
103
|
+
when /^#{jpg2}/
|
104
|
+
'jpg'
|
105
|
+
end
|
106
|
+
end
|
41
107
|
end
|
42
108
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: livedoor_blog_atom_pub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- YuheiNakasaka
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|