lolcommits-lolsrv 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e50c83486cf5146e9f9c96e853e934aa90da4af3
4
- data.tar.gz: 02a923be72368002ec890aeb83211a7e580d343d
3
+ metadata.gz: 331ae4bed89222c2ed6c6987a550c087f722ee9f
4
+ data.tar.gz: ed0f42d81741114b3d346bf985f1333285427d81
5
5
  SHA512:
6
- metadata.gz: f9f54506b3ed4a07f69ff448272bc09bcdd75cacdbafe4a3fb9cb5377863a89bde24bcadc4c62c6fc88e7577b4acb3462b870f2aeda7ac67ac20118c3fcae968
7
- data.tar.gz: eb8d7672a36f12d0dcf8230e28412a0a82f09503f9a385ba5a09eaa18dd98ef0456cbc73d2e1cdd3f2e8990b313662ac3cf0727263ae8c4ffbb4ad18ddb7dbb3
6
+ metadata.gz: 5766e45c68caa163fab9edcefa683fe235809469c4c5a8906e47c4f78c26ea548137a4408f2db9a30cfbee3ce988ef4ad3b3c3dcad09928bc8b14bd7e72e00cc
7
+ data.tar.gz: acd9396d02f088c4facf10136f7d18bb5e068cfb2952da0980748863500df2469f1f23c8efcb20f4b599e827af86da21bf4d6a5c5330f6a78526fd9ebdedb826
data/README.md CHANGED
@@ -16,11 +16,16 @@ you've already captured. Syncing is then performed after each commit, only
16
16
  uploading images that have not already been synced.
17
17
 
18
18
  You configure the plugin by setting the base url of the remote server. The
19
- server must respond to these requests.
19
+ server must respond at these paths:
20
+
21
+ **GET /lols**
22
+
23
+ Returns a JSON array of all lolcommits already synced. The commit `sha` is the
24
+ only required JSON attribute (used to identify the already synced image).
20
25
 
21
26
  **POST /uplol**
22
27
 
23
- These params are submitted as `multipart/form-data`.
28
+ The following upload params are `multipart/form-data` encoded:
24
29
 
25
30
  * `lol` - captured lolcommit image file
26
31
  * `url` - remote repository URL (with commit SHA appended)
@@ -28,11 +33,6 @@ These params are submitted as `multipart/form-data`.
28
33
  * `date` - UTC date time for the commit (ISO8601)
29
34
  * `sha` - commit SHA
30
35
 
31
- **GET /lols**
32
-
33
- Should return a JSON array of all lolcommits already synced. The commit `sha` is the
34
- only required JSON attribute (used to identify the already synced image).
35
-
36
36
  ## Requirements
37
37
 
38
38
  * Ruby >= 2.0.0
@@ -1,5 +1,5 @@
1
1
  module Lolcommits
2
2
  module Lolsrv
3
- VERSION = "0.0.3".freeze
3
+ VERSION = "0.0.4".freeze
4
4
  end
5
5
  end
@@ -47,7 +47,7 @@ module Lolcommits
47
47
  print "server: "
48
48
  options.merge!('server' => parse_user_input(gets.strip))
49
49
  puts '---------------------------------------------------------------'
50
- puts ' Lolsrv - Upload and sync lolcommits to a remote server'
50
+ puts ' Lolsrv - Sync lolcommits to a remote server'
51
51
  puts ''
52
52
  puts ' Handle POST /uplol with these request params'
53
53
  puts ''
@@ -82,67 +82,56 @@ module Lolcommits
82
82
 
83
83
  private
84
84
 
85
- ##
86
- # Message to show if syncing fails
87
- #
88
- # @return [String] message text
89
- #
90
- def fail_message
91
- "failed :( (try again with --debug)\n"
92
- end
93
-
94
85
  ##
95
86
  #
96
87
  # Syncs lolcommmit images to the remote server
97
88
  #
98
- # Fetches from /lols and iterates over objects in the JSON array
99
- # For each image found in the local loldir folder, check if it has already
100
- # been uploaded. If not upload the image with a POST request and
89
+ # Fetches from /lols and iterates over shas in the JSON array. Then for
90
+ # each image found in the local loldir folder, check if it has already
91
+ # been uploaded. If not, upload the image with a POST request and
101
92
  # upload_params.
102
93
  #
103
- # Upload requests that fail are skipped.
94
+ # Upload requests that fail abort the sync
104
95
  #
105
96
  def sync
106
- print "Syncing with lolsrv ... "
107
- existing = existing_lols
108
-
109
- # abort sync when invalid response or error from lols_endpoint
110
- unless existing
111
- print fail_message; return
112
- end
97
+ print "Syncing lols ... "
98
+ raise 'failed fetching existing lols' unless existing_shas
113
99
 
114
100
  Dir[runner.config.loldir + '/*.{jpg,gif}'].each do |image|
115
101
  sha = File.basename(image, '.*')
116
- response = upload(image, sha) unless existing.include?(sha) || sha == 'tmp_snapshot'
117
- unless response
118
- print fail_message; return
102
+ unless existing_shas.include?(sha) || sha == 'tmp_snapshot'
103
+ response = upload(image, sha)
104
+ raise "failed uploading #{image}" if response.nil?
119
105
  end
120
106
  end
121
107
 
122
108
  print "done!\n"
109
+ rescue RuntimeError => e
110
+ print "#{e.message} (try again with --debug)\n"
123
111
  end
124
112
 
125
113
  ##
126
114
  #
127
115
  # Fetch and parse JSON response from `server/lols`, returning an array of
128
- # commit SHA's. Logs error and returns nil on NET/HTTP and JSON parsing
129
- # errors.
116
+ # commit SHA's. Logs and returns nil on NET/HTTP and JSON parsing errors.
130
117
  #
131
118
  # @return [Array] containing commit SHA's
132
119
  # @return [Nil] if an error occurred
133
120
  #
134
- def existing_lols
135
- lols = JSON.parse(RestClient.get(lols_endpoint))
136
- lols.map { |lol| lol['sha'] }
137
- rescue JSON::ParserError, SocketError, RestClient::RequestFailed => e
138
- log_error(e, "ERROR: existing lols could not be retrieved #{e.class} - #{e.message}")
139
- return nil
121
+ def existing_shas
122
+ @existing_shas ||= begin
123
+ lols = JSON.parse(RestClient.get(lols_endpoint))
124
+ lols.map { |lol| lol['sha'] }
125
+ rescue JSON::ParserError, SocketError, RestClient::RequestFailed => e
126
+ log_error(e, "ERROR: existing lols could not be retrieved #{e.class} - #{e.message}")
127
+ return nil
128
+ end
140
129
  end
141
130
 
142
131
  ##
143
132
  #
144
133
  # Upload the lolcommit image to `server/uplol` with commit params. Logs
145
- # error and returns nil on NET/HTTP errors.
134
+ # and returns nil on NET/HTTP errors.
146
135
  #
147
136
  # @return [RestClient::Response] response object from POST request
148
137
  #
@@ -64,7 +64,10 @@ describe Lolcommits::Plugin::Lolsrv do
64
64
  end
65
65
 
66
66
  describe "run_capture_ready" do
67
- before { commit_repo_with_message }
67
+ before do
68
+ commit_repo_with_message
69
+ end
70
+
68
71
  after { teardown_repo }
69
72
 
70
73
  it "syncs lolcommits" do
@@ -77,8 +80,11 @@ describe Lolcommits::Plugin::Lolsrv do
77
80
 
78
81
  stub_request(:post, "https://lolsrv.com/uplol").to_return(status: 200)
79
82
 
80
- fake_io_capture { plugin.run_capture_ready(do_fork: false) }
83
+ output = fake_io_capture do
84
+ plugin.run_capture_ready(do_fork: false)
85
+ end
81
86
 
87
+ assert_equal output, "Syncing lols ... done!\n"
82
88
  assert_requested :get, "https://lolsrv.com/lols", times: 1
83
89
  assert_requested :post, "https://lolsrv.com/uplol", times: 1,
84
90
  headers: {'Content-Type' => /multipart\/form-data/ } do |req|
@@ -88,6 +94,20 @@ describe Lolcommits::Plugin::Lolsrv do
88
94
  end
89
95
  end
90
96
  end
97
+
98
+ it "shows error and aborts on failed lols endpoint" do
99
+ in_repo do
100
+ plugin.config = valid_enabled_config
101
+ stub_request(:get, "https://lolsrv.com/lols").to_return(status: 404)
102
+
103
+ output = fake_io_capture do
104
+ plugin.run_capture_ready(do_fork: false)
105
+ end
106
+
107
+ assert_equal output, "Syncing lols ... failed fetching existing lols (try again with --debug)\n"
108
+ assert_not_requested :post, "https://lolsrv.com/uplol"
109
+ end
110
+ end
91
111
  end
92
112
 
93
113
  describe "configuration" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lolcommits-lolsrv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Hutchinson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-15 00:00:00.000000000 Z
11
+ date: 2017-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client