slapshot 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/bin/slapshot CHANGED
@@ -8,18 +8,12 @@ program_desc 'A command line interface to the Appshot API'
8
8
 
9
9
  #version Slapshot::VERSION
10
10
 
11
- desc 'Your Appshot url'
11
+ desc 'Your Appshot url'
12
12
  flag [:url]
13
13
 
14
14
  desc 'Your Appshot instance'
15
15
  flag [:i,:instance]
16
16
 
17
- desc 'Your Appshot username'
18
- flag [:u,:username]
19
-
20
- desc 'Your Appshot password'
21
- flag [:p,:password]
22
-
23
17
  desc 'Search your instance of Appshot'
24
18
  arg_name 'Pass in the query'
25
19
  command :search do |c|
@@ -63,19 +57,70 @@ command :upload do |c|
63
57
  end
64
58
  end
65
59
 
60
+ desc 'Login to the API and generate a token'
61
+ arg_name 'Pass in the username and password to login and generate a token'
62
+
63
+ command :login do |c|
64
+
65
+ c.desc 'Username (required)'
66
+ c.flag :u
67
+
68
+ c.desc 'Password (required)'
69
+ c.flag :p
70
+
71
+ c.action do |global_options,options,args|
72
+
73
+ url = global_options[:url] || ENV['APPSHOT_URL'] || "https://api.appshot.com"
74
+
75
+ if !options[:u] or !options[:p]
76
+ raise "When logging in you need to pass in both a username and a password"
77
+ end
78
+
79
+ if !File.exists? get_token_file
80
+
81
+ token = create_token(url, options[:u], options [:p])
82
+ puts "Generated token: #{token} and stored it in #{get_token_file}"
83
+
84
+ else
85
+ puts "Found existing file at #{get_token_file}"
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ desc 'Logout to the API, disactivate and remove the token'
93
+ command :logout do |c|
94
+
95
+ c.action do |global_options,options,args|
96
+
97
+ url = global_options[:url] || ENV['APPSHOT_URL'] || "https://api.appshot.com"
98
+
99
+ if File.exists? get_token_file
100
+
101
+ token = remove_token(url)
102
+ puts "Disactivated token: #{token} and removed #{get_token_file}"
103
+
104
+ else
105
+ puts "You are not logged in."
106
+ end
107
+
108
+ end
109
+
110
+ end
111
+
112
+
66
113
  pre do |global,command,options,args|
67
- # Pre logic here
68
- # Return true to proceed; false to abort and not call the
69
- # chosen command
70
- # Use skips_pre before a command to skip this block
71
- # on that command only
72
-
73
- url = global[:url] || ENV['APPSHOT_URL'] || "https://api.appshot.com"
74
- instance = global[:i] || ENV['APPSHOT_INSTANCE']
75
- username = global[:u] || ENV['APPSHOT_USERNAME']
76
- password = global[:p] || ENV['APPSHOT_PASSWORD']
77
-
78
- @sc = SlapshotConnection.new url, instance, username, password
114
+
115
+ url = global[:url] || ENV['APPSHOT_URL'] || "https://api.appshot.com"
116
+
117
+ if command.names != 'login' and command.names != 'logout'
118
+
119
+ instance = global[:i] || ENV['APPSHOT_INSTANCE']
120
+
121
+ @sc = Appshot.new url, get_token, instance
122
+
123
+ end
79
124
 
80
125
  true
81
126
  end
@@ -89,6 +134,9 @@ end
89
134
  on_error do |exception|
90
135
  # Error logic here
91
136
  # return false to skip default error handling
137
+
138
+ #puts "exception #{exception.http_body}"
139
+
92
140
  true
93
141
  end
94
142
 
data/lib/slapshot/main.rb CHANGED
@@ -3,12 +3,50 @@ require 'json'
3
3
  require 'formatador'
4
4
  require 'spreadsheet'
5
5
 
6
- class SlapshotConnection
6
+ def get_token_file
7
+ homes = ["HOME", "HOMEPATH"]
8
+ realHome = homes.detect {|h| ENV[h] != nil}
9
+ ENV[realHome] + '/.slapshot_token' unless !realHome
10
+ end
11
+
12
+ def create_token(url, username, password)
13
+ token = RestClient.get url + '/login', :params => { :u => username, :p => password }
14
+
15
+ File.open(get_token_file, 'w') {|f|
16
+ f.write(token)
17
+ }
7
18
 
8
- def initialize(url, instance, username, password)
19
+ token
20
+ end
21
+
22
+ def remove_token(url)
23
+
24
+ token = get_token
25
+
26
+ RestClient.get url + '/logout', :params => { :t => token }
27
+
28
+ File.delete(get_token_file)
29
+
30
+ token
31
+
32
+ end
33
+
34
+ def get_token
35
+
36
+ if not File.exists? get_token_file
37
+ raise "Could not find a token file to read from"
38
+ end
39
+
40
+ File.open(get_token_file, 'r').read
41
+ end
42
+
43
+ class Appshot
44
+
45
+ def initialize(url, token, instance)
9
46
  @app = "Portfolio"
10
47
  @instance = instance
11
- @api = RestClient::Resource.new url, :user => username, :password => password, :timeout => -1
48
+
49
+ @api = RestClient::Resource.new url, :timeout => -1, :headers => {'Appshot-AuthToken' => token, 'Appshot-Instance' => instance}
12
50
  end
13
51
 
14
52
  def set_app(app)
@@ -17,9 +55,10 @@ class SlapshotConnection
17
55
 
18
56
  def search(query, count)
19
57
 
20
- response = @api["search"].get :params => { :i => @instance, :app => @app, :q => query, :c => count }, :content_type => :json, :accept => :json
58
+ response = @api["v1/search"].get :params => { :i => @instance, :app => @app, :q => query, :c => count }, :content_type => :json, :accept => :json
21
59
 
22
60
  json_response = JSON.parse(response.body)
61
+
23
62
  puts "Found #{json_response['count']} total results, retrieving #{count} results in #{json_response['search_time']} ms."
24
63
 
25
64
  json_response['docs']
@@ -34,7 +73,7 @@ class SlapshotConnection
34
73
  book = Spreadsheet::Workbook.new
35
74
  sheet1 = book.create_worksheet :name => 'Search Results'
36
75
 
37
- columns = ['name', 'entity_type', 'highlight']
76
+ columns = ['name', 'entity_type']
38
77
 
39
78
  rownum = 0
40
79
  for column in columns
@@ -47,7 +86,6 @@ class SlapshotConnection
47
86
  end
48
87
  end
49
88
  book.write(out)
50
-
51
89
  end
52
90
 
53
91
  end
@@ -1,3 +1,3 @@
1
1
  module Slapshot
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-24 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake