Class: Localedata::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/localedata/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/localedata/client.rb', line 6

def initialize(access_token)
  @access_token = access_token
end

Instance Method Details

#connectionObject



39
40
41
# File 'lib/localedata/client.rb', line 39

def connection
  @connection ||= Faraday.new(url: "https://app.localedata.com")
end

#pull(project_id, locale) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/localedata/client.rb', line 10

def pull(project_id, locale)
  success = false
  status_code = 0
  yaml_data = nil
  error_message = nil

  begin
    get_data = { access_token: @access_token, project_id: project_id, language_code: locale }
    response = connection.get("/api/v1/exports", get_data)

    success = response.success?
    status_code = response.status

    data = JSON.parse(response.body)
    yaml_data = data["yaml"]
    error_message = data["error"]

  rescue Faraday::ConnectionFailed
    success = false
    error_message = "API connection failed."

  rescue JSON::ParserError
    success = false
    error_message = "API response parsing failed."
  end

  { success: success, status_code: status_code, yaml: yaml_data, error: error_message }
end