Class: CopyTunerClient::Client
- Inherits:
-
Object
- Object
- CopyTunerClient::Client
- 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]
Instance Method Summary collapse
-
#deploy ⇒ Object
Issues a deploy, marking all draft content as published for this project.
-
#download {|Hash| ... } ⇒ Object
Downloads all blurbs for the given api_key.
-
#initialize(options) ⇒ Client
constructor
Usually instantiated from CopyTunerClient::Configuration#apply.
-
#upload(data) ⇒ Object
Uploads the given hash of blurbs as draft content.
Constructor Details
#initialize(options) ⇒ Client
Usually instantiated from CopyTunerClient::Configuration#apply. Copies options.
29 30 31 32 33 34 |
# File 'lib/copy_tuner_client/client.rb', line 29 def initialize() [:api_key, :host, :port, :public, :http_read_timeout, :http_open_timeout, :secure, :logger, :ca_file, :s3_host].each do |option| instance_variable_set "@#{option}", [option] end end |
Instance Method Details
#deploy ⇒ Object
Issues a deploy, marking all draft content as published for this project.
78 79 80 81 82 83 84 |
# File 'lib/copy_tuner_client/client.rb', line 78 def deploy connect(host) 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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/copy_tuner_client/client.rb', line 46 def download 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 if check response log "Downloaded translations (#{t_ms}ms)" yield JSON.parse(response.body) else log "No new translations (#{t_ms}ms)" end @etag = response['ETag'] end end |
#upload(data) ⇒ Object
Uploads the given hash of blurbs as draft content.
68 69 70 71 72 73 74 |
# File 'lib/copy_tuner_client/client.rb', line 68 def upload(data) connect(host) do |http| response = http.post(uri('draft_blurbs'), data.to_json, 'Content-Type' => 'application/json') check response log 'Uploaded missing translations' end end |