linguist_ruby 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ module Linguist::Command
2
+ class Translations < Base
3
+
4
+ def down
5
+ if rails_environment?
6
+ project.resources["en.yml"].download rails_locale_dir
7
+ else
8
+ end
9
+ end
10
+
11
+ def up
12
+ if rails_environment?
13
+ else
14
+
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def rails_environment?
21
+ true #TODO
22
+ end
23
+
24
+ def rails_locale_dir
25
+ Dir.pwd + "/conf/locales"
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,7 @@
1
+ module Linguist::Command
2
+ class Version < Base
3
+ def index
4
+ display Linguist::Client.gem_version_string
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,121 @@
1
+ module Linguist
2
+ module Helpers
3
+ def home_directory
4
+ running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
5
+ end
6
+
7
+ def running_on_windows?
8
+ RUBY_PLATFORM =~ /mswin32|mingw32/
9
+ end
10
+
11
+ def running_on_a_mac?
12
+ RUBY_PLATFORM =~ /-darwin\d/
13
+ end
14
+
15
+ def display(msg, newline=true)
16
+ if newline
17
+ puts(msg)
18
+ else
19
+ print(msg)
20
+ STDOUT.flush
21
+ end
22
+ end
23
+
24
+ def redisplay(line, line_break = false)
25
+ display("\r\e[0K#{line}", line_break)
26
+ end
27
+
28
+ def deprecate(version)
29
+ display "!!! DEPRECATION WARNING: This command will be removed in version #{version}"
30
+ display ""
31
+ end
32
+
33
+ def error(msg)
34
+ STDERR.puts(msg)
35
+ exit 1
36
+ end
37
+
38
+ def confirm(message="Are you sure you wish to continue? (y/n)?")
39
+ display("#{message} ", false)
40
+ ask.downcase == 'y'
41
+ end
42
+
43
+ def confirm_command(app = app)
44
+ # if extract_option('--force')
45
+ # display("Warning: The --force switch is deprecated, and will be removed in a future release. Use --confirm #{app} instead.")
46
+ # return true
47
+ # end
48
+
49
+ raise(Linguist::Command::CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") unless app
50
+
51
+ confirmed_app = extract_option('--confirm', false)
52
+ if confirmed_app
53
+ unless confirmed_app == app
54
+ raise(Linguist::Command::CommandFailed, "Confirmed app #{confirmed_app} did not match the selected app #{app}.")
55
+ end
56
+ return true
57
+ else
58
+ display ""
59
+ display " ! WARNING: Potentially Destructive Action"
60
+ display " ! This command will affect the app: #{app}"
61
+ display " ! To proceed, type \"#{app}\" or re-run this command with --confirm #{app}"
62
+ display ""
63
+ display "> ", false
64
+ if ask.downcase != app
65
+ display " ! Input did not match #{app}. Aborted."
66
+ false
67
+ else
68
+ true
69
+ end
70
+ end
71
+ end
72
+
73
+ def format_date(date)
74
+ date = Time.parse(date) if date.is_a?(String)
75
+ date.strftime("%Y-%m-%d %H:%M %Z")
76
+ end
77
+
78
+ def ask
79
+ gets.strip
80
+ end
81
+
82
+ def shell(cmd)
83
+ FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`}
84
+ end
85
+
86
+ def run_command(command, args=[])
87
+ Linguist::Command.run_internal(command, args)
88
+ end
89
+
90
+ def retry_on_exception(*exceptions)
91
+ retry_count = 0
92
+ begin
93
+ yield
94
+ rescue *exceptions => ex
95
+ raise ex if retry_count >= 3
96
+ sleep 3
97
+ retry_count += 1
98
+ retry
99
+ end
100
+ end
101
+
102
+ def has_git?
103
+ %x{ git --version }
104
+ $?.success?
105
+ end
106
+
107
+ def git(args)
108
+ return "" unless has_git?
109
+ flattened_args = [args].flatten.compact.join(" ")
110
+ %x{ git #{flattened_args} 2>&1 }.strip
111
+ end
112
+ end
113
+ end
114
+
115
+ unless String.method_defined?(:shellescape)
116
+ class String
117
+ def shellescape
118
+ empty? ? "''" : gsub(/([^A-Za-z0-9_\-.,:\/@\n])/n, '\\\\\\1').gsub(/\n/, "'\n'")
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,81 @@
1
+ module Linguist
2
+ module Models
3
+ require 'linguist/models/resource'
4
+
5
+ class Project
6
+ def self.lazy_attr_accessor(*params)
7
+ params.each do |sym|
8
+ define_method(sym) do
9
+ unless defined? @fetched
10
+ fetch
11
+ end
12
+ self.instance_variable_get("@#{sym}")
13
+ end
14
+ define_method("#{sym}=") do |value|
15
+ self.instance_variable_set("@#{sym}", value)
16
+ end
17
+ end
18
+ end
19
+
20
+ lazy_attr_accessor(:title, :link, :weburl, :resources_url, :translations_url, :translations_count, :owner)
21
+
22
+ def initialize(client, link)
23
+ @client = client
24
+ @link = link
25
+ end
26
+
27
+ def create!(attributes={ })
28
+ self.title = attribtes[:title]
29
+ end
30
+
31
+ def destroy
32
+ @client.delete self.link
33
+ end
34
+
35
+ def update(attributes={ })
36
+ @client.put self.link, { :project => attributes }
37
+ end
38
+
39
+ def resources
40
+ unless defined? @resources
41
+ @resources = { }
42
+ response = @client.get(self.resources_url)
43
+ resource_hash = JSON.parse(response)
44
+ links = resource_hash["link"]
45
+ links.each do |link|
46
+ file_name = link["rel"]
47
+ locale, extension = File.basename(file_name, '.*') , File.extname(file_name)
48
+ @resources[file_name] = Linguist::Models::Resource.new(@client, locale, extension, link["href"])
49
+ end
50
+ end
51
+ puts "RESOURCES #{@resources}"
52
+ @resources
53
+ end
54
+
55
+ private
56
+
57
+ def fetch
58
+ @fetched = true
59
+ response = @client.get @link
60
+ project_hash = JSON.parse(response)
61
+ links = project_hash["link"]
62
+ link = links[0]["href"]
63
+ weburl = links[1]["href"]
64
+ translations_url = links[2]["href"]
65
+ resources_url = links[3]["href"]
66
+ init_attributes :title => project_hash["title"], :link => link, :weburl => weburl,
67
+ :owner => project_hash["owner_email"], :translations_count => project_hash["translations_count"],
68
+ :translations_url => translations_url, :resources_url => resources_url
69
+ end
70
+
71
+ def init_attributes(attributes)
72
+ attributes.each_pair do |key, value|
73
+ unless self.instance_variable_get("@#{key}")
74
+ self.send "#{key}=", value
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,34 @@
1
+ module Linguist
2
+ module Models
3
+ require 'linguist/models/project'
4
+
5
+ class Projects
6
+
7
+ PROJECT_URL = '/projects'
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ def create(title)
14
+ @client.post(PROJECT_URL, :project => {:title => title})
15
+ end
16
+
17
+ def all
18
+ return @projects if defined? @projects
19
+ @projects = {}
20
+ response = JSON.parse @client.get(PROJECT_URL).to_s
21
+ response["projects"]["members"].each do |member|
22
+ project = Linguist::Models::Project.new(@client, member["link"][0]["href"])
23
+ @projects[member["title"]] = project
24
+ end
25
+ @projects
26
+ end
27
+
28
+ def [](project_title)
29
+ return all[project_title]
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,26 @@
1
+ module Linguist
2
+ module Models
3
+ class Resource
4
+ attr_reader :locale, :format, :link
5
+
6
+ # @param client [Linguist::Client] passed through client instance
7
+ # @param locale [String] String that represents a locale in ISO 2 format, e.g. 'en', 'de'
8
+ # @param format [String] Extension of the file format, e.g. 'en', 'de'
9
+ # @param link [String] Link to the resource, e.g. 'http://lvh.me:3000/api/v1/projects/project-1/resources/de.properties'
10
+ def initialize(client, locale, format, link)
11
+ @client = client
12
+ @link = link
13
+ @locale = locale
14
+ @format = format
15
+ end
16
+
17
+ # Downloads the resource and creates the new resource file. Overrides existing files.
18
+ # @param dir [String] The directory where to store the file, e.g. '/Users/heli' would create a file '/Users/heli/en.yml'
19
+ def download(dir)
20
+ response = @client.get(link)
21
+ File.open(dir + "/#{locale}#{format}", 'w+') { |f| f.write(response) }
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ class User
2
+ # To change this template use File | Settings | File Templates.
3
+ end
@@ -0,0 +1,3 @@
1
+ module Linguist
2
+ VERSION = "0.0.2"
3
+ end
data/lib/linguist.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Linguist; end
2
+
3
+ require 'linguist/client'