angellist_api 0.0.4 → 0.0.5
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.
- data/lib/angellist_api/version.rb +1 -1
- data/lib/faraday/request/gateway.rb +18 -0
- data/lib/faraday/request/multipart_with_file.rb +36 -0
- data/lib/faraday/request/phoenix.rb +18 -0
- data/lib/faraday/request/twitter_oauth.rb +24 -0
- data/lib/faraday/response/raise_http_4xx.rb +45 -0
- data/lib/faraday/response/raise_http_5xx.rb +24 -0
- metadata +8 -2
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Request::Gateway < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
url = env[:url].dup
|
9
|
+
url.host = @gateway
|
10
|
+
env[:url] = url
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(app, gateway)
|
15
|
+
@app, @gateway = app, gateway
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Request::MultipartWithFile < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
if env[:body].is_a?(Hash)
|
9
|
+
env[:body].each do |key, value|
|
10
|
+
if value.is_a?(File)
|
11
|
+
env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
|
12
|
+
elsif value.is_a?(Hash) && (value['io'].is_a?(IO) || value['io'].is_a?(StringIO))
|
13
|
+
env[:body][key] = Faraday::UploadIO.new(value['io'], mime_type('.'+value['type']), '')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def mime_type(path)
|
24
|
+
case path
|
25
|
+
when /\.jpe?g/i
|
26
|
+
'image/jpeg'
|
27
|
+
when /\.gif$/i
|
28
|
+
'image/gif'
|
29
|
+
when /\.png$/i
|
30
|
+
'image/png'
|
31
|
+
else
|
32
|
+
'application/octet-stream'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request::Phoenix < Faraday::Middleware
|
5
|
+
def call(env)
|
6
|
+
# Not sure what what the X-Phx (Phoenix?) header is for but it's
|
7
|
+
# required to access certain undocumented resources
|
8
|
+
# e.g. GET urls/resolve
|
9
|
+
env[:request_headers]['X-Phx'] = 'true'
|
10
|
+
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(app)
|
15
|
+
@app = app
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request::AngellistApiOAuth < Faraday::Middleware
|
5
|
+
dependency 'simple_oauth'
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
params = env[:body] || {}
|
9
|
+
signature_params = params
|
10
|
+
|
11
|
+
params.map{ |k,v| signature_params = {} if v.respond_to?(:content_type) }
|
12
|
+
|
13
|
+
header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)
|
14
|
+
|
15
|
+
env[:request_headers]['Authorization'] = header.to_s
|
16
|
+
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(app, options)
|
21
|
+
@app, @options = app, options
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Response::RaiseHttp4xx < Response::Middleware
|
7
|
+
def on_complete(env)
|
8
|
+
case env[:status].to_i
|
9
|
+
when 400
|
10
|
+
raise AngellistApi::BadRequest.new(error_message(env), env[:response_headers])
|
11
|
+
when 401
|
12
|
+
raise AngellistApi::Unauthorized.new(error_message(env), env[:response_headers])
|
13
|
+
when 403
|
14
|
+
raise AngellistApi::Forbidden.new(error_message(env), env[:response_headers])
|
15
|
+
when 404
|
16
|
+
raise AngellistApi::NotFound.new(error_message(env), env[:response_headers])
|
17
|
+
when 406
|
18
|
+
raise AngellistApi::NotAcceptable.new(error_message(env), env[:response_headers])
|
19
|
+
when 420
|
20
|
+
raise AngellistApi::EnhanceYourCalm.new(error_message(env), env[:response_headers])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def error_message(env)
|
27
|
+
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def error_body(body)
|
31
|
+
if body.nil?
|
32
|
+
nil
|
33
|
+
elsif body['error']
|
34
|
+
": #{body['error']}"
|
35
|
+
elsif body['errors']
|
36
|
+
first = Array(body['errors']).first
|
37
|
+
if first.kind_of? Hash
|
38
|
+
": #{first['message'].chomp}"
|
39
|
+
else
|
40
|
+
": #{first.chomp}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Response::RaiseHttp5xx < Response::Middleware
|
7
|
+
def on_complete(env)
|
8
|
+
case env[:status].to_i
|
9
|
+
when 500
|
10
|
+
raise AngellistApi::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers])
|
11
|
+
when 502
|
12
|
+
raise AngellistApi::BadGateway.new(error_message(env, "AngellistApi is down or being upgraded."), env[:response_headers])
|
13
|
+
when 503
|
14
|
+
raise AngellistApi::ServiceUnavailable.new(error_message(env, "(__-){ AngellistApi is over capacity."), env[:response_headers])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def error_message(env, body=nil)
|
21
|
+
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{[env[:status].to_s + ':', body].compact.join(' ')} You should follow @angellistapi for frequent updates. And please, send your feedback to api@angel.co. Happy hacking!"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 5
|
9
|
+
version: 0.0.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paul Singh
|
@@ -139,6 +139,12 @@ files:
|
|
139
139
|
- lib/angellist_api/request.rb
|
140
140
|
- lib/angellist_api/version.rb
|
141
141
|
- lib/angellist_api.rb
|
142
|
+
- lib/faraday/request/gateway.rb
|
143
|
+
- lib/faraday/request/multipart_with_file.rb
|
144
|
+
- lib/faraday/request/phoenix.rb
|
145
|
+
- lib/faraday/request/twitter_oauth.rb
|
146
|
+
- lib/faraday/response/raise_http_4xx.rb
|
147
|
+
- lib/faraday/response/raise_http_5xx.rb
|
142
148
|
- lib/tasks/angellist_api_tasks.rake
|
143
149
|
- MIT-LICENSE
|
144
150
|
- Rakefile
|