Class: CopyTunerClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/copy_tuner_client/client.rb

Overview

Communicates with the CopyTuner server. This class is used to actually download and upload blurbs, as well as issuing deploys.

A client is usually instantiated when CopyTunerClient::Configuration#apply is called, and the application will not need to interact with it directly.

Constant Summary collapse

HTTP_ERRORS =

These errors will be rescued when connecting CopyTuner.

[
  Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
  Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
  Net::ProtocolError, SocketError, OpenSSL::SSL::SSLError,
  Errno::ECONNREFUSED
].freeze
USER_AGENT =
"copy_tuner_client #{CopyTunerClient::VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Usually instantiated from CopyTunerClient::Configuration#apply. Copies options.

Parameters:

  • options (Hash)

Options Hash (options):

  • :api_key (String)

    API key of the project to connect to

  • :port (Fixnum)

    the port to connect to

  • :public (Boolean)

    whether to download draft or published content

  • :http_read_timeout (Fixnum)

    how long to wait before timing out when reading data from the socket

  • :http_open_timeout (Fixnum)

    how long to wait before timing out when opening the socket

  • :secure (Boolean)

    whether to use SSL

  • :logger (Logger)

    where to log transactions

  • :ca_file (String)

    path to root certificate file for ssl verification



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/copy_tuner_client/client.rb', line 37

def initialize(options)
  @etag = nil
  @downloaded_blurbs = {}

  %i[
    api_key host port public http_read_timeout
    http_open_timeout secure logger ca_file s3_host download_cache_dir
  ].each do |option|
    instance_variable_set("@#{option}", options[option])
  end

  @download_cache_dir.mkpath
  load_cachedata(last_download_path)
end

Instance Attribute Details

#etagString? (readonly)

ETags を外部から取得可能にする

Returns:

  • (String, nil)

    現在のETag値



25
26
27
# File 'lib/copy_tuner_client/client.rb', line 25

def etag
  @etag
end

Instance Method Details

#deployObject

Issues a deploy, marking all draft content as published for this project.

Raises:



99
100
101
102
103
104
105
# File 'lib/copy_tuner_client/client.rb', line 99

def deploy
  connect(host) do |http|
    response = http.post(uri('deploys'), '', 'User-Agent' => USER_AGENT)
    check(response)
    log('Deployed')
  end
end

#download(cache_fallback: false) {|Hash| ... } ⇒ Object

Downloads all blurbs for the given api_key.

If the public option was set to true, this will use published blurbs. Otherwise, draft content is fetched.

The client tracks ETags between download requests, and will return without yielding anything if the server returns a not modified response.

Yields:

  • (Hash)

    downloaded blurbs

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/copy_tuner_client/client.rb', line 62

def download(cache_fallback: false)
  connect(s3_host) do |http|
    request = Net::HTTP::Get.new(uri(download_resource))
    request['If-None-Match'] = @etag
    log('Start downloading translations')
    t = Time.now
    response = http.request(request)
    t_ms = ((Time.now - t) * 1000).to_i
    downloaded = check(response)
    if downloaded
      # NOTE: Net::HTTPではgzipが透過的に扱われるため正確なファイルサイズや速度をログに出すのは難しい
      log("Downloaded translations (#{t_ms}ms)")
      @downloaded_blurbs = JSON.parse(response.body)
      @etag = response['ETag']
      last_download_path.write(JSON.pretty_generate(etag: @etag, downloaded_blurbs: @downloaded_blurbs))
    else
      log("No new translations (#{t_ms}ms)")
    end

    yield(@downloaded_blurbs) if downloaded || cache_fallback
  end
end

#upload(data) ⇒ Object

Uploads the given hash of blurbs as draft content.

Parameters:

  • data (Hash)

    the blurbs to upload

Raises:



88
89
90
91
92
93
94
95
# File 'lib/copy_tuner_client/client.rb', line 88

def upload(data)
  connect(host) do |http|
    headers = { 'Content-Type' => 'application/json', 'User-Agent' => USER_AGENT }
    response = http.post(uri('draft_blurbs'), data.to_json, headers)
    check(response)
    log('Uploaded missing translations')
  end
end