Class: Txgh::TransifexApi

Inherits:
Object
  • Object
show all
Defined in:
lib/txgh/transifex_api.rb

Constant Summary collapse

API_ROOT =
'/api/2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ TransifexApi

Returns a new instance of TransifexApi.



34
35
36
# File 'lib/txgh/transifex_api.rb', line 34

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



32
33
34
# File 'lib/txgh/transifex_api.rb', line 32

def connection
  @connection
end

Class Method Details

.create_from_connection(connection) ⇒ Object



27
28
29
# File 'lib/txgh/transifex_api.rb', line 27

def create_from_connection(connection)
  new(connection)
end

.create_from_credentials(username, password) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/txgh/transifex_api.rb', line 11

def create_from_credentials(username, password)
  connection = Faraday.new(url: 'https://www.transifex.com') do |faraday|
    faraday.request(:multipart)
    faraday.request(:json)
    faraday.request(:url_encoded)

    faraday.response(:logger)
    faraday.use(FaradayMiddleware::FollowRedirects)
    faraday.adapter(Faraday.default_adapter)
  end

  connection.basic_auth(username, password)
  connection.headers.update(Accept: 'application/json')
  create_from_connection(connection)
end

Instance Method Details

#create(tx_resource, content, categories = []) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/txgh/transifex_api.rb', line 52

def create(tx_resource, content, categories = [])
  name = if tx_resource.branch
    "#{tx_resource.source_file} (#{tx_resource.branch})"
  else
    tx_resource.source_file
  end

  payload = {
    slug: tx_resource.resource_slug,
    name: name,
    i18n_type: tx_resource.type,
    categories: CategorySupport.join_categories(categories.uniq),
    content: get_content_io(tx_resource, content)
  }

  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resources/"
  post(url, payload)
end

#create_or_update(tx_resource, content, categories = []) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/txgh/transifex_api.rb', line 38

def create_or_update(tx_resource, content, categories = [])
  if resource_exists?(tx_resource)
    resource = get_resource(*tx_resource.slugs)
    new_categories = Set.new(resource['categories'])
    new_categories.merge(categories)

    # update details first so new content is always tagged
    update_details(tx_resource, categories: new_categories.to_a)
    update_content(tx_resource, content)
  else
    create(tx_resource, content, categories)
  end
end

#delete_resource(tx_resource) ⇒ Object



71
72
73
74
# File 'lib/txgh/transifex_api.rb', line 71

def delete_resource(tx_resource)
  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resource/#{tx_resource.resource_slug}/"
  delete(url)
end

#download(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/txgh/transifex_api.rb', line 97

def download(*args)
  project_slug, resource_slug = case args.first
    when TxResource, TxBranchResource
      [args.first.project_slug, args.first.resource_slug]
    else
      [args[0], args[1]]
  end

  lang = args.last

  json_data = get_json(
    "#{API_ROOT}/project/#{project_slug}/resource/#{resource_slug}/translation/#{lang}/"
  )

  json_data['content']
end

#get_formatsObject



134
135
136
137
# File 'lib/txgh/transifex_api.rb', line 134

def get_formats
  url = "#{API_ROOT}/formats/"
  get_json(url)
end

#get_languages(project_slug) ⇒ Object



124
125
126
127
# File 'lib/txgh/transifex_api.rb', line 124

def get_languages(project_slug)
  url = "#{API_ROOT}/project/#{project_slug}/languages/"
  get_json(url)
end

#get_project(project_slug) ⇒ Object



129
130
131
132
# File 'lib/txgh/transifex_api.rb', line 129

def get_project(project_slug)
  url = "#{API_ROOT}/project/#{project_slug}/"
  get_json(url)
end

#get_resource(project_slug, resource_slug) ⇒ Object



114
115
116
117
# File 'lib/txgh/transifex_api.rb', line 114

def get_resource(project_slug, resource_slug)
  url = "#{API_ROOT}/project/#{project_slug}/resource/#{resource_slug}/"
  get_json(url)
end

#get_resources(project_slug) ⇒ Object



119
120
121
122
# File 'lib/txgh/transifex_api.rb', line 119

def get_resources(project_slug)
  url = "#{API_ROOT}/project/#{project_slug}/resources/"
  get_json(url)
end

#get_stats(project_slug, resource_slug) ⇒ Object



139
140
141
142
# File 'lib/txgh/transifex_api.rb', line 139

def get_stats(project_slug, resource_slug)
  url = "#{API_ROOT}/project/#{project_slug}/resource/#{resource_slug}/stats/"
  get_json(url)
end

#resource_exists?(tx_resource) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/txgh/transifex_api.rb', line 88

def resource_exists?(tx_resource)
  project = tx_resource.project_slug
  slug = tx_resource.resource_slug
  response = get("#{API_ROOT}/project/#{project}/resource/#{slug}/")
  response.status == 200
rescue TransifexNotFoundError
  false
end

#update_content(tx_resource, content) ⇒ Object



76
77
78
79
80
81
# File 'lib/txgh/transifex_api.rb', line 76

def update_content(tx_resource, content)
  content_io = get_content_io(tx_resource, content)
  payload = { content: content_io }
  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resource/#{tx_resource.resource_slug}/content/"
  put(url, payload)
end

#update_details(tx_resource, details = {}) ⇒ Object



83
84
85
86
# File 'lib/txgh/transifex_api.rb', line 83

def update_details(tx_resource, details = {})
  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resource/#{tx_resource.resource_slug}/"
  put(url, details)
end