chimps-cli 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.
Files changed (44) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +32 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +322 -0
  5. data/Rakefile +24 -0
  6. data/VERSION +1 -0
  7. data/bin/chimps +4 -0
  8. data/lib/chimps-cli.rb +46 -0
  9. data/lib/chimps-cli/commands.rb +179 -0
  10. data/lib/chimps-cli/commands/base.rb +65 -0
  11. data/lib/chimps-cli/commands/create.rb +38 -0
  12. data/lib/chimps-cli/commands/delete.rb +29 -0
  13. data/lib/chimps-cli/commands/destroy.rb +36 -0
  14. data/lib/chimps-cli/commands/download.rb +40 -0
  15. data/lib/chimps-cli/commands/get.rb +30 -0
  16. data/lib/chimps-cli/commands/help.rb +100 -0
  17. data/lib/chimps-cli/commands/list.rb +48 -0
  18. data/lib/chimps-cli/commands/me.rb +30 -0
  19. data/lib/chimps-cli/commands/post.rb +33 -0
  20. data/lib/chimps-cli/commands/put.rb +33 -0
  21. data/lib/chimps-cli/commands/query.rb +58 -0
  22. data/lib/chimps-cli/commands/search.rb +54 -0
  23. data/lib/chimps-cli/commands/show.rb +40 -0
  24. data/lib/chimps-cli/commands/test.rb +37 -0
  25. data/lib/chimps-cli/commands/update.rb +38 -0
  26. data/lib/chimps-cli/commands/upload.rb +86 -0
  27. data/lib/chimps-cli/utils.rb +13 -0
  28. data/lib/chimps-cli/utils/acts_on_resource.rb +93 -0
  29. data/lib/chimps-cli/utils/explicit_path.rb +30 -0
  30. data/lib/chimps-cli/utils/http_format.rb +51 -0
  31. data/lib/chimps-cli/utils/uses_param_value_data.rb +90 -0
  32. data/spec/chimps-cli/commands/delete_spec.rb +9 -0
  33. data/spec/chimps-cli/commands/get_spec.rb +8 -0
  34. data/spec/chimps-cli/commands/help_spec.rb +18 -0
  35. data/spec/chimps-cli/commands/list_spec.rb +7 -0
  36. data/spec/chimps-cli/commands/post_spec.rb +10 -0
  37. data/spec/chimps-cli/commands/put_spec.rb +10 -0
  38. data/spec/chimps-cli/commands/show_spec.rb +7 -0
  39. data/spec/spec_helper.rb +52 -0
  40. data/spec/support/acts_on_resource.rb +22 -0
  41. data/spec/support/explicit_path.rb +42 -0
  42. data/spec/support/http_format.rb +23 -0
  43. data/spec/support/uses_param_value_data.rb +88 -0
  44. metadata +166 -0
@@ -0,0 +1,48 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ # A command to issue a GET request against an index of resources
5
+ # at Infochimps.
6
+ class List < Chimps::Command
7
+
8
+ include Chimps::Utils::HttpFormat
9
+ include Chimps::Utils::ActsOnResource
10
+
11
+ def self.default_response_fmt
12
+ 'tsv'
13
+ end
14
+
15
+ def self.allowed_response_fmts
16
+ super().concat(['tsv'])
17
+ end
18
+
19
+ def self.allowed_resources
20
+ %w[collection dataset source license]
21
+ end
22
+
23
+ USAGE = "usage: chimps [OPTIONS] list [RESOURCE]"
24
+ HELP = <<EOF
25
+
26
+ List #{default_resources_type} on Infochimps.
27
+
28
+ #{resources_listing}
29
+
30
+ Examples:
31
+
32
+ $ chimps list # list all datasets
33
+ $ chimps list --my # list your datasets
34
+ $ chimps list licenses # list licenses
35
+ $ chimps list --my sources # list your sources
36
+ EOF
37
+
38
+ # Issue the GET request.
39
+ def execute!
40
+ query_params = {}
41
+ query_params[:skip_header] = true if config[:skip_column_names]
42
+ Request.new(resources_path, :query_params => query_params, :sign => config[:my]).get(headers).print(config)
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+
@@ -0,0 +1,30 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ class Me < Chimps::Command
5
+
6
+ include Chimps::Utils::HttpFormat
7
+
8
+ def self.default_response_fmt
9
+ 'yaml'
10
+ end
11
+
12
+ USAGE = "usage: chimps me [OPTIONS]"
13
+ HELP = <<EOF
14
+
15
+ Show a summary of your account.
16
+
17
+ Examples:
18
+
19
+ $ chimps me
20
+ $ chimps me --response_format=json
21
+ EOF
22
+
23
+ def execute!
24
+ Chimps::Request.new("/me" + ".#{response_fmt}", :sign => true).get.print
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,33 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ class Post < Chimps::Command
5
+
6
+ include Chimps::Utils::HttpFormat
7
+ include Chimps::Utils::ExplicitPath
8
+ include Chimps::Utils::UsesParamValueData
9
+
10
+ USAGE = "usage: chimps post [OPTIONS] PATH [PARAM=VALUE] ..."
11
+ HELP = <<EOF
12
+
13
+ Send a GET request to the given PATH at Infochimps.
14
+
15
+ Examples:
16
+
17
+ # create a dataset
18
+ $ chimps post /datasets title="List of Chimps" description="Awesome..."
19
+
20
+ # create a source
21
+ $ chimps post /sources --data=/path/to/some/metadata.json
22
+ EOF
23
+
24
+ def execute!
25
+ response = Chimps::Request.new(path, :query_params => query_params, :data => (data || true), :sign => config[:sign]).post(headers)
26
+ response.print_headers if config[:headers]
27
+ response.print
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,33 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ class Put < Chimps::Command
5
+
6
+ include Chimps::Utils::HttpFormat
7
+ include Chimps::Utils::ExplicitPath
8
+ include Chimps::Utils::UsesParamValueData
9
+
10
+ USAGE = "usage: chimps put [OPTIONS] PATH [PARAM=VALUE] ..."
11
+ HELP = <<EOF
12
+
13
+ Send a GET request to the given PATH at Infochimps.
14
+
15
+ Examples:
16
+
17
+ # update a dataset
18
+ $ chimps put /datasets/my-dataset title="A new title"
19
+
20
+ # update a source
21
+ $ chimps put /sources/my-source --data=/path/to/new/metadata.yaml
22
+ EOF
23
+
24
+ def execute!
25
+ response = Chimps::Request.new(path, :query_params => query_params, :data => (data || true), :sign => config[:sign]).put(headers)
26
+ response.print_headers if config[:headers]
27
+ response.print
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,58 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ # A command to issue a GET request against the Infochimps paid
5
+ # query API.
6
+ class Query < Chimps::Command
7
+
8
+ include Chimps::Utils::UsesParamValueData
9
+
10
+ USAGE = "usage: chimps query [OPTIONS] ENDPOINT [PROP=VALUE] ..."
11
+ HELP = <<EOF
12
+
13
+ Send a query to the given ENDPOINT on the Infochimps Query API with the
14
+ given parameters.
15
+
16
+ #{how_to_input_data}
17
+ You can learn more about the Infochimps query API, discover datasets
18
+ to query, and look up the available parameters at
19
+
20
+ http://api.infochimps.com
21
+
22
+ Examples:
23
+
24
+ $ chimps query soc/net/tw/trstrank screen_name=infochimps
25
+ $ chimps query soc/net/tw/trstrank?screen_name=infochimps
26
+ $ chimps query web/an/de/demographics ip=146.6.180.1
27
+ $ chimps query soc/net/tw/trstrank --data=trstrank_queries.yml
28
+ EOF
29
+
30
+ def endpoint
31
+ raise CLIError.new(self.class::USAGE) unless config.argv.size > 1 && (! config.argv[1].empty?)
32
+ config.argv[1]
33
+ end
34
+
35
+ def requests
36
+ # ensure_data_is_present! unless endpoint.include?("?")
37
+ if data.is_a?(Hash)
38
+ [QueryRequest.new(endpoint, :query_params => data, :sign => true)]
39
+ else
40
+ data.map { |params| QueryRequest.new(endpoint, :query_params => params, :sign => true) }
41
+ end
42
+ end
43
+
44
+ def execute!
45
+ requests.each do |request|
46
+ response = request.get
47
+ if response.error?
48
+ response.print :to => $stderr
49
+ else
50
+ puts(config[:pretty] ? JSON.pretty_generate(JSON.parse(response.body)) : response.body)
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,54 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ class Search < Chimps::Command
5
+
6
+ include Chimps::Utils::HttpFormat
7
+ include Chimps::Utils::ActsOnResource
8
+
9
+ def self.allowed_resources
10
+ %w[dataset]
11
+ end
12
+
13
+ def self.allowed_response_fmts
14
+ super().concat(['tsv'])
15
+ end
16
+
17
+ def self.default_response_fmt
18
+ 'tsv'
19
+ end
20
+
21
+ USAGE = "usage: chimps search [OPTIONS] QUERY"
22
+ HELP = <<EOF
23
+
24
+ Search for #{default_resources_type} on Infochimps for the given
25
+ QUERY.
26
+
27
+ Examples:
28
+
29
+ $ chimps search music # search all datasets for 'music'
30
+ $ chimps search --my 'finance AND nyse' # search your datasets for 'finance' and 'nyse'
31
+ EOF
32
+
33
+ def query
34
+ raise CLIError.new(self.class::USAGE) if config.rest.size < 2
35
+ config.rest[1..-1].join(' ')
36
+ end
37
+
38
+ def query_params
39
+ {:query => query}.tap do |qp|
40
+ qp[:skip_header] = true if config[:skip_column_names]
41
+ end
42
+ end
43
+
44
+ def path
45
+ config[:my] ? "/my/search.#{response_fmt}" : "/search.#{response_fmt}"
46
+ end
47
+
48
+ def execute!
49
+ Request.new(path, :query_params => query_params, :sign => config[:my]).get(headers).print(config)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,40 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ class Show < Chimps::Command
5
+
6
+ include Chimps::Utils::ActsOnResource
7
+ include Chimps::Utils::HttpFormat
8
+
9
+ def self.default_response_fmt
10
+ 'yaml'
11
+ end
12
+
13
+ def self.allowed_resources
14
+ %w[dataset collection source license user]
15
+ end
16
+
17
+ USAGE = "usage: chimps show [OPTIONS] [RESOURCE] SLUG_OR_ID"
18
+ HELP = <<EOF
19
+
20
+ Return a description of the #{default_resource_type} with the given
21
+ ID or or slug.
22
+
23
+ #{resources_listing}
24
+
25
+ Examples:
26
+
27
+ $ chimps show musicbrainz # show musicbrainz dataset
28
+ $ chimps show 11598 # show musicbrainz dataset
29
+ $ chimps show source musicbrainzorg # show musicbrainz.org source page
30
+ $ chimps show user Infochimps # show Infochimps' user page
31
+ EOF
32
+
33
+ def execute!
34
+ puts Chimps::Request.new(resource_path + ".#{response_fmt}").get.body
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,37 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ class Test < Chimps::Command
5
+
6
+ USAGE = "usage: chimps [OPTIONS] test"
7
+ HELP = <<EOF
8
+
9
+ Print diagnostic information on the API credentials being used by chimps
10
+ and send a test request to Infochimps to make sure the API credentials
11
+ work.
12
+ EOF
13
+
14
+ def execute!
15
+ dataset_response = Chimps::Request.new("/me", :sign => true).get
16
+ query_response = Chimps::QueryRequest.new("soc/net/tw/trstrank", :query => {:screen_name => :infochimps}).get
17
+
18
+ if dataset_response.error?
19
+ puts "Could not authenticate with Infochimps Dataset API."
20
+ else
21
+
22
+ dataset_response.parse!
23
+ username = dataset_response['user']['username']
24
+ puts "Authenticated as user '#{username}' for Infochimps Dataset API at http://www.infochimps.com"
25
+ end
26
+
27
+ if query_response.error?
28
+ puts "Could not authenticate with Infochimps Query API."
29
+ else
30
+ puts "Authenticated for Infochimps Query API at http://api.infochimps.com"
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,38 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ # A command to issue a PUT request to update a resource at
5
+ # Infochimps.
6
+ class Update < Chimps::Command
7
+
8
+ include Chimps::Utils::ActsOnResource
9
+ include Chimps::Utils::UsesParamValueData
10
+
11
+ def self.allowed_models
12
+ %w[dataset source license]
13
+ end
14
+
15
+ USAGE = "usage: chimps update [OPTIONS] [RESOURCE] ID_OR_SLUG [PROP=VALUE] ..."
16
+ HELP = <<EOF
17
+
18
+ Updates a #{default_resource_type} identified by the given ID or slug using the properties
19
+ and values supplied.
20
+
21
+ #{how_to_input_data}
22
+ #{resources_listing}
23
+
24
+ Examples:
25
+
26
+ $ chimps update my-awesome-dataset title='Yet More Awesome Dataset' description="It is even cooler"
27
+ $ chimps update source my-source -d my_source.yml
28
+ EOF
29
+
30
+ def execute!
31
+ ensure_data_is_present!
32
+ Request.new(resource_path, :body => {resource_type => data } , :sign => true).put.print
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,86 @@
1
+ module Chimps
2
+ module Commands
3
+
4
+ class Upload < Chimps::Command
5
+
6
+ USAGE = "usage: chimps upload [OPTIONS] ID_OR_SLUG [PATH|LINK] ..."
7
+ HELP = <<EOF
8
+
9
+ Create, modify, inspect, or submit an upload for the dataset with the
10
+ given ID or slug on Infochimps.
11
+
12
+ Check the status of the current upload for 'my-dataset'
13
+
14
+ $ chimps upload my-dataset
15
+
16
+ If this returns a "404" then it just means that the upload does not
17
+ exist yet. You will also see whether the upload has started or not.
18
+
19
+ Assuming the upload hasn't started, you can add files from your local
20
+ machine or links that you want Infochimps to scrape:
21
+
22
+ $ chimps upload my-dataset /path/to/some/data.tsv
23
+ $ chimps upload my-dataset http://www.cooldata.com/some/great/data.html
24
+ $ chimps upload my-dataset s3://mybucket/some/open/data.xls
25
+
26
+ You can also pass more than one local file or link in each call to
27
+ `chimps upload' if you wish.
28
+
29
+ When you're ready to have Infochimps start processing the upload run
30
+
31
+ $ chimps upload --start my-dataset
32
+
33
+ You can watch your upload's status by running
34
+
35
+ $ chimps upload my-dataset
36
+
37
+ at any time.
38
+ EOF
39
+
40
+ def upload
41
+ return @upload if @upload
42
+ raise CLIError.new(self.class::USAGE) if config.argv.size < 2 or config.argv[1].blank?
43
+ @upload = Chimps::Upload.new(config.argv[1])
44
+ end
45
+
46
+ def objs_to_upload
47
+ config.argv[2..-1] if config.argv.size > 2
48
+ end
49
+
50
+ def execute!
51
+ case
52
+ when config[:create]
53
+ upload.create # don't print b/c output is messy
54
+ when config[:start]
55
+ upload.start.print
56
+ when config[:restart]
57
+ upload.restart.print
58
+ when config[:destroy]
59
+ upload.destroy.print
60
+ when objs_to_upload
61
+ links = []
62
+ upload.create if upload.show.error?
63
+ objs_to_upload.each do |obj|
64
+ if File.exist?(obj)
65
+ tok = upload.upload_token
66
+ upload.upload_file(obj, tok)
67
+ else
68
+ validate_link(obj)
69
+ links << obj
70
+ end
71
+ upload.create_links(*links) unless links.empty?
72
+ end
73
+ else
74
+ upload.show.print
75
+ end
76
+ end
77
+
78
+ def validate_link link
79
+ scheme = link.split("://").first
80
+ raise UploadError.new("Infochimps only accepts links with a URI scheme (prefix) of http, https, ftp, or s3 -- you gave #{scheme}") unless ['http', 'https', 's3','ftp'].include?(scheme)
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+