zedkit 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+
2
+ Copyright (c) Zedkit.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,8 @@
1
+
2
+ = Zedkit
3
+
4
+ More details TBA.
5
+
6
+ == Copyright
7
+
8
+ Copyright (c) 2010 Zedkit. See LICENSE for details. http://zedkit.com.
data/Rakefile ADDED
@@ -0,0 +1,75 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ require 'rubygems'
19
+ require 'rake'
20
+
21
+ begin
22
+ require 'jeweler'
23
+ Jeweler::Tasks.new do |gem|
24
+ gem.name = "zedkit"
25
+ gem.summary = %Q{gem for Zedkit}
26
+ gem.description = %Q{gem for Zedkit with all the applicable good stuff easily accessible}
27
+
28
+ gem.email = "support@zedkit.com"
29
+ gem.homepage = "http://github.com/zedkit/zedkit"
30
+ gem.authors = ["Zedkit"]
31
+ gem.rubyforge_project = "zedkit"
32
+
33
+ gem.files = FileList['[A-Z]*', 'bin/*', 'lib/**/*.rb', 'test/**/*.rb']
34
+ gem.executables = %w(zedkit)
35
+ gem.default_executable = %q{zedkit}
36
+
37
+ gem.add_dependency 'json', '>= 1.4.0'
38
+ gem.add_dependency 'nestful'
39
+ end
40
+ Jeweler::GemcutterTasks.new
41
+ rescue LoadError
42
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
43
+ end
44
+
45
+ require 'rake/testtask'
46
+ Rake::TestTask.new(:test) do |test|
47
+ test.libs << 'lib' << 'test'
48
+ test.pattern = 'test/**/test_*.rb'
49
+ test.verbose = true
50
+ end
51
+
52
+ begin
53
+ require 'rcov/rcovtask'
54
+ Rcov::RcovTask.new do |test|
55
+ test.libs << 'test'
56
+ test.pattern = 'test/**/test_*.rb'
57
+ test.verbose = true
58
+ end
59
+ rescue LoadError
60
+ task :rcov do
61
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
62
+ end
63
+ end
64
+
65
+ task :test => :check_dependencies
66
+ task :default => :test
67
+
68
+ require 'rake/rdoctask'
69
+ Rake::RDocTask.new do |rdoc|
70
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
71
+ rdoc.rdoc_dir = 'rdoc'
72
+ rdoc.title = "zedkit #{version}"
73
+ rdoc.rdoc_files.include('README*')
74
+ rdoc.rdoc_files.include('lib/**/*.rb')
75
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.3
data/bin/zedkit ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ ##
3
+ # Copyright (c) Zedkit.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
6
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
7
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
8
+ # Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ # Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
16
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ ##
18
+
19
+ require 'zedkit'
20
+
21
+ Dir["#{File.dirname(__FILE__)}/../lib/cli/*.rb"].each {|ci| require ci }
22
+
23
+ Zedkit::CLI::Runner.new.go!
data/lib/cli/config.rb ADDED
@@ -0,0 +1,20 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ Zedkit.configure do |zk|
19
+ zk.exceptions = true
20
+ end
@@ -0,0 +1,32 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ module Zedkit::CLI
19
+ class Bottom
20
+ class << self
21
+ def method_missing(*args)
22
+ raise Zedkit::CLI::UnknownCommand.new(:message => "#{Zedkit::CLI.ee(args[1][:locale], :general, :unknown)}" \
23
+ << " [#{args[0]}]")
24
+ end
25
+
26
+ protected
27
+ def dashes(length = 128)
28
+ Array.new(length, '-').join << "\n"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,49 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ module Zedkit
19
+ module CLI
20
+ class CommandLineError < Zedkit::ZedkitError
21
+ attr_reader :locale, :message
22
+
23
+ def initialize(info = {})
24
+ @locale = info[:locale] || :en
25
+ @message = info[:message] || nil
26
+ end
27
+
28
+ def to_s
29
+ rs = "\n" << Zedkit::CLI.ee(locale, :general, :error) << "\n"
30
+ rs << " #{Zedkit::CLI.ee(locale, :general, :message)} => #{message}.\n\n" unless message.nil?
31
+ end
32
+ end
33
+
34
+ class MissingCredentials < CommandLineError
35
+ def to_s
36
+ rs = "\n" << Zedkit::CLI.ee(locale, :general, :error) << "\n"
37
+ rs << " #{Zedkit::CLI.ee(locale, :general, :message)} => #{message}.\n" unless message.nil?
38
+ rs << " You need to setup your Zedkit login or user API key in ~/.zedkit to the use the Zedkit CLI.\n"
39
+ rs << " Example:\n"
40
+ rs << " fred@flintstone.com\n"
41
+ rs << " StRongPassWoRd\n\n"
42
+ end
43
+ end
44
+ class UnknownCommand < CommandLineError
45
+ end
46
+ class MissingParameter < CommandLineError
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,56 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ module Zedkit
19
+ module CLI
20
+ class Projects < Zedkit::CLI::Bottom
21
+ class << self
22
+ def list(opts = {})
23
+ rs = dashes(122) << "| #{'Zedkit Projects'.ljust(118)} |\n" << dashes(122) \
24
+ << "| #{'UUID'.ljust(32)} | #{'Name'.ljust(32)} | #{'Location'.ljust(48)} |\n" << dashes(122)
25
+ Zedkit::Users::Projects.get(:user_key => opts[:user_key]) do |up|
26
+ pp = Zedkit::Project.new.set_with_hash up['project']
27
+ rs << "| #{pp.uuid.ljust(32)} | #{pp.name.ljust(32)} | #{pp.location.ljust(48)} |\n"
28
+ end
29
+ puts rs << dashes(122)
30
+ end
31
+
32
+ def show(opts = {})
33
+ puts Zedkit::Project.new(:user_key => opts[:user_key], :locale => opts[:locale], :uuid => opts[:argv][0])
34
+ end
35
+
36
+ def create(opts = {})
37
+ puts "\n" << Zedkit::CLI.ee(opts[:locale], :general, :not_done) << "\n\n"
38
+ end
39
+
40
+ def update(opts = {})
41
+ puts "\n" << Zedkit::CLI.ee(opts[:locale], :general, :not_done) << "\n\n"
42
+ end
43
+
44
+ def delete(opts = {})
45
+ puts "\n" << Zedkit::CLI.ee(opts[:locale], :general, :not_done) << "\n\n"
46
+ end
47
+
48
+ protected
49
+ def before_show_update_delete(opts = {})
50
+ if opts[:argv][0].nil?
51
+ raise Zedkit::CLI::MissingParameter.new(:message => Zedkit::CLI.ee(opts[:locale], :project, :nil)) end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,144 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ module Zedkit
19
+ module CLI
20
+ class Runner
21
+ attr_accessor :user_key, :username, :password, :locale
22
+ attr_reader :section, :command, :items
23
+
24
+ SECTIONS = ['projects']
25
+ CONFIG = "#{File.expand_path('~')}/.zedkit"
26
+
27
+ def initialize
28
+ @user_key, @username, @password = nil, nil, nil
29
+ @locale = :en
30
+ if !ARGV.empty? && ARGV[0].include?(":")
31
+ @section = ARGV[0].split(":")[0]
32
+ @command = ARGV[0].split(":")[1]
33
+ else
34
+ @section = SECTIONS[0]
35
+ ARGV.empty? ? @command = nil : @command = ARGV[0]
36
+ end
37
+ ARGV.shift
38
+ begin
39
+ set_credentials if has_section? && has_command?
40
+ set_items
41
+ rescue Zedkit::CLI::ZedkitError => zke
42
+ puts zke end
43
+ end
44
+
45
+ def has_user_key?
46
+ user_key && user_key.is_a?(String) && !user_key.nil?
47
+ end
48
+ def has_username?
49
+ username && username.is_a?(String) && !username.nil?
50
+ end
51
+ def has_password?
52
+ password && password.is_a?(String) && !password.nil?
53
+ end
54
+ def has_credentials?
55
+ has_user_key? || (has_username? && has_password?)
56
+ end
57
+
58
+ def has_section?
59
+ not section.nil?
60
+ end
61
+ def has_command?
62
+ not command.nil?
63
+ end
64
+
65
+ def has_items?
66
+ items && items.length > 0
67
+ end
68
+ def items_to_key_value_hash
69
+ rh = {}
70
+ items.each {|item| rh[item.split('=')[0]] = item.split('=')[1] if item.include?('=') } if has_items?
71
+ rh
72
+ end
73
+
74
+ def go!
75
+ begin
76
+ if has_section? && has_command?
77
+ if has_credentials?
78
+ unless has_user_key?
79
+ @user_key = Zedkit::Users.verify(:username => username, :password => password)['user_key']
80
+ end
81
+ run
82
+ else
83
+ raise Zedkit::CLI::MissingCredentials.new(:message => Zedkit::CLI.ee(locale, :runner, :credentials)) end
84
+ else
85
+ puts commands end
86
+ rescue Zedkit::ZedkitError => zke
87
+ puts zke end
88
+ end
89
+
90
+ protected
91
+ def me
92
+ Object.const_get('Zedkit').const_get('CLI')
93
+ end
94
+ def run
95
+ is = items_to_key_value_hash
96
+ ks = me.const_get(section.capitalize)
97
+ case command.to_sym
98
+ when :list
99
+ ks.send :before_list, :locale => locale, :items => is, :argv => ARGV if ks.respond_to?(:before_list)
100
+ when :create
101
+ ks.send :before_create, :locale => locale, :items => is, :argv => ARGV if ks.respond_to?(:before_create)
102
+ when :show, :update, :delete
103
+ if ks.respond_to?(:before_show_update_delete)
104
+ ks.send :before_show_update_delete, :locale => locale, :items => is, :argv => ARGV end
105
+ end
106
+ ks.send command.to_sym, :locale => locale, :user_key => user_key, :items => items_to_key_value_hash, :argv => ARGV
107
+ end
108
+ def commands
109
+ "\n" \
110
+ << "== #{Zedkit::CLI.tt(locale, :commands, :projects)}\n\n" \
111
+ << "list # #{Zedkit::CLI.tt(locale, :commands, :projects_list)}\n" \
112
+ << "show <uuid> # #{Zedkit::CLI.tt(locale, :commands, :projects_show)}\n" \
113
+ << "create <name> key=value [...] # #{Zedkit::CLI.tt(locale, :commands, :projects_create)}\n" \
114
+ << "update <uuid> key=value [...] # #{Zedkit::CLI.tt(locale, :commands, :projects_update)}\n" \
115
+ << "delete <uuid> # #{Zedkit::CLI.tt(locale, :commands, :projects_delete)}\n\n" \
116
+ << "==\n\n"
117
+ end
118
+
119
+ private
120
+ def set_credentials
121
+ if File.exists?(CONFIG)
122
+ st = File.open(CONFIG).readlines.map(&:strip).delete_if {|i| i.empty? }
123
+ if st.length > 2
124
+ @username = st[0]
125
+ @password = st[1]
126
+ else
127
+ @user_key = st[0] end
128
+ else
129
+ raise Zedkit::CLI::MissingCredentials.new(:message => Zedkit::CLI.ee(locale, :runner, :zedkit_file)) end
130
+ end
131
+ def set_items
132
+ ARGV.length > 1 ? @items = ARGV.slice(1, ARGV.length - 1) : @items = nil
133
+ end
134
+ def set_locale
135
+ if File.exists?(CONFIG)
136
+ st = File.open(CONFIG).readlines.map(&:strip).delete_if {|i| i.empty? }
137
+ if st.length >= 2
138
+ @locale = st[st.length - 1].to_sym if Zedkit::CLI.include?(st[st.length - 1].to_sym)
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,82 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ module Zedkit::CLI
19
+ class << self
20
+ def tt(locale, key, item)
21
+ Zedkit::CLI.lookup_tt("Zedkit::CLI", locale, key, item)
22
+ end
23
+ def ee(locale, key, item)
24
+ Zedkit::CLI.lookup_ee("Zedkit::CLI", locale, key, item)
25
+ end
26
+ end
27
+
28
+ LOCALES = %w(en).freeze
29
+ CONTENT = {
30
+ :en => {
31
+ :commands => {
32
+ :projects => "Project Commands",
33
+ :projects_list => "List Zedkit projects",
34
+ :projects_show => "Show Zedkit project details",
35
+ :projects_create => "Create a new Zedkit project",
36
+ :projects_update => "Update an existing Zedkit project",
37
+ :projects_delete => "Delete an existing Zedkit project"
38
+ }
39
+ }
40
+ }
41
+ ERRORS = {
42
+ :en => {
43
+ :runner => {
44
+ :credentials => "Missing Zedkit Credentials",
45
+ :zedkit_file => "Missing Zedkit Credentials in ~/.zedkit"
46
+ },
47
+ :general => {
48
+ :error => "Zedkit CLI ERROR.",
49
+ :message => "Message",
50
+ :section => "Unknown Section",
51
+ :unknown => "Unknown Command",
52
+ :not_done => "This operation is not yet implemented."
53
+ },
54
+ :project => {
55
+ :nil => "Project UUID is nil"
56
+ }
57
+ }
58
+ }.freeze
59
+
60
+ class << self
61
+ def lookup_tt(ch, locale, key, item)
62
+ rs = lookup(eval("#{ch}::CONTENT"), locale, key, item)
63
+ rs = lookup(eval("Zedkit::CLI::CONTENT"), locale, key, item) if rs.nil?
64
+ rs
65
+ end
66
+ def lookup_ee(ch, locale, key, item)
67
+ rs = lookup(eval("#{ch}::ERRORS"), locale, key, item)
68
+ rs = lookup(eval("Zedkit::CLI::ERRORS"), locale, key, item) if rs.nil?
69
+ rs
70
+ end
71
+
72
+ protected
73
+ def lookup(set, locale, key, item)
74
+ if set.has_key?(locale) && set[locale].has_key?(key) && set[locale][key].has_key?(item)
75
+ set[locale][key][item]
76
+ elsif set[:en].has_key?(key) && set[:en][key].has_key?(item)
77
+ set[:en][key][item]
78
+ else
79
+ nil end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,108 @@
1
+ ##
2
+ # Copyright (c) Zedkit.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ # Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ # Software.
11
+ #
12
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ ##
17
+
18
+ module Zedkit
19
+ module Client
20
+ class << self
21
+ def crud(method, resource, zks, da, &block)
22
+ rs = nil
23
+ case method
24
+ when :get
25
+ rs = submit_request(:get, resource, zks[:user_key], zks.zdelete_keys!(%w(user_key uuid) + da))
26
+ when :create
27
+ rs = submit_request(:post, resource, zks[:user_key], zks.zdelete_keys!(%w(user_key) + da))
28
+ when :update
29
+ rs = submit_request(:put, resource, zks[:user_key], zks.zdelete_keys!(%w(user_key uuid) + da))
30
+ when :delete
31
+ rs = submit_request(:delete, resource, zks[:user_key])
32
+ end
33
+ if rs && block_given?
34
+ rs.is_a?(Array) ? rs.each {|i| yield(i) } : yield(rs)
35
+ end
36
+ rs
37
+ end
38
+
39
+ def verify(username, password)
40
+ submit_request(:verify, 'users/verify', nil, {}, { :user => username, :password => password })
41
+ end
42
+ def get(resource, user_key, params = {})
43
+ submit_request(:get, resource, user_key, params)
44
+ end
45
+
46
+ protected
47
+ def submit_request(method, rs, user_key = nil, params = {}, options = {})
48
+ rvss = nil
49
+ begin
50
+ http = http_request(method, resource_url(rs), user_key, params.flatten_zedkit_params!, options)
51
+ rvss = JSON.parse(http)
52
+ if rvss.is_a?(Hash) && rvss.has_key?('status') && Zedkit.configuration.exceptions?
53
+ raise DataValidationError.new(:http_code => 200, :api_code => rvss['status']['code'],
54
+ :message => rvss['status']['message'] << " [#{method.upcase} #{resource_url(rs)}]",
55
+ :errors => rvss['errors'])
56
+ end
57
+ rescue Net::HTTPBadResponse
58
+ ## TBD
59
+ rescue Nestful::UnauthorizedAccess
60
+ if Zedkit.configuration.exceptions?
61
+ raise Zedkit::Client::UnauthorizedAccess.new(:http_code => 401,
62
+ :message => "User Credentials are Invalid [#{method.upcase} #{resource_url(rs)}]")
63
+ end
64
+ rescue Nestful::ResourceNotFound
65
+ if Zedkit.configuration.exceptions?
66
+ raise Zedkit::Client::ResourceNotFound.new(:http_code => 404,
67
+ :message => "Resource Requested does not Exist [#{method.upcase} #{resource_url(rs)}]")
68
+ end
69
+ rescue Nestful::ForbiddenAccess
70
+ if Zedkit.configuration.exceptions?
71
+ raise Zedkit::Client::ForbiddenAccess.new(:http_code => 403,
72
+ :message => "Access Denied to the Resource Requested [#{method.upcase} #{resource_url(rs)}]")
73
+ end
74
+ end
75
+ rvss
76
+ end
77
+
78
+ private
79
+ def http_request(method, rs_url, uk, params, options)
80
+ http = nil
81
+ case method
82
+ when :verify
83
+ http = Nestful.get(rs_url, options.merge({ :params => merged_params(params) }))
84
+ when :get
85
+ http = Nestful.get(rs_url, options.merge({ :params => merged_params(params, uk) }))
86
+ when :post
87
+ http = Nestful.post(rs_url, options.merge({ :format => :form, :params => merged_params(params, uk) }))
88
+ when :put
89
+ http = Nestful.put(rs_url, options.merge({ :format => :form, :params => merged_params(params, uk) }))
90
+ when :delete
91
+ http = Nestful.delete(rs_url, options.merge({ :params => merged_params(params, uk) }))
92
+ end
93
+ http
94
+ end
95
+ def resource_url(rs)
96
+ "#{Zedkit.configuration.api_url}/#{rs}"
97
+ end
98
+ def merged_params(params = {}, user_key = nil)
99
+ rh = {}
100
+ rh.merge!({ :project_key => Zedkit.configuration.project_key }) if Zedkit.configuration.has_project_key?
101
+ rh.merge!({ :locales_key => Zedkit.configuration.locales_key }) if Zedkit.configuration.has_locales_key?
102
+ rh.merge!({ :user_key => user_key }) unless user_key.nil?
103
+ rh.merge!(params) unless params.empty?
104
+ rh
105
+ end
106
+ end
107
+ end
108
+ end