Debian Codegraph @

tasks/transifex.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 =begin rdoc
4 Transifexと連携するためのユーティリティ
5 =end
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 # Transifexプロジェクトの情報を取得する
15 # ==== Args
16 # [project_name] String プロジェクト名
17 # ==== Return
18 # Hashプロジェクトの情報
19 def project_detail(project_name)
20 get_request("/project/#{project_name}/?details")
21 end
22
23 # resource(mikutterの場合はpotファイル)をアップロードし、新しいResourceとして登録する。
24 # 既に同じslugを持つResourceは登録できない。代わりに、 Transifex.resource_update を使う
25 # ==== Args
26 # [project_name:] プロジェクト名
27 # [slug:] String アップロードするresourceのslug。プラグインスラッグを使用する。50文字まで
28 # [name:] String resourceの名前。プラグイン名を使用する。
29 # [i18n_type:] String 翻訳形式。省略するとPO。mikutterでは必ず省略する。
30 # [categories:] Array カテゴリ。いらん
31 # [priority:] Transifex::Priority 翻訳優先順位。
32 # [content:] IO|String resourceの内容。potファイルの中身のこと。IOを渡すとそれをreadした結果、Stringならその内容をそのままアップロードする
33 # ==== Return
34 # Hash レスポンス
35 # ==== See
36 # http://docs.transifex.com/api/resources/#uploading-and-downloading-resources
37 # ==== Raise
38 # SlugTooLongError slugが SLUG_SIZE 文字を超えている場合
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 # resource(mikutterの場合はpotファイル)をアップロードし、同じslugを持つResourceを上書きする。
58 # 存在しないResourceは登録できない。代わりに、 Transifex.resource_create を使う
59 # ==== Args
60 # [project_name:] プロジェクト名
61 # [slug:] String アップロードするresourceのslug。プラグインスラッグを使用する。50文字まで
62 # [content:] IO|String resourceの内容。potファイルの中身のこと。IOを渡すとそれをreadした結果、Stringならその内容をそのままアップロードする
63 # ==== Return
64 # Hash レスポンス
65 # ==== See
66 # http://docs.transifex.com/api/resources/#uploading-and-downloading-translations-for-a-file
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