Class: Mode::Commands::Import
- Inherits:
-
Object
- Object
- Mode::Commands::Import
show all
- Includes:
- Helpers
- Defined in:
- lib/mode/commands/import.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
#config_dir, #config_exists?, #config_path, #configure_api_requests!, #connect_config_exists?, #require_config!, #require_connect_config!, #require_credentials!, #timer_block
Constructor Details
#initialize(source, account, table_name) ⇒ Import
Returns a new instance of Import.
10
11
12
13
14
15
16
17
18
|
# File 'lib/mode/commands/import.rb', line 10
def initialize(source, account, table_name)
@source = source
@account = account
@table_name = table_name
require_config!
require_credentials!
configure_api_requests!
end
|
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
7
8
9
|
# File 'lib/mode/commands/import.rb', line 7
def account
@account
end
|
#source ⇒ Object
Returns the value of attribute source.
6
7
8
|
# File 'lib/mode/commands/import.rb', line 6
def source
@source
end
|
#table_name ⇒ Object
Returns the value of attribute table_name.
8
9
10
|
# File 'lib/mode/commands/import.rb', line 8
def table_name
@table_name
end
|
Class Method Details
.build_package(source, options = {}) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/mode/commands/import.rb', line 94
def build_package(source, options = {})
name = options[:name] || 'data'
dest = options[:dest] || tmpdir
if File.exist?(source)
converter = convert(source)
DataPackage::Package.new(dest).tap do |package|
package.name = name
package.version = '0.0.1'
package.resources << build_resource(converter)
end
else
nil
end
end
|
.find_or_build_package(source, options = {}) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/mode/commands/import.rb', line 110
def find_or_build_package(source, options = {})
package = find_package(source)
if package.nil?
build_opts = {
:name => options[:name],
:dest => options[:dest]
}
package = build_package(source, build_opts)
else
package = prune_package(source, package)
end
package
end
|
.find_package(source, path = nil) ⇒ Object
Utilities that can be useful elsewhere
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/mode/commands/import.rb', line 81
def find_package(source, path = nil)
path = path || search_path(source)
if path =~ /\/data$/
parent = parent_path(path)
find_package(source, parent)
elsif package_exists?(path)
DataPackage::Package.open(path)
else
nil
end
end
|
.target_resource_path(path = nil) ⇒ Object
126
127
128
|
# File 'lib/mode/commands/import.rb', line 126
def target_resource_path(path = nil)
"data/index#{path.nil? ? '.csv' : File.extname(path)}"
end
|
Instance Method Details
#execute ⇒ Object
20
21
22
23
24
|
# File 'lib/mode/commands/import.rb', line 20
def execute
package = make_package
uploaded = upload_package(package)
imported = import_package(uploaded)
end
|
#import_package(uploaded) ⇒ Object
44
45
46
47
|
# File 'lib/mode/commands/import.rb', line 44
def import_package(uploaded)
resource = parse_uploaded_response(uploaded)
submit_import_form(resource, { 'table_name' => table_name })
end
|
#make_package ⇒ Object
26
27
28
|
# File 'lib/mode/commands/import.rb', line 26
def make_package
find_or_build_package(source, :name => table_name)
end
|
#target_resource_path(path) ⇒ Object
49
50
51
|
# File 'lib/mode/commands/import.rb', line 49
def target_resource_path(path)
self.class.target_resource_path(path)
end
|
#upload_package(package) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/mode/commands/import.rb', line 30
def upload_package(package)
resource = package.resources.first
file_path = File.join(resource.base_path, resource.path)
replace_resource_path!(resource)
Mode::API::Request.post(upload_path, :package => {
:contents => {
'datapackage.json' => package.to_json,
resource.path => Faraday::UploadIO.new(file_path, 'application/octet-stream')
}, :name => table_name
})
end
|