class HatenaFotolife::Client

Constants

DEFAULT_CONFIG_PATH
FOTOLIFE_URI
POST_IMAGE_URI

Attributes

requester[W]

Public Class Methods

create(config_file = DEFAULT_CONFIG_PATH) { |fotolife| ... } click to toggle source

Create a new hatenafotolife AtomPub client from a configuration file. @param [String] config_file configuration file path @return [HatenaFotolife::Client] created hatenafotolife client

# 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
new(config = nil) { |config = configuration| ... } click to toggle source
# 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

Public Instance Methods

image_xml(title:, content:, subject:) click to toggle source

Build a entry XML from arguments. @param [String] title entry title @param [String] subject folder name @param [String] content entry content @return [String] XML string

# File lib/hatena_fotolife/client.rb, line 50
def image_xml(title:, content:, subject:)
  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: 'image/jpeg', 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) click to toggle source

Post a image. @param [String] title entry title @param [String] content entry content @return [HatenaImage::Image] posted image

# File lib/hatena_fotolife/client.rb, line 35
def post_image(title: nil, file_path:, subject: nil)
  title = File.basename(file_path, '.*') unless title
  content = Base64.encode64(open(file_path).read)
  entry_xml = image_xml(title: title, content: content, subject:subject)
  response = post(entry_xml)
  image = Image.load_xml(response.body)
  puts "Image url: #{image.image_uri}"
  image.image_uri
end

Private Instance Methods

post(entry_xml) click to toggle source
# File lib/hatena_fotolife/client.rb, line 66
def post(entry_xml)
  @requester.post(POST_IMAGE_URI, entry_xml)
end