Class: HatenaFotolife::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute requester to.



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.

Parameters:

  • config_file (String) (defaults to: DEFAULT_CONFIG_PATH)

    configuration file path

Yields:

  • (fotolife)

Returns:



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

#image_xml(title:, content:, subject:) ⇒ String

Build a entry XML from arguments.

Parameters:

  • title (String)

    entry title

  • subject (String)

    folder name

  • content (String)

    entry content

Returns:

  • (String)

    XML string



50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ HatenaImage::Image

Post a image.

Parameters:

  • title (String) (defaults to: nil)

    entry title

  • content (String)

    entry content

Returns:

  • (HatenaImage::Image)

    posted image



35
36
37
38
39
40
41
42
43
# 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