class CopyTunerClient::Client
the application will not need to interact with it directly.
A client is usually instantiated when {Configuration#apply} is called, and
download and upload blurbs, as well as issuing deploys.
Communicates with the CopyTuner server. This class is used to actually
def check(response)
def check(response) case response when Net::HTTPNotFound raise InvalidApiKey, "Invalid API key: #{api_key}" when Net::HTTPNotModified false when Net::HTTPSuccess true else raise ConnectionError, "#{response.code}: #{response.body}" end end
def connect
def connect http = Net::HTTP.new(host, port) http.open_timeout = http_open_timeout http.read_timeout = http_read_timeout http.use_ssl = secure http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.ca_file = ca_file begin yield http rescue *HTTP_ERRORS => exception raise ConnectionError, "#{exception.class.name}: #{exception.message}" end end
def deploy
-
(ConnectionError)
- if the connection fails
def deploy connect do |http| response = http.post(uri('deploys'), '') check response log 'Deployed' end end
def download
-
(ConnectionError)
- if the connection fails
Other tags:
- Yield: - downloaded blurbs
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
def download_resource
def download_resource if public? 'published_blurbs' else 'draft_blurbs' end end
def initialize(options)
(**options)
-
:ca_file
(String
) -- path to root certificate file for ssl verification -
:logger
(Logger
) -- where to log transactions -
:secure
(Boolean
) -- whether to use SSL -
:http_open_timeout
(Fixnum
) -- how long to wait before timing out when opening the socket -
:http_read_timeout
(Fixnum
) -- how long to wait before timing out when reading data from the socket -
:public
(Boolean
) -- whether to download draft or published content -
:port
(Fixnum
) -- the port to connect to -
:api_key
(String
) -- API key of the project to connect to
Parameters:
-
options
(Hash
) --
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
def log(message)
def log(message) logger.info message end
def public?
def public? @public end
def upload(data)
-
(ConnectionError)
- if the connection fails
Parameters:
-
data
(Hash
) -- the blurbs to upload
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
def uri(resource)
def uri(resource) "/api/v2/projects/#{api_key}/#{resource}" end