Class: Dydra::Command

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/dydra/command.rb,
lib/dydra/command/url.rb,
lib/dydra/command/list.rb,
lib/dydra/command/drop.rb,
lib/dydra/command/open.rb,
lib/dydra/command/login.rb,
lib/dydra/command/abort.rb,
lib/dydra/command/clear.rb,
lib/dydra/command/count.rb,
lib/dydra/command/query.rb,
lib/dydra/command/create.rb,
lib/dydra/command/status.rb,
lib/dydra/command/rename.rb,
lib/dydra/command/import.rb,
lib/dydra/command/logout.rb,
lib/dydra/command/export.rb,
lib/dydra/command/register.rb

Overview

Base class for CLI commands.

Direct Known Subclasses

Abort, Clear, Count, Create, Drop, Export, Import, List, Login, Logout, Open, Query, Register, Rename, Status, URL

Defined Under Namespace

Classes: Abort, Clear, Count, Create, Drop, Export, Import, List, Login, Logout, Open, Query, Register, Rename, Status, URL

Instance Method Summary (collapse)

Methods included from Client

authenticate!, credentials_file, delete, get, get_json, post, reset!, resource, rpc, setup?

Constructor Details

- (Command) initialize(options = {})

A new instance of Command



23
24
25
# File 'lib/dydra/command.rb', line 23

def initialize(options = {})
  @options = options.dup
end

Instance Method Details

- (Object) abort(msg)



47
48
49
# File 'lib/dydra/command.rb', line 47

def abort(msg)
  RDF::CLI.abort(msg)
end

- (Object) basename



27
28
29
# File 'lib/dydra/command.rb', line 27

def basename
  RDF::CLI.basename
end

- (Object) catch_errors



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dydra/command.rb', line 59

def catch_errors
  begin
    yield
  rescue RepositoryMisspecified => e
    puts e
  rescue RestClient::Forbidden
    puts "Insufficient permissions to perform the requested action"
  rescue RestClient::ResourceNotFound => e
    puts "Not Found"
  rescue RestClient::InternalServerError => e
    puts "Internal error: #{e.response.body}"
  rescue RestClient::BadRequest => e
    puts "#{e.response.body}"
  rescue RestClient::RequestTimeout
    puts "No response from server"
  rescue Errno::ECONNREFUSED
    puts "Connection refused by server"
  end
end

- (Boolean) debug?

Returns:

  • (Boolean)


35
36
37
# File 'lib/dydra/command.rb', line 35

def debug?
  @options[:debug] || $DEBUG
end

- (Object) parse_repository_specs(resource_specs)



109
110
111
112
113
114
115
116
117
# File 'lib/dydra/command.rb', line 109

def parse_repository_specs(resource_specs)
  resources = parse_resource_specs(resource_specs)
  resources.each do |resource|
    unless resource.is_a?(Repository)
      abort "invalid repository spec `#{resource}'"
    end
  end
  resources
end

- (Object) parse_resource_specs(resource_specs)



119
120
121
122
123
124
125
126
127
128
# File 'lib/dydra/command.rb', line 119

def parse_resource_specs(resource_specs)
  resources = []
  resource_specs.each do |resource_spec|
    unless resource = Resource.new(resource_spec)
      abort "invalid resource spec `#{resource_spec}'"
    end
    resources << resource
  end
  resources
end

- (Object) require_gem!(gem, msg)



51
52
53
54
55
56
57
# File 'lib/dydra/command.rb', line 51

def require_gem!(gem, msg)
  begin
    require gem
  rescue LoadError => e
    abort "#{msg} (hint: `gem install #{gem}')."
  end
end

- (Object) stderr



43
44
45
# File 'lib/dydra/command.rb', line 43

def stderr
  $stderr
end

- (Object) stdout



39
40
41
# File 'lib/dydra/command.rb', line 39

def stdout
  $stdout
end

- (Object) validate_repository_specs(resource_specs)



85
86
87
88
89
90
91
92
93
# File 'lib/dydra/command.rb', line 85

def validate_repository_specs(resource_specs)
  resources = validate_resource_specs(resource_specs)
  resources.each do |resource|
    unless resource.is_a?(Repository)
      abort "invalid repository spec `#{resource}'"
    end
  end
  resources
end

- (Object) validate_resource_specs(resource_specs)



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dydra/command.rb', line 95

def validate_resource_specs(resource_specs)
  resources = parse_resource_specs(resource_specs)
  resources.each do |resource|
    case resource
      when Account
        #abort "unknown account `#{resource}'" unless resource.exists? # FIXME
      when Repository
        #abort "unknown account `#{resource.account}'" unless resource.account.exists? # FIXME
        #abort "unknown repository `#{resource}'" unless resource.exists?
    end
  end
  resources
end

- (Boolean) verbose?

Returns:

  • (Boolean)


31
32
33
# File 'lib/dydra/command.rb', line 31

def verbose?
  @options[:verbose] || $VERBOSE || debug?
end

- (Object) wrap_errors(*args)



79
80
81
82
83
# File 'lib/dydra/command.rb', line 79

def wrap_errors(*args)
  catch_errors do
    execute(*args)
  end
end