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
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ source :gemcutter
3
+ gem "chimps", ">= 0.3.1"
4
+ gem "configliere", ">= 0.3.2"
5
+ gem "json"
6
+ group :test do
7
+ gem "rspec"
8
+ end
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.4)
5
+ chimps (0.3.1)
6
+ addressable
7
+ configliere (>= 0.3.2)
8
+ json
9
+ rest-client (>= 1.6.1)
10
+ configliere (0.3.2)
11
+ diff-lcs (1.1.2)
12
+ json (1.5.1)
13
+ mime-types (1.16)
14
+ rest-client (1.6.1)
15
+ mime-types (>= 1.16)
16
+ rspec (2.5.0)
17
+ rspec-core (~> 2.5.0)
18
+ rspec-expectations (~> 2.5.0)
19
+ rspec-mocks (~> 2.5.0)
20
+ rspec-core (2.5.1)
21
+ rspec-expectations (2.5.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-mocks (2.5.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ chimps (>= 0.3.1)
30
+ configliere (>= 0.3.2)
31
+ json
32
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Dhruv Bansal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,322 @@
1
+ = Chimps on the Command Line
2
+
3
+ Infochimps[http://www.infochimps.com] is an online data marketplace
4
+ and repository where anyone can find, share, and sell data.
5
+
6
+ Infochimps offers two APIs for users to access and modify data
7
+
8
+ - a {Dataset API}[http://www.infochimps.com/apis] to list, show, create, update, and destroy datasets and associated resources on Infochimps[http://www.infochimps.com]
9
+ - a {Query API}[http://www.infochimps.com/apis] to query data from particular rows of these datasets
10
+
11
+ Chimps[http://github.com/infochimps/chimps-cli] is a Ruby wrapper for
12
+ these APIs that makes interacting with them simple. You can embed
13
+ Chimps inside your web application or any other software you write.
14
+
15
+ But if you finding yourself wishing that you could make queries,
16
+ create datasets, &c. from your command line, where you already live,
17
+ where you already keep your data...then Chimps CLI is for you:
18
+
19
+ # See your datasets
20
+ $ chimps list --my
21
+ ...
22
+
23
+ # Create a new dataset
24
+ $ chimps create title="A Brand New Dataset" description="That I created in 2 minutes. But that doesn't mean it's not awesome."
25
+ ...
26
+
27
+ # Take a look
28
+ $ chimps show a-brand-new-dataset
29
+
30
+ # Check out your competition
31
+ $ chimps search awesome data
32
+ ...
33
+
34
+ # Hmmm. Better do some more work.
35
+ $ chimps update a-brand-new-dataset tag_list="awesome,new,data"
36
+
37
+ = First Steps
38
+
39
+ == Installing Chimps CLI
40
+
41
+ Assuming you've already set up your Gem sources, just run
42
+
43
+ gem install chimps-cli
44
+
45
+ This will also install Chimps[http://github.com/infochimps/chimps] if
46
+ it's not already present on your system.
47
+
48
+ == Configuring Chimps
49
+
50
+ Chimps CLI is just a command-line wrapper for the Chimps library. If
51
+ Chimps is already properly configured with your API credentials then
52
+ Chimps CLI will read them just fine without you having to do anything.
53
+
54
+ If you need to obtain API keys for either the Dataset API or the Query
55
+ API then {sign up at Infochimps}[http://www.infochimps.com/signup].
56
+
57
+ You'll need to put your API keys into one of two files, either
58
+ <tt>/etc/chimps/chimps.yaml</tt> or <tt>~/.chimps</tt>. See the
59
+ README for Chimps[http://github.com/infochimps/chimps] for more
60
+ details on how to set up these configuration files.
61
+
62
+ = Usage
63
+
64
+ Try running
65
+
66
+ $ chimps help
67
+
68
+ to make sure you can run the +chimps+ command and to see an overview
69
+ of what subcommands are available. You can get more detailed help as
70
+ well as example usage on COMMAND by running
71
+
72
+ $ chimps help COMMAND
73
+
74
+ You can test and see whether your credentials are valid using the
75
+ +test+ command:
76
+
77
+ $ chimps test
78
+ Authenticated as user 'Infochimps' for Infochimps Dataset API at http://www.infochimps.com
79
+ Authenticated for Infochimps Query API at http://api.infochimps.com
80
+
81
+ If you get messages about missing keys and so on go back and read the
82
+ {Chimps installation
83
+ instructions}[http://github.com/infochimps/chimps].
84
+
85
+ If you get messages about not being able to authenticate, double-check
86
+ that the API keys in your configuration file (either
87
+ <tt>~/.chimps</tt> or <tt>/etc/chimps/chimps.yaml</tt>) match the
88
+ credentials listed in your profile[http://www.infochimps.com/me].
89
+
90
+ == Options
91
+
92
+ Commands to +chimps+ accept arguments as well as options. Options
93
+ always begin with two dashes and some options have single-letter flags
94
+ as well.
95
+
96
+ Some options work for every +chimps+ command. <tt>--verbose</tt>
97
+ (<tt>-v</tt>), for example, is a great way to see what underlying HTTP
98
+ request(s) a given command is making.
99
+
100
+ == Operating on a Dataset, Source, License, &c.
101
+
102
+ Many requests can operate on a particular resource. The +show+
103
+ command, for example, can be used to show a dataset (the default
104
+ choice), a license, a source, or a user.
105
+
106
+ You can see what resources +COMMAND+ can operate on with <tt>chimps
107
+ help COMMAND</tt>. Two examples
108
+
109
+ # Will attempt to show the Dataset 'an-example'
110
+ $ chimps show an-example
111
+
112
+ # Will attempt to show the Source 'an-example'
113
+ $ chimps show source an-example
114
+
115
+ # Will search datasets for 'stocks'
116
+ $ chimps search stocks
117
+
118
+ # Will list all licenses
119
+ $ chimps list licenses
120
+
121
+ == Providing Data to a Command
122
+
123
+ Some commands (typically those that result in HTTP +GET+ and +DELETE+
124
+ requests) don't require you to pass any data to Infochimps.
125
+
126
+ Other commands (typically those that result in HTTP +POST+ and +PUT+
127
+ requests) do. These commands usually create or modify a dataset or
128
+ other resource at Infochimps.
129
+
130
+ Say you wanted to create a new dataset on Infochimps with the title
131
+ "List of hottest Salsas" and with description "All salsas were tried
132
+ personally by me."
133
+
134
+ There are two methods you can use to pass this data to a Chimps CLI
135
+ command:
136
+
137
+ 1) You can put the data you need to pass into a file on disk. Chimps
138
+ understands YAML and JSON files formats and will automatically parse
139
+ and serialize them properly when making a request. You could create
140
+ the following file
141
+
142
+ # in salsa_dataset.yml
143
+ ---
144
+ title: "List of hottest Salsas"
145
+ description: |-
146
+
147
+ All salsas were tried personally by me.
148
+
149
+ and you can create the dataset with
150
+
151
+ $ chimps create --data=salsa_dataset.yml
152
+
153
+ 2) You can pass parameters and values directly on the command line.
154
+ You could create the same dataset as above with
155
+
156
+ $ chimps create title="List of hottest Salsas" description="All salsas were tried personally by me."
157
+
158
+ This will only work for a flat collection of parameters and values, as
159
+ in this example. If you need to pass a nested data structure you
160
+ should use a file and the <tt>--data</tt> option above.
161
+
162
+ Another example, which makes a query to the Query API and returns
163
+ demographics on an IP address
164
+
165
+ $ chimps query web/an/ip_census ip=67.78.118.7
166
+
167
+ = Basic HTTP Verbs
168
+
169
+ Infochimps' Dataset API is RESTful so it respects the semantics of
170
+ {HTTP
171
+ verbs}[http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods].
172
+ You can use this "lower-level" interface to make simple +GET+, +POST+,
173
+ +PUT+, and +DELETE+ requests.
174
+
175
+ Here's how to return information on a Yahoo! Stocks dataset
176
+
177
+ $ chimps get /datasets/yahoo-stock-search
178
+
179
+ The default response will be in JSON but you can change the response
180
+ format by explicitly passing a different one of +xml+, +json+, or
181
+ +yaml+. This works for (almost) all Dataset API requests.
182
+
183
+ $ chimps get /datasets/yahoo-stock-search --response_format=yaml
184
+
185
+ Try running <tt>chimps help</tt> for the +get+ command (<tt>chimps
186
+ help get</tt>) as well as for the +post+, +put+, and +delete+
187
+ commands.
188
+
189
+ == Signed vs. Unsigned Requests
190
+
191
+ Some requests, like the +GET+ request above, don't need to be signed
192
+ in any way: using +chimps+ to make a simple unsigned +GET+ request
193
+ isn't anything different than just doing it with +curl+.
194
+
195
+ All +POST+, +PUT+, and +DELETE+ requests, however, need to be signed
196
+ and using Chimps to do it makes it easy. Here's how you might create
197
+ a dataset
198
+
199
+ $ chimps post --sign /datasets title="My dataset" description="Some text..."
200
+
201
+ If you leave out the <tt>--sign</tt> option then the request will fail
202
+ with a 401 Authentication error.
203
+
204
+ The above request is really just the same as
205
+
206
+ $ chimps create title="My dataset" description="Some text..."
207
+
208
+ which is a little simple because +create+ understands what you're
209
+ trying to do and internally constructs the appropriate +POST+ request.
210
+
211
+ You can find a list of all available requests, the correct HTTP verb
212
+ to use, whether the request needs to be signed, and what parameters it
213
+ accepts at http://www.infochimps.com/apis.
214
+
215
+ To round out this section, here's an example of a +PUT+ request and a
216
+ +DELETE+ request (both of which must be signed):
217
+
218
+ # Update your existing dataset
219
+ $ chimps put --sign /datasets/my-dataset title="A New Title"
220
+
221
+ # Let's delete this dataset now because we're fickle little monkeys...
222
+ $ chimps delete --sign /datasets/my-dataset
223
+
224
+ Most things you might want to do with this "low-level" HTTP verb
225
+ interface can be done with specialized +chimps+ commands. Read on.
226
+
227
+ = Core REST Actions
228
+
229
+ Since the Infochimps Dataset API is
230
+ RESTful[http://en.wikipedia.org/wiki/Representational_State_Transfer],
231
+ it implements list, show, create, update, and destroy actions for all
232
+ resources. Each of these actions has a corresponding Chimps command.
233
+
234
+ Here's how to +list+ datasets:
235
+
236
+ $ chimps list
237
+
238
+ The +list+ command is one of a few (+search+ being another) that
239
+ accepts the <tt>--my</tt> (<tt>-m</tt>) option. This will restrict
240
+ the output to only datasets (or whatever resource you're listing) that
241
+ are owned by you.
242
+
243
+ $ chimps list --my
244
+ $ chimps list --my licenses
245
+
246
+ Here's how to +show+ a dataset:
247
+
248
+ $ chimps show my-dataset
249
+
250
+ this returns YAML by default but you can specify a different response
251
+ format by passing the <tt>--response_format</tt> option
252
+
253
+ $ chimps show my-dataset --response_format=json
254
+
255
+ You've already seen +create+ in action a few times so here's +update+
256
+ instead
257
+
258
+ $ chimps update my-dataset title="A new title"
259
+
260
+ And of course +destroy+
261
+
262
+ $ chimps destroy my-dataset
263
+
264
+ If you're curious about the underlying HTTP requests being sent, try
265
+ running these commands with the <tt>--verbose</tt> (<tt>-v</tt>) flag.
266
+
267
+ = Special Requests
268
+
269
+ Chimps CLI has a few special commands which aren't HTTP verbs or core
270
+ REST actions.
271
+
272
+ == Search
273
+
274
+ Here's how to search Infochimps for datasets about music:
275
+
276
+ $ chimps search music
277
+
278
+ Here's the same search restricted to only datasets you own and
279
+ pretty-printed:
280
+
281
+ $ chimps search --my music --pretty
282
+
283
+ == Download
284
+
285
+ If a dataset on Infochimps has a downloadable package then the
286
+ +download+ command can be used to download the data:
287
+
288
+ $ chimps download daily-1970-2010-open-close-hi-low-and-volume-nyse-exchange
289
+
290
+ The dataset must be free, you must own it, or you must have purchased
291
+ it (through the website) before you can +download+ it with Chimps.
292
+
293
+ You may want to include the <tt>--verbose</tt> (<tt>-v</tt>) flag so
294
+ that you can see the progress of the download, especially if it is a
295
+ large file.
296
+
297
+ == Upload
298
+
299
+ Infochimps does not presently allow you to upload data by using an
300
+ API. Please create a dataset first (you can do this with Chimps) and
301
+ then go to that dataset's page in a browser and upload any data you
302
+ wish.
303
+
304
+ This feature will be coming very, very soon!
305
+
306
+ == Help/Test
307
+
308
+ <tt>chimps help</tt> and <tt>chimps help COMMAND</tt> should carry you
309
+ a good ways with the examples and usage they output.
310
+
311
+ <tt>chimps test</tt> should confirm that your API keys are properly
312
+ configured.
313
+
314
+ = Contributing
315
+
316
+ Chimps CLI is an open source project created by the Infochimps team to
317
+ encourage adoption of the Infochimps APIs. The official repository is
318
+ hosted on GitHub
319
+
320
+ http://github.com/infochimps/chimps-cli
321
+
322
+ Feel free to clone it and send pull requests.
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ # http://github.com/technicalpickles/jeweler
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "chimps-cli"
9
+ gem.summary = "Chimps CLI is command-line interface for the Chimps gem (http://www.infochimps.com/labs)"
10
+ gem.description = "Chimps CLI provides the command line program 'chimps' which allows you to easily make API calls against Infochimps web services."
11
+ gem.email = "coders@infochimps.com"
12
+ gem.homepage = "http://github.com/infochimps/chimps-cli"
13
+ gem.authors = ["Dhruv Bansal"]
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ desc "Build tags"
21
+ task :tags do
22
+ system "etags -R README.rdoc bin lib spec"
23
+ end
24
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib') unless $:.include?(File.dirname(__FILE__) + '/../lib')
3
+ require 'chimps-cli'
4
+ exit Chimps::CLI.execute!
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path('../Gemfile', File.dirname(__FILE__))
3
+ require 'bundler/setup'
4
+ require 'chimps'
5
+ require 'chimps-cli/utils'
6
+ require 'chimps-cli/commands'
7
+
8
+ # FIXME add bundler once new chimps is published
9
+
10
+ module Chimps
11
+
12
+ autoload :Command, 'chimps-cli/commands/base'
13
+
14
+ # Defines methods for choosing which Chimps::Command class should be
15
+ # instantiated from the ARGV passed in on the command line.
16
+ module CLI
17
+
18
+ include Chimps::Commands
19
+
20
+ # Execute the Chimps command specified on the command line.
21
+ #
22
+ # Will exit the Ruby process with 0 on success or 1 on an error.
23
+ def self.execute!
24
+ begin
25
+ Chimps.boot!
26
+ if command
27
+ Chimps.config.command_settings.resolve!
28
+ command.execute!
29
+ return 0
30
+ else
31
+ $stderr.puts Chimps.config.usage
32
+ Chimps.config.dump_help
33
+ return 1
34
+ end
35
+ rescue Chimps::Error, Configliere::Error => e
36
+ $stderr.puts e.message
37
+ return 1
38
+ rescue => e
39
+ $stderr.puts("#{e.message} (#{e.class})")
40
+ $stderr.puts(e.backtrace.join("\n"))
41
+ return 2
42
+ end
43
+ end
44
+ end
45
+ end
46
+