altmetric.rb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/altmetric.rb +42 -5
  2. metadata +2 -2
data/lib/altmetric.rb CHANGED
@@ -2,8 +2,36 @@ require 'json'
2
2
  require 'httpclient'
3
3
  require 'uri_template'
4
4
 
5
+
5
6
  module Altmetric
7
+
8
+ class APIException < RuntimeError
9
+ attr_reader :url, :query
10
+ def initialize(url, query, msg)
11
+ super(msg)
12
+ @url = url
13
+ @query = query
14
+ end
15
+ end
16
+
17
+ class UnknownArticleException < APIException
18
+ def initialize(url, query, msg)
19
+ super(url, query, msg)
20
+ end
21
+ end
6
22
 
23
+ class UnauthorizedException < APIException
24
+ def initialize(url, query, msg)
25
+ super(url, query, msg)
26
+ end
27
+ end
28
+
29
+ class RateLimitedException < APIException
30
+ def initialize(url, query, msg)
31
+ super(url, query, message)
32
+ end
33
+ end
34
+
7
35
  class Client
8
36
 
9
37
  DEFAULT_USER_AGENT = "altmetric-ruby-client/0.0.1"
@@ -110,7 +138,7 @@ module Altmetric
110
138
  if block_given?
111
139
  yield response
112
140
  end
113
- validate_response(response)
141
+ validate_response(url, query, response)
114
142
  return JSON.parse( response.content )
115
143
  end
116
144
 
@@ -122,10 +150,19 @@ module Altmetric
122
150
  return response
123
151
  end
124
152
 
125
- def validate_response(response)
126
- if response.status != 200
127
- raise "Unable to perform request. Status: #{response.status}. Message: #{response.content}"
128
- end
153
+ def validate_response(url, query, response)
154
+ case response.status
155
+ when 200
156
+ #OK
157
+ when 403
158
+ raise UnauthorizedException.new(url, query, response.content)
159
+ when 404
160
+ raise UnknownArticleException.new(url, query, response.content)
161
+ when 420
162
+ raise RateLimitedException.new(url, query, response.content)
163
+ else
164
+ raise APIException.new(url, query, response)
165
+ end
129
166
  end
130
167
 
131
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: altmetric.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-05-23 00:00:00.000000000 Z
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json