| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
ためのユーティリティ |
| 5 |
|
| 6 |
module Transifex |
| 7 |
extend self |
| 8 |
|
| 9 |
API_URL_PREFIX = 'https://www.transifex.com/api/2'.freeze |
| 10 |
SLUG_SIZE = 50 |
| 11 |
CONTENT_TYPE_MULTIPART_FORMDATA = 'multipart/form-data' |
| 12 |
CONTENT_TYPE_APPLICATION_JSON = 'application/json' |
| 13 |
|
| 14 |
クトの情報を取得する |
| 15 |
|
| 16 |
ジェクト名 |
| 17 |
|
| 18 |
ェクトの情報 |
| 19 |
def project_detail(project_name) |
| 20 |
get_request("/project/#{project_name}/?details") |
| 21 |
end |
| 22 |
|
| 23 |
ロードし、新しいResourceとして登録する。 |
| 24 |
わりに、 Transifex.resource_update を使う |
| 25 |
|
| 26 |
ジェクト名 |
| 27 |
プラグインスラッグを使用する。50文字まで |
| 28 |
ラグイン名を使用する。 |
| 29 |
PO。mikutterでは必ず省略する。 |
| 30 |
ゴリ。いらん |
| 31 |
優先順位。 |
| 32 |
とそれをreadした結果、Stringならその内容をそのままアップロードする |
| 33 |
|
| 34 |
スポンス |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
を超えている場合 |
| 39 |
def resource_create(project_name:, slug:, name:, i18n_type: 'PO', categories: [], priority: Priority::NORMAL, content:) |
| 40 |
slug, name, priority = slug.to_s, name.to_s, priority.to_i |
| 41 |
raise SlugTooLongError, "The current maximum value for the field slug is #{SLUG_SIZE} characters. http://docs.transifex.com/api/resources/#uploading-and-downloading-resources" if slug.size > SLUG_SIZE |
| 42 |
if content.is_a? IO |
| 43 |
content = content.read end |
| 44 |
post_request("/project/#{project_name}/resources/", |
| 45 |
content_type: CONTENT_TYPE_APPLICATION_JSON, |
| 46 |
params: { |
| 47 |
slug: slug, |
| 48 |
name: name, |
| 49 |
i18n_type: i18n_type, |
| 50 |
categories: categories, |
| 51 |
priority: priority, |
| 52 |
content: content |
| 53 |
} |
| 54 |
) |
| 55 |
end |
| 56 |
|
| 57 |
ードし、同じslugを持つResourceを上書きする。 |
| 58 |
に、 Transifex.resource_create を使う |
| 59 |
|
| 60 |
ジェクト名 |
| 61 |
プラグインスラッグを使用する。50文字まで |
| 62 |
とそれをreadした結果、Stringならその内容をそのままアップロードする |
| 63 |
|
| 64 |
スポンス |
| 65 |
|
| 66 |
|
| 67 |
def resource_update(project_name:, slug:, content:) |
| 68 |
slug = slug.to_s |
| 69 |
if content.is_a? IO |
| 70 |
content = content.read end |
| 71 |
put_request("/project/#{project_name}/resource/#{slug}/content/", |
| 72 |
content_type: CONTENT_TYPE_APPLICATION_JSON, |
| 73 |
params: {content: content} |
| 74 |
) |
| 75 |
end |
| 76 |
|
| 77 |
def resource_get(project_name:, slug:) |
| 78 |
slug = slug.to_s |
| 79 |
get_request("/project/#{project_name}/resource/#{slug}/content/") |
| 80 |
end |
| 81 |
|
| 82 |
private |
| 83 |
|
| 84 |
def get_request(path) |
| 85 |
clnt = HTTPClient.new |
| 86 |
clnt.ssl_config.set_default_paths |
| 87 |
clnt.set_auth(API_URL_PREFIX, ENV.fetch('TRANSIFEX_USER'), ENV.fetch('TRANSIFEX_PASSWORD')) |
| 88 |
JSON.parse(clnt.get_content(API_URL_PREFIX + path), symbolize_names: true) |
| 89 |
end |
| 90 |
|
| 91 |
def post_request(path, content_type: CONTENT_TYPE_MULTIPART_FORMDATA, params:) |
| 92 |
clnt = HTTPClient.new |
| 93 |
clnt.ssl_config.set_default_paths |
| 94 |
clnt.set_auth(API_URL_PREFIX, ENV.fetch('TRANSIFEX_USER'), ENV.fetch('TRANSIFEX_PASSWORD')) |
| 95 |
case content_type |
| 96 |
when CONTENT_TYPE_MULTIPART_FORMDATA |
| 97 |
content = params |
| 98 |
when CONTENT_TYPE_APPLICATION_JSON |
| 99 |
content = params.to_json |
| 100 |
end |
| 101 |
JSON.parse(clnt.post_content(API_URL_PREFIX + path, content, 'Content-Type' => content_type), symbolize_names: true) |
| 102 |
rescue HTTPClient::BadResponseError => err |
| 103 |
pp err.res.content |
| 104 |
end |
| 105 |
|
| 106 |
def put_request(path, content_type: CONTENT_TYPE_MULTIPART_FORMDATA, params:) |
| 107 |
clnt = HTTPClient.new |
| 108 |
clnt.ssl_config.set_default_paths |
| 109 |
clnt.set_auth(API_URL_PREFIX, ENV.fetch('TRANSIFEX_USER'), ENV.fetch('TRANSIFEX_PASSWORD')) |
| 110 |
case content_type |
| 111 |
when CONTENT_TYPE_MULTIPART_FORMDATA |
| 112 |
content = params |
| 113 |
when CONTENT_TYPE_APPLICATION_JSON |
| 114 |
content = params.to_json |
| 115 |
end |
| 116 |
JSON.parse(clnt.__send__(:follow_redirect, :put, API_URL_PREFIX + path, nil, content, 'Content-Type' => content_type).content, symbolize_names: true) |
| 117 |
rescue HTTPClient::BadResponseError => err |
| 118 |
pp err.res.content |
| 119 |
end |
| 120 |
|
| 121 |
|
| 122 |
class Priority |
| 123 |
attr_reader :to_i |
| 124 |
def initialize(prio) |
| 125 |
@to_i = prio.to_i end |
| 126 |
|
| 127 |
NORMAL = new(0) |
| 128 |
HIGH = new(1) |
| 129 |
URGENT = new(2) |
| 130 |
end |
| 131 |
|
| 132 |
class Error < RuntimeError; end |
| 133 |
class SlugTooLongError < Error; end |
| 134 |
end |