class Terrestrial::Web

Public Class Methods

new(api_token = nil) click to toggle source
# File lib/terrestrial/web.rb, line 8
def initialize(api_token = nil)
  @url   = URI.parse(Config[:api_url])
  @token = api_token || token
end

Public Instance Methods

create_app(project_id, platform) click to toggle source
# File lib/terrestrial/web.rb, line 22
def create_app(project_id, platform)
  post("projects/#{project_id}/apps", 
  {
    app: {
      platform: platform
    }
  })
end
get_app_strings(project_id, app_id) click to toggle source
# File lib/terrestrial/web.rb, line 31
def get_app_strings(project_id, app_id)
  get("projects/#{project_id}/apps/#{app_id}/strings")
end
get_profile() click to toggle source
# File lib/terrestrial/web.rb, line 39
def get_profile
  get("me")
end
get_translations(project_id, app_id) click to toggle source
# File lib/terrestrial/web.rb, line 35
def get_translations(project_id, app_id)
  get("projects/#{project_id}/apps/#{app_id}/translations")
end
push(project_id, app_id, strings_and_context) click to toggle source
# File lib/terrestrial/web.rb, line 13
def push(project_id, app_id, strings_and_context)
  post("projects/#{project_id}/apps/#{app_id}/imports", 
  {
    import: {
      entries: strings_and_context
    }
  })
end

Private Instance Methods

base_url() click to toggle source
# File lib/terrestrial/web.rb, line 72
def base_url
  @url.request_uri
end
get(path) click to toggle source
# File lib/terrestrial/web.rb, line 59
def get(path)
  http = Net::HTTP.new(@url.host, @url.port)
  http.use_ssl = true if @url.scheme == "https"

  request = Net::HTTP::Get.new(base_url + path)
  request["Content-Type"] = "application/json"
  request["AUTHENTICATE"] = @token

  Response.new(http.request(request))
rescue Errno::ECONNREFUSED, SocketError
  abort 'Unable to connect to Terrestrial Mission Control. Are you connected to the internet?'
end
post(path, payload) click to toggle source
# File lib/terrestrial/web.rb, line 45
def post(path, payload)
  http = Net::HTTP.new(@url.host, @url.port)
  http.use_ssl = true if @url.scheme == "https"

  request = Net::HTTP::Post.new(base_url + path)
  request.body = payload.to_json
  request["Content-Type"] = "application/json"
  request["AUTHENTICATE"] = @token

  Response.new(http.request(request))
rescue Errno::ECONNREFUSED, SocketError
  abort 'Unable to connect to Terrestrial Mission Control. Are you connected to the internet?'
end
token() click to toggle source
# File lib/terrestrial/web.rb, line 78
def token
  Config[:api_key]
end