hapyrus 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.autotest ADDED
@@ -0,0 +1,46 @@
1
+ # -*- ruby -*-
2
+
3
+ # require 'autotest/autoupdate'
4
+ # require 'autotest/once'
5
+ # require 'autotest/rcov'
6
+ # require 'autotest/restart'
7
+ # require 'autotest/timestamp'
8
+
9
+ # Autotest::AutoUpdate.sleep_time = o
10
+ # Autotest::AutoUpdate.update_cmd = o
11
+ # Autotest::RCov.command = o
12
+ # Autotest::RCov.pattern = o
13
+
14
+ module Autotest::Growl
15
+ def self.growl(title, msg, img, pri=0, sticky="")
16
+ msg += " at #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
17
+ system "growlnotify #{ENV['GROWLNOTIFY_OPTS']} -n autotest #{title} -m #{msg.inspect} --image #{img} -p #{pri} #{sticky}"
18
+ end
19
+
20
+ Autotest.add_hook :ran_command do |at|
21
+ result = [at.results].flatten.join("\n")
22
+ #output = results.slice(/(\d+)\s+tests,\s*(\d+)\s+assertions,\s*(\d+)\s+failures,\s*(\d+)\s+errors/)
23
+ #result = at.results.last
24
+
25
+ examples = result.slice(/(\d+)\s+example/).to_i
26
+ failures = result.slice(/(\d+)\s+failure/).to_i
27
+ pendings = result.slice(/(\d+)\s+pending/).to_i
28
+ output = "#{examples} examples, #{failures} failures"
29
+ output += ", #{pendings} pendings" if pendings
30
+
31
+ if output
32
+ if failures > 0
33
+ growl "Tests Failed", "#{output}", "autotest/rails_fail.png", 2
34
+ else
35
+ growl "Tests Passed", "#{output}", "autotest/rails_ok.png"
36
+ end
37
+ else
38
+ growl "Tests Failed", "errors", "autotest/rails_fail.png", 2, "-s"
39
+ end
40
+ end
41
+
42
+ Autotest.add_hook :initialize do |at|
43
+ at.add_exception %r{^\./db}
44
+ at.add_exception %r{^\./log}
45
+ end
46
+ end
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,48 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+
14
+ # jeweler generated
15
+ pkg
16
+
17
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
18
+ #
19
+ # * Create a file at ~/.gitignore
20
+ # * Include files you want ignored
21
+ # * Run: git config --global core.excludesfile ~/.gitignore
22
+ #
23
+ # After doing this, these files will be ignored in all your git projects,
24
+ # saving you from having to 'pollute' every project you touch with them
25
+ #
26
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
27
+ #
28
+ # For MacOS:
29
+ #
30
+ #.DS_Store
31
+
32
+ # For TextMate
33
+ #*.tmproj
34
+ #tmtags
35
+
36
+ # For emacs:
37
+ #*~
38
+ #\#*
39
+ #.\#*
40
+
41
+ # For vim:
42
+ #*.swp
43
+
44
+ # For redcar:
45
+ #.redcar
46
+
47
+ # For rubinius:
48
+ #*.rbc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -fs -c
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rest-client", "~>1.6.3"
4
+ gem "i18n"
5
+ gem "activesupport"
6
+ gem "json"
7
+
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.4"
11
+ gem "rspec", ">= 2.0"
12
+ gem "autotest"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.6.1)
5
+ activesupport (3.0.10)
6
+ autotest (4.4.6)
7
+ ZenTest (>= 4.4.1)
8
+ diff-lcs (1.1.2)
9
+ git (1.2.5)
10
+ i18n (0.6.0)
11
+ jeweler (1.6.4)
12
+ bundler (~> 1.0)
13
+ git (>= 1.2.5)
14
+ rake
15
+ json (1.5.3)
16
+ mime-types (1.16)
17
+ rake (0.8.7)
18
+ rest-client (1.6.3)
19
+ mime-types (>= 1.16)
20
+ rspec (2.6.0)
21
+ rspec-core (~> 2.6.0)
22
+ rspec-expectations (~> 2.6.0)
23
+ rspec-mocks (~> 2.6.0)
24
+ rspec-core (2.6.4)
25
+ rspec-expectations (2.6.0)
26
+ diff-lcs (~> 1.1.2)
27
+ rspec-mocks (2.6.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ activesupport
34
+ autotest
35
+ bundler (~> 1.0.0)
36
+ i18n
37
+ jeweler (~> 1.6.4)
38
+ json
39
+ rest-client (~> 1.6.3)
40
+ rspec (>= 2.0)
data/README.rd ADDED
@@ -0,0 +1,6 @@
1
+ = Hapyrus Library and CLI for Ruby
2
+
3
+ == Copyright
4
+
5
+ Copyright (c) 2011 Hapyrus Inc.
6
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "hapyrus"
18
+ gem.homepage = "http://github.com/hapyrus/hapyrus"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Hapyrus Library and CLI for Ruby}
21
+ gem.description = %Q{Hapyrus Library and CLI for Ruby}
22
+ gem.email = "fujibee@hapyrus.com"
23
+ gem.authors = ["Koichi Fujikawa"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require "rspec/core/rake_task"
29
+ desc "Run all specs"
30
+ RSpec::Core::RakeTask.new(:spec) do |t|
31
+ t.verbose = true
32
+ end
33
+
34
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
Binary file
Binary file
data/bin/hapyrus ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'rubygems'
7
+ require 'hapyrus'
8
+
9
+ args = ARGV.dup
10
+ ARGV.clear # for 'gets'
11
+ Hapyrus::Cli.new(args).run
data/hapyrus.gemspec ADDED
@@ -0,0 +1,90 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{hapyrus}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Koichi Fujikawa}]
12
+ s.date = %q{2011-08-22}
13
+ s.description = %q{Hapyrus Library and CLI for Ruby}
14
+ s.email = %q{fujibee@hapyrus.com}
15
+ s.executables = [%q{hapyrus}]
16
+ s.files = [
17
+ ".autotest",
18
+ ".document",
19
+ ".gitignore",
20
+ ".rspec",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "README.rd",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "autotest/discover.rb",
27
+ "autotest/rails_fail.png",
28
+ "autotest/rails_ok.png",
29
+ "bin/hapyrus",
30
+ "hapyrus.gemspec",
31
+ "lib/hapyrus.rb",
32
+ "lib/hapyrus/api_client.rb",
33
+ "lib/hapyrus/cli.rb",
34
+ "lib/hapyrus/command/auth.rb",
35
+ "lib/hapyrus/command/base.rb",
36
+ "lib/hapyrus/command/datasets.rb",
37
+ "lib/hapyrus/command/jobs.rb",
38
+ "lib/hapyrus/credentials.rb",
39
+ "lib/hapyrus/helpers.rb",
40
+ "lib/hapyrus/proxy.rb",
41
+ "spec/hapyrus/api_client_spec.rb",
42
+ "spec/hapyrus/cli_spec.rb",
43
+ "spec/hapyrus/command/auth_spec.rb",
44
+ "spec/hapyrus/command/base_spec.rb",
45
+ "spec/hapyrus/credentials_spec.rb",
46
+ "spec/hapyrus/helpers_spec.rb",
47
+ "spec/hapyrus/proxy_spec.rb",
48
+ "spec/hapyrus_spec.rb",
49
+ "spec/spec_helper.rb"
50
+ ]
51
+ s.homepage = %q{http://github.com/hapyrus/hapyrus}
52
+ s.licenses = [%q{MIT}]
53
+ s.require_paths = [%q{lib}]
54
+ s.rubygems_version = %q{1.8.8}
55
+ s.summary = %q{Hapyrus Library and CLI for Ruby}
56
+
57
+ if s.respond_to? :specification_version then
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
61
+ s.add_runtime_dependency(%q<rest-client>, ["~> 1.6.3"])
62
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
63
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
64
+ s.add_runtime_dependency(%q<json>, [">= 0"])
65
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
67
+ s.add_development_dependency(%q<rspec>, [">= 2.0"])
68
+ s.add_development_dependency(%q<autotest>, [">= 0"])
69
+ else
70
+ s.add_dependency(%q<rest-client>, ["~> 1.6.3"])
71
+ s.add_dependency(%q<i18n>, [">= 0"])
72
+ s.add_dependency(%q<activesupport>, [">= 0"])
73
+ s.add_dependency(%q<json>, [">= 0"])
74
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
75
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
76
+ s.add_dependency(%q<rspec>, [">= 2.0"])
77
+ s.add_dependency(%q<autotest>, [">= 0"])
78
+ end
79
+ else
80
+ s.add_dependency(%q<rest-client>, ["~> 1.6.3"])
81
+ s.add_dependency(%q<i18n>, [">= 0"])
82
+ s.add_dependency(%q<activesupport>, [">= 0"])
83
+ s.add_dependency(%q<json>, [">= 0"])
84
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
85
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
86
+ s.add_dependency(%q<rspec>, [">= 2.0"])
87
+ s.add_dependency(%q<autotest>, [">= 0"])
88
+ end
89
+ end
90
+
@@ -0,0 +1,28 @@
1
+ require 'rest_client'
2
+
3
+ module Hapyrus
4
+ HAPYRUS_API_HOST = ENV['HAPYRUS_API_HOST'] || 'www.hapyrus.com'
5
+
6
+ class ApiClient
7
+ attr_reader :response
8
+ attr_accessor :credentials
9
+
10
+ def initialize(credentials=nil)
11
+ @credentials = credentials || Hapyrus::Credentials.new
12
+ end
13
+ def post(path, params=nil)
14
+ uri = "http://#{HAPYRUS_API_HOST}#{path}"
15
+ resource = RestClient::Resource.new(uri, resource_opts)
16
+ @response = resource.post(params)
17
+ end
18
+ def get(path)
19
+ uri = "http://#{HAPYRUS_API_HOST}#{path}"
20
+ resource = RestClient::Resource.new(uri, resource_opts)
21
+ @response = resource.get(:accept => :json)
22
+ end
23
+ private
24
+ def resource_opts
25
+ {:user => @credentials.user, :password => @credentials.password}
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+ module Hapyrus
2
+ class Cli
3
+ include Hapyrus::Helpers
4
+
5
+ def initialize(args)
6
+ @args = args
7
+ end
8
+
9
+ def run
10
+ begin
11
+ command_class, method = parse_args
12
+ command = command_class.new
13
+
14
+ if @args.size > 0
15
+ command.send(ask_for(method), *@args) if command.respond_to?(ask_for method)
16
+ command.send(method, *@args)
17
+ command.send(display(method), *@args) if command.respond_to?(display method)
18
+ else
19
+ command.send(ask_for method) if command.respond_to?(ask_for method)
20
+ command.send(method)
21
+ command.send(display method) if command.respond_to?(display method)
22
+ end
23
+ rescue => e
24
+ #raise e
25
+ puts "! #{e.to_s}"
26
+ puts
27
+ usage
28
+ end
29
+ end
30
+
31
+ private
32
+ def parse_args
33
+ if @args.size > 0
34
+ cmd = @args.shift
35
+ parse_command(cmd)
36
+ else
37
+ raise 'no command given'
38
+ end
39
+ end
40
+
41
+ def usage
42
+ puts "Usage: hapyrus COMMAND"
43
+ puts
44
+ puts " auth:login # log into hapyrus"
45
+ puts " auto:logout # log out form hapyrus"
46
+ puts " jobs:index # list jobs"
47
+ puts " jobs:show # show job details"
48
+ puts " jobs:create # create job"
49
+ end
50
+
51
+ def ask_for(method); "ask_for_#{method}" end
52
+ def display(method); "display_#{method}" end
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ module Hapyrus
2
+ module Command
3
+ class Auth < Base
4
+ def ask_for_login
5
+ puts "Email:"; email = ask
6
+ puts "password:"; password = ask
7
+ hapyrus.credentials = Hapyrus::Credentials.new(email, password)
8
+ end
9
+ def login
10
+ hapyrus.post('/users/sign_in')
11
+ hapyrus.credentials.authenticate! if hapyrus.response.code == 201 # 201: Created
12
+ end
13
+ def display_login
14
+ puts hapyrus.credentials.authenticated? ?
15
+ "Login successfully!" : "Email or password incorrect."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+ require 'pp'
3
+
4
+ module Hapyrus
5
+ module Command
6
+ class Base
7
+ def initialize
8
+ @api_client = ApiClient.new
9
+ end
10
+ def hapyrus; @api_client end
11
+ def ask; gets.strip end
12
+ def index
13
+ @result = JSON.parse hapyrus.get(class_name_to_path)
14
+ end
15
+ def display_index
16
+ pp @result.first
17
+ end
18
+ def show(id)
19
+ @result = JSON.parse hapyrus.get("#{class_name_to_path}/#{id}")
20
+ end
21
+ def display_show(id)
22
+ pp @result
23
+ end
24
+ private
25
+ def class_name_to_path
26
+ '/' + self.class.name.split('::').last.underscore
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,14 @@
1
+ module Hapyrus
2
+ module Command
3
+ class Datasets < Base
4
+ def create(dataset_params=nil)
5
+ dataset_params ||= {:type => "DatasetSmallText"}
6
+ begin
7
+ hapyrus.post(class_name_to_path, :dataset => dataset_params)
8
+ rescue RestClient::Found
9
+ # OK
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Hapyrus
2
+ module Command
3
+ class Jobs < Base
4
+ def create(app_id, script_id, src_dataset_id, dest_dataset_params=nil)
5
+ dest_dataset = dest_dataset_params || {:type => "DatasetSmallText"}
6
+ begin
7
+ hapyrus.post('/jobs',
8
+ :job => {:type => 'Job', :app_id => app_id, :script_id => script_id,
9
+ :src_dataset_id => src_dataset_id,
10
+ :dest_dataset_attributes => dest_dataset
11
+ })
12
+ rescue RestClient::Found
13
+ # OK
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ module Hapyrus
2
+ class Credentials
3
+ include Helpers
4
+ attr_reader :user, :password
5
+ def initialize(user=nil, password=nil)
6
+ @user = user || ENV['HAPYRUS_LOGIN']
7
+ @password = password || ENV['HAPYRUS_PASSWORD']
8
+ end
9
+ def authenticate!
10
+ @authenticated = true
11
+ write_credentials
12
+ end
13
+ def authenticated?
14
+ @authenticated
15
+ end
16
+ def write_credentials
17
+ dir = File.dirname(credentials_file)
18
+ FileUtils.mkdir_p(dir)
19
+ File.delete(credentials_file) if FileTest.exists?(credentials_file)
20
+ File.open(credentials_file, 'w', 0400) do |out|
21
+ out.puts @user
22
+ out.puts @password
23
+ end
24
+ FileUtils.chmod(0700, dir)
25
+ end
26
+ private
27
+ def credentials_file
28
+ File.join(home_directory, '.hapyrus', 'credentials')
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ module Hapyrus
2
+ module Helpers
3
+ module_function
4
+ def parse_command(cmd)
5
+ klass = Hapyrus::Command::Base
6
+ method = cmd
7
+ if cmd.include?(':')
8
+ class_name, method = cmd.split(':')
9
+ klass = to_command_class(class_name)
10
+ end
11
+ [klass, method]
12
+ end
13
+ def to_command_class(class_name)
14
+ eval("Hapyrus::Command::#{class_name.camelcase}")
15
+ end
16
+ def home_directory
17
+ ENV['HOME'] || Dir.pwd
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module Hapyrus
2
+ class Proxy
3
+ attr_accessor :command_class
4
+ def run_command(method, *args)
5
+ args.flatten!
6
+ if args.size > 0
7
+ @command_class.new.send(method, *args)
8
+ else
9
+ @command_class.new.send(method)
10
+ end
11
+ end
12
+ def method_missing(method, *args)
13
+ run_command(method, args)
14
+ end
15
+ end
16
+ end
data/lib/hapyrus.rb ADDED
@@ -0,0 +1,18 @@
1
+ # common
2
+ require 'active_support/core_ext/string'
3
+ require 'active_support/dependencies'
4
+
5
+ # require all hapyrus libs
6
+ lib_dir = File.expand_path(File.dirname(__FILE__))
7
+ ActiveSupport::Dependencies.autoload_paths << lib_dir
8
+
9
+ # library entry point
10
+ module Hapyrus
11
+ @@proxy = Hapyrus::Proxy.new
12
+
13
+ module_function
14
+ def method_missing(method, *args)
15
+ @@proxy.command_class = Hapyrus::Helpers.to_command_class(method.to_s)
16
+ @@proxy
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hapyrus::ApiClient do
4
+ before do
5
+ @mock_resource = mock(:resource)
6
+ RestClient::Resource.stub(:new) { @mock_resource }
7
+
8
+ @client = Hapyrus::ApiClient.new(Hapyrus::Credentials.new)
9
+ end
10
+ describe '.post' do
11
+ it 'posts request to hapyrus' do
12
+ @mock_resource.should_receive(:post)
13
+
14
+ @client.post('/path')
15
+ end
16
+ end
17
+ describe '.response' do
18
+ it 'returns response after querying' do
19
+ mock_response = mock(:response)
20
+ @mock_resource.should_receive(:post).and_return(mock_response)
21
+ mock_response.should_receive(:code).and_return(201)
22
+
23
+ @client.post('/path')
24
+ @client.response.code.should == 201
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hapyrus::Cli do
4
+ describe '.run' do
5
+ it 'returns usage unless command' do
6
+ cli = Hapyrus::Cli.new([])
7
+ out = capture(:stdout) { cli.run }
8
+ out.should match("Usage")
9
+ end
10
+ it 'calls command class method' do
11
+ cli = Hapyrus::Cli.new(%w(jobs:index))
12
+ mock_job = mock(:job)
13
+ Hapyrus::Command::Jobs.should_receive(:new) { mock_job }
14
+ mock_job.should_receive(:index)
15
+
16
+ cli.run
17
+ end
18
+ end
19
+ describe '.login' do
20
+ before do
21
+ @cli = Hapyrus::Cli.new(%w(auth:login))
22
+ @mock_auth = mock(:auth)
23
+ Hapyrus::Command::Auth.stub(:new) { @mock_auth }
24
+ end
25
+ it 'calls command methods' do
26
+ @mock_auth.should_receive(:ask_for_login)
27
+ @mock_auth.should_receive(:login)
28
+ @mock_auth.should_receive(:display_login)
29
+
30
+ @cli.run
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hapyrus::Command::Auth do
4
+ describe '.login' do
5
+ it 'asks for login' do
6
+ pending 'input method does not work..'
7
+ #out = capture(:stdout) { input("email\npass") { @client.run } }
8
+ auth_command = Hapyrus::Command::Auth.new
9
+ input("email\npass\n") { auth_command.ask_for_login }
10
+ p auth_command.hapyrus.credentials
11
+ auth_command.hapyrus.credentials.user.should == "email"
12
+ auth_command.hapyrus.credentials.password.should == "pass"
13
+ end
14
+ it 'tries to login' do
15
+ mock_api_client = mock(:api_client)
16
+ mock_response = mock(:response)
17
+ mock_cred = mock(:credentials)
18
+ Hapyrus::ApiClient.stub(:new) { mock_api_client }
19
+ mock_api_client.should_receive(:post)
20
+ mock_api_client.should_receive(:response).and_return(mock_response)
21
+ mock_api_client.should_receive(:credentials).and_return(mock_cred)
22
+ mock_response.should_receive(:code).and_return(201)
23
+ mock_cred.should_receive(:authenticate!)
24
+
25
+ auth_command = Hapyrus::Command::Auth.new
26
+ auth_command.login
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hapyrus::Command::Base do
4
+ describe '.class_name_to_path' do
5
+ it 'returns rest root path of the resource' do
6
+ base = Hapyrus::Command::Jobs.new
7
+ base.instance_eval{ class_name_to_path }.should == '/jobs'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hapyrus::Credentials do
4
+ before do
5
+ @credentials_file = "#{ENV['HOME']}/.hapyrus/credentials"
6
+ end
7
+ describe '.initialize' do
8
+ it 'sets user and password from args' do
9
+ cred = Hapyrus::Credentials.new('user', 'pass')
10
+ cred.instance_eval{ @user }.should == 'user'
11
+ cred.instance_eval{ @password }.should == 'pass'
12
+ end
13
+ it 'also sets user and password from env unless args' do
14
+ ENV['HAPYRUS_LOGIN'] = 'user'
15
+ ENV['HAPYRUS_PASSWORD'] = 'pass'
16
+ cred = Hapyrus::Credentials.new
17
+ cred.instance_eval{ @user }.should == 'user'
18
+ cred.instance_eval{ @password }.should == 'pass'
19
+ end
20
+ end
21
+ describe '.authenticate!' do
22
+ it 'forces auth state to authenticated' do
23
+ credentials = Hapyrus::Credentials.new
24
+ credentials.should_receive(:write_credentials)
25
+
26
+ credentials.authenticate!
27
+ credentials.authenticated?.should == true
28
+ end
29
+ end
30
+ describe '.write_credentials' do
31
+ it 'writes credentials info to a file' do
32
+ credentials = Hapyrus::Credentials.new('user', 'pass')
33
+ credentials.write_credentials
34
+ File.read(@credentials_file).should == "user\npass\n"
35
+ end
36
+ it 'changes credentials file and dir to safe permission' do
37
+ credentials = Hapyrus::Credentials.new('user', 'pass')
38
+ credentials.write_credentials
39
+
40
+ mode = ("%o" % File::stat(File.dirname(@credentials_file)).mode)[-3, 3]
41
+ mode.should == '700'
42
+ mode = ("%o" % File::stat(@credentials_file).mode)[-3, 3]
43
+ mode.should == '400'
44
+ end
45
+ after do
46
+ FileUtils.rm_r(File.dirname(@credentials_file))
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ include Hapyrus::Helpers
4
+ describe Hapyrus::Helpers do
5
+ before do
6
+ @reserved_home = ENV['HOME']
7
+ end
8
+ after do
9
+ ENV['HOME'] = @reserved_home
10
+ end
11
+ describe '.parse_command' do
12
+ it 'separates class and method from command' do
13
+ cmd = 'auth:login'
14
+ parse_command(cmd).should == [Hapyrus::Command::Auth, 'login']
15
+ end
16
+ end
17
+ describe '.to_command_class' do
18
+ it 'converts name to command class' do
19
+ to_command_class('auth').should == Hapyrus::Command::Auth
20
+ end
21
+ end
22
+ describe '.home_directory' do
23
+ it 'returns home if exists Env var' do
24
+ home_directory.should == @reserved_home
25
+ end
26
+ it 'returns pwd unless exists Env var' do
27
+ ENV['HOME'] = nil
28
+ home_directory.should == Dir.pwd
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hapyrus::Proxy do
4
+ describe '.run_command' do
5
+ it 'runs command' do
6
+ proxy = Hapyrus::Proxy.new
7
+ proxy.command_class = Hapyrus::Command::Auth
8
+ Hapyrus::Command::Auth.should_receive(:new) do
9
+ mock = mock(:auth)
10
+ mock.should_receive(:login)
11
+ mock
12
+ end
13
+
14
+ proxy.run_command('login')
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hapyrus do
4
+ it 'returns proxy object unless command class' do
5
+ obj = Hapyrus.auth
6
+ obj.class.should == Hapyrus::Proxy
7
+ obj.command_class.should == Hapyrus::Command::Auth
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ RSpec.configure do |config|
2
+ config.mock_with :rspec
3
+ end
4
+
5
+ require 'hapyrus'
6
+
7
+ # http://rails-bestpractices.com/questions/1-test-stdin-stdout-in-rspec
8
+ require 'stringio'
9
+ def capture(*streams)
10
+ streams.map! { |stream| stream.to_s }
11
+ begin
12
+ result = StringIO.new
13
+ streams.each { |stream| eval "$#{stream} = result" }
14
+ yield
15
+ ensure
16
+ streams.each { |stream| eval("$#{stream} = #{stream.upcase}") }
17
+ end
18
+ result.string
19
+ end
20
+
21
+ def input(lines)
22
+ begin
23
+ $stdin = StringIO.new(lines)
24
+ yield
25
+ ensure
26
+ $stdin = STDIN
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hapyrus
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Koichi Fujikawa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-22 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rest-client
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.6.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: i18n
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: activesupport
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: json
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: bundler
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.0.0
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: jeweler
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.6.4
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: &id007 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "2.0"
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *id007
92
+ - !ruby/object:Gem::Dependency
93
+ name: autotest
94
+ requirement: &id008 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *id008
103
+ description: Hapyrus Library and CLI for Ruby
104
+ email: fujibee@hapyrus.com
105
+ executables:
106
+ - hapyrus
107
+ extensions: []
108
+
109
+ extra_rdoc_files: []
110
+
111
+ files:
112
+ - .autotest
113
+ - .document
114
+ - .gitignore
115
+ - .rspec
116
+ - Gemfile
117
+ - Gemfile.lock
118
+ - README.rd
119
+ - Rakefile
120
+ - VERSION
121
+ - autotest/discover.rb
122
+ - autotest/rails_fail.png
123
+ - autotest/rails_ok.png
124
+ - bin/hapyrus
125
+ - hapyrus.gemspec
126
+ - lib/hapyrus.rb
127
+ - lib/hapyrus/api_client.rb
128
+ - lib/hapyrus/cli.rb
129
+ - lib/hapyrus/command/auth.rb
130
+ - lib/hapyrus/command/base.rb
131
+ - lib/hapyrus/command/datasets.rb
132
+ - lib/hapyrus/command/jobs.rb
133
+ - lib/hapyrus/credentials.rb
134
+ - lib/hapyrus/helpers.rb
135
+ - lib/hapyrus/proxy.rb
136
+ - spec/hapyrus/api_client_spec.rb
137
+ - spec/hapyrus/cli_spec.rb
138
+ - spec/hapyrus/command/auth_spec.rb
139
+ - spec/hapyrus/command/base_spec.rb
140
+ - spec/hapyrus/credentials_spec.rb
141
+ - spec/hapyrus/helpers_spec.rb
142
+ - spec/hapyrus/proxy_spec.rb
143
+ - spec/hapyrus_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: http://github.com/hapyrus/hapyrus
146
+ licenses:
147
+ - MIT
148
+ post_install_message:
149
+ rdoc_options: []
150
+
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 3466021671570644332
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: "0"
168
+ requirements: []
169
+
170
+ rubyforge_project:
171
+ rubygems_version: 1.8.8
172
+ signing_key:
173
+ specification_version: 3
174
+ summary: Hapyrus Library and CLI for Ruby
175
+ test_files: []
176
+