Class: CopycopterClient::Client

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

Overview

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

A client is usually instantiated when CopycopterClient::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 Copycopter.

[Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
Net::ProtocolError, SocketError, OpenSSL::SSL::SSLError,
Errno::ECONNREFUSED]

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Usually instantiated from CopycopterClient::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



28
29
30
31
32
33
# File 'lib/copycopter_client/client.rb', line 28

def initialize(options)
  [:api_key, :host, :port, :public, :http_read_timeout,
    :http_open_timeout, :secure, :logger, :ca_file].each do |option|
    instance_variable_set "@#{option}", options[option]
  end
end

Instance Method Details

#deployObject

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

Raises:



75
76
77
78
79
80
81
# File 'lib/copycopter_client/client.rb', line 75

def deploy
  connect do |http|
    response = http.post(uri('deploys'), '')
    check response
    log 'Deployed'
  end
end

#download {|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:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/copycopter_client/client.rb', line 45

def download
  connect do |http|
    request = Net::HTTP::Get.new(uri(download_resource))
    request['If-None-Match'] = @etag
    response = http.request(request)

    if check response
      log 'Downloaded translations'
      yield JSON.parse(response.body)
    else
      log 'No new translations'
    end

    @etag = response['ETag']
  end
end

#upload(data) ⇒ Object

Uploads the given hash of blurbs as draft content.

Parameters:

  • data (Hash)

    the blurbs to upload

Raises:



65
66
67
68
69
70
71
# File 'lib/copycopter_client/client.rb', line 65

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