Class: HatenaFotolife::Client
- Inherits:
-
Object
- Object
- HatenaFotolife::Client
- Defined in:
- lib/hatena_fotolife/client.rb
Constant Summary collapse
- DEFAULT_CONFIG_PATH =
'./config.yml'
- FOTOLIFE_URI =
'http://f.hatena.ne.jp'
- POST_IMAGE_URI =
'https://f.hatena.ne.jp/atom/post'
Instance Attribute Summary collapse
-
#requester ⇒ Object
writeonly
Sets the attribute requester.
Class Method Summary collapse
-
.create(config_file = DEFAULT_CONFIG_PATH) {|fotolife| ... } ⇒ HatenaFotolife::Client
Create a new hatenafotolife AtomPub client from a configuration file.
Instance Method Summary collapse
- #file_path_to_mime_type(file_path:) ⇒ Object
-
#image_xml(title:, content:, subject:, mime_type:) ⇒ String
Build a entry XML from arguments.
-
#initialize(config = nil) ⇒ Client
constructor
A new instance of Client.
-
#post_image(title: nil, file_path:, subject: nil) ⇒ String
Post a image.
Constructor Details
#initialize(config = nil) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 |
# File 'lib/hatena_fotolife/client.rb', line 23 def initialize(config = nil) if block_given? yield config = Configuration.new config.check_valid_or_raise end @requester = Requester.create(config) end |
Instance Attribute Details
#requester=(value) ⇒ Object (writeonly)
Sets the attribute requester
11 12 13 |
# File 'lib/hatena_fotolife/client.rb', line 11 def requester=(value) @requester = value end |
Class Method Details
.create(config_file = DEFAULT_CONFIG_PATH) {|fotolife| ... } ⇒ HatenaFotolife::Client
Create a new hatenafotolife AtomPub client from a configuration file.
16 17 18 19 20 21 |
# File 'lib/hatena_fotolife/client.rb', line 16 def self.create(config_file = DEFAULT_CONFIG_PATH) config = Configuration.create(config_file) fotolife = HatenaFotolife::Client.new(config) return fotolife unless block_given? yield fotolife end |
Instance Method Details
#file_path_to_mime_type(file_path:) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/hatena_fotolife/client.rb', line 31 def file_path_to_mime_type(file_path:) ext = File.extname(file_path).downcase mime_type = case ext when '.png' then 'image/png' when '.gif' then 'image/gif' when '.jpg','.jpeg' then 'image/jpeg' else raise "Unsupported file extension: #{ext}" end return mime_type end |
#image_xml(title:, content:, subject:, mime_type:) ⇒ String
Build a entry XML from arguments.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hatena_fotolife/client.rb', line 67 def image_xml(title:, content:, subject:, mime_type:) builder = Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml| xml.entry('xmlns' => 'http://purl.org/atom/ns') do xml.title title xml.content(content, type: mime_type, mode: 'base64') if subject xml.doc.root.add_namespace_definition('dc', 'http://purl.org/dc/elements/1.1/') xml['dc'].subject(subject) end end end builder.to_xml end |
#post_image(title: nil, file_path:, subject: nil) ⇒ String
Post a image.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hatena_fotolife/client.rb', line 47 def post_image(title: nil, file_path:, subject: nil) begin title = File.basename(file_path, '.*') unless title content = Base64.encode64(File.open(file_path, 'rb').read) mime_type = file_path_to_mime_type(file_path: file_path) entry_xml = image_xml(title: title, content: content, subject: subject, mime_type: mime_type) response = post(entry_xml) image = Image.load_xml(response.body) puts "Image url: #{image.image_uri}" image.image_uri rescue StandardError => e puts "Error: #{e.}" end end |