spadeio 0.0.1 → 0.0.2

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.
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 jondot
1
+ Copyright (c) 2013 Dotan Nahum
2
2
 
3
3
  MIT License
4
4
 
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 jondot
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Spadeio
2
2
 
3
- TODO: Write a gem description
3
+ This is the Ruby driver for [Spade](http://spade.io)
4
+
5
+ See more at the docs: [docs.spade.io](http://docs.spade.io)
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,16 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ Here's how to quickly scrape a url with the Ruby client:
24
+
25
+
26
+ ```ruby
27
+ require 'spadeio'
28
+
29
+ client = SpadeIO::Client.new :key => '<your-key>'
30
+ res = client.scrape "http://a.url.example.com/some/article.html"
31
+ ```
32
+
22
33
 
23
34
  ## Contributing
24
35
 
@@ -27,3 +38,11 @@ TODO: Write usage instructions here
27
38
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
39
  4. Push to the branch (`git push origin my-new-feature`)
29
40
  5. Create new Pull Request
41
+
42
+
43
+
44
+
45
+ ## Copyright
46
+
47
+ Copyright (c) 2013 [Dotan Nahum](http://gplus.to/dotan) [@jondot](http://twitter.com/jondot). See MIT-LICENSE for further details.
48
+
@@ -1,4 +1,30 @@
1
1
  module SpadeIO::API
2
2
  V1 = 'http://api.spade.io/v1/'
3
3
  STABLE = V1
4
+ FIND_OUT_MORE = {
5
+ :key => ['http://spade.io'],
6
+ 400 => ['http://docs.spade.io/#errors'],
7
+ 500 => ['http://status.spade.io'],
8
+ 300 => ['http://status.spade.io']
9
+ }
10
+
11
+
12
+ WHAT_HAPPENED = {
13
+ :key => "An API key was not supplied when initializing a Spade Client",
14
+ 400 => 'Unauthorized, forbidden or unacceptable request.',
15
+ 500 => 'Server error.',
16
+ 300 => 'Network or downtime error.'
17
+ }
18
+
19
+ FIX_WITH = {
20
+ :key => ["Please make sure to initialize a client\n Client.new :key => '<your-key>'\n Before using it."],
21
+ 400 => ['Verify that your passing a key to Client.',
22
+ 'If you don\'t have a key, sign up at http://spade.io.',
23
+ 'Make sure your key is valid, try support@spade.io for help.'],
24
+ 500 => ['Email support@spade.io with the exact error snippet you\'ve got.',
25
+ 'Check out http://status.spade.io for known downtime.'],
26
+ 300 => ['Try making sure you don\'t have a proxy between you and api.spade.io.',
27
+ 'Make sure you can access http://api.spade.io/ping from a browser.']
28
+ }
29
+
4
30
  end
@@ -7,7 +7,7 @@ class SpadeIO::Client
7
7
 
8
8
  def initialize(opts)
9
9
  if !opts[:key]
10
- raise "Please supply an authentication key in ':key'. Get yours at http://spade.io."
10
+ raise build_helpful_error(:key)
11
11
  end
12
12
 
13
13
  @opts = opts
@@ -20,31 +20,59 @@ class SpadeIO::Client
20
20
 
21
21
  def scrape(uri, bucket=nil)
22
22
  res = scrape_all(uri, bucket)
23
- body = res.body
24
- if body && body.count > 0 && body.first['objects']
25
- return body.first['objects']
26
- else
27
- return nil
23
+ body = res ? res.body : nil
24
+
25
+ unless bucket
26
+ if body && body.count > 0 && body.first['objects']
27
+ body = body.first['objects']
28
+ else
29
+ body = nil
30
+ end
28
31
  end
32
+
33
+ body
29
34
  end
30
35
 
31
36
  def scrape_all(uri, bucket)
32
37
  res = @conn.get("scrape", { :url => uri })
33
38
 
34
39
 
35
- # XXX seriously work on this error handling area.
36
- # dev should get a clear explanation of:
37
- #
38
- # * what was wrong
39
- # * what we think may be wrong, and how to fix it
40
- # * where to get more information
41
- #
42
40
  if res.status != 200
43
- raise "error"
41
+ code_class = res.status - (res.status % 100)
42
+ raise build_helpful_error(code_class, (res.body || "").to_s[0..50])
44
43
  end
45
44
 
46
45
  res
47
46
  end
48
47
 
48
+
49
+ private
50
+ def build_helpful_error(key, snipt="")
51
+ res = <<EOF
52
+ Error: #{ SpadeIO::API::WHAT_HAPPENED[key]}
53
+ ------
54
+ #{
55
+ if snipt != ""
56
+ "\n<<------ response ------>>\n" + snipt + \
57
+
58
+ "\n<<----------------------->>\n"
59
+ end
60
+ }
61
+
62
+ Try one of these fix it:
63
+ ------------------------
64
+
65
+ #{ bullets( SpadeIO::API::FIX_WITH[key]) }
66
+
67
+ Find out more here:
68
+ -------------------
69
+ #{ bullets( SpadeIO::API::FIND_OUT_MORE[key]) }
70
+
71
+ EOF
72
+ end
73
+
74
+ def bullets(arr)
75
+ arr.map{|s| "* #{s}"}.join("\n")
76
+ end
49
77
  end
50
78
 
@@ -1,3 +1,3 @@
1
1
  module SpadeIO
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "title": "title",
3
+ "thumb": "thumb",
4
+ "description": "desc",
5
+ "src": "src"
6
+ }
@@ -13,6 +13,19 @@ describe SpadeIO::Client do
13
13
  end.must_raise RuntimeError
14
14
  end
15
15
 
16
+ it "should fail nicely for 300, 400, 500 class errors" do
17
+ uri = "http://foo.example.com/article.html"
18
+ stub_request(:get,"#{API_ENDPOINT}scrape?url=#{CGI.escape uri}")
19
+ .to_return( :status => 401, :body => file_fixture('empty.json'))
20
+
21
+ c = SpadeIO::Client.new(OPTS)
22
+ begin
23
+ res = c.scrape(uri)
24
+ rescue RuntimeError => err
25
+ assert_equal true, !!err.message.match(/.*Unauthorized, forbidden or unacceptable.*/m)
26
+ end
27
+ end
28
+
16
29
  it "should send an authenticated scrape to a page" do
17
30
  uri = "http://foo.example.com/article.html"
18
31
  stub_request(:get,"#{API_ENDPOINT}scrape?url=#{CGI.escape uri}")
@@ -71,5 +84,18 @@ describe SpadeIO::Client do
71
84
  res = c.scrape(uri)
72
85
  end.must_raise RuntimeError
73
86
  end
87
+
88
+ it "should return sucessfully when given a preview bucket" do
89
+ uri = "http://foo.example.com/article.html"
90
+ stub_request(:get,"#{API_ENDPOINT}scrape?url=#{CGI.escape uri}")
91
+ .to_return( :status => 200, :body => file_fixture('preview_bucket.json'))
92
+
93
+ c = SpadeIO::Client.new(OPTS)
94
+ res = c.scrape(uri, 'preview')
95
+
96
+ expected = JSON.parse(file_fixture('preview_bucket.json'))
97
+ assert_equal res, expected
98
+ end
99
+
74
100
  end
75
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spadeio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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: 2013-04-27 00:00:00.000000000 Z
12
+ date: 2013-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -150,6 +150,7 @@ files:
150
150
  - Gemfile
151
151
  - Guardfile
152
152
  - LICENSE.txt
153
+ - MIT-LICENSE
153
154
  - README.md
154
155
  - Rakefile
155
156
  - lib/spadeio.rb
@@ -160,6 +161,7 @@ files:
160
161
  - spec/fixtures/bigres.json
161
162
  - spec/fixtures/empty.json
162
163
  - spec/fixtures/no_objects.json
164
+ - spec/fixtures/preview_bucket.json
163
165
  - spec/spade_io/client_spec.rb
164
166
  - spec/spec_helper.rb
165
167
  homepage: http://spade.io
@@ -191,5 +193,6 @@ test_files:
191
193
  - spec/fixtures/bigres.json
192
194
  - spec/fixtures/empty.json
193
195
  - spec/fixtures/no_objects.json
196
+ - spec/fixtures/preview_bucket.json
194
197
  - spec/spade_io/client_spec.rb
195
198
  - spec/spec_helper.rb