evrythng 0.0.1
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/README +0 -0
- data/Rakefile +10 -0
- data/evrythng.gemspec +28 -0
- data/lib/evrythng/api.rb +23 -0
- data/lib/evrythng/authentication.rb +25 -0
- data/lib/evrythng/client/collections.rb +21 -0
- data/lib/evrythng/client/things.rb +21 -0
- data/lib/evrythng/client.rb +13 -0
- data/lib/evrythng/configuration.rb +100 -0
- data/lib/evrythng/connection.rb +42 -0
- data/lib/evrythng/error.rb +59 -0
- data/lib/evrythng/request.rb +44 -0
- data/lib/evrythng/version.rb +4 -0
- data/lib/evrythng.rb +25 -0
- data/lib/faraday/request/evrythng_oauth.rb +24 -0
- data/lib/faraday/request/gateway.rb +18 -0
- data/lib/faraday/request/multipart_with_file.rb +30 -0
- data/lib/faraday/request/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 +160 -0
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
data/evrythng.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
$:.unshift File.expand_path('../lib', __FILE__)
|
4
|
+
require 'evrythng/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "evrythng"
|
8
|
+
s.version = Evrythng::VERSION.dup
|
9
|
+
s.authors = ["beawesomeinstead"]
|
10
|
+
s.email = "graf.otodrakula@gmail.com"
|
11
|
+
s.homepage = "http://github.com/bai/evrythng"
|
12
|
+
s.summary = "A Ruby wrapper for the Evrythng API."
|
13
|
+
s.description = "A Ruby wrapper for the Evrythng API."
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.platform = Gem::Platform::RUBY
|
17
|
+
s.require_path = 'lib'
|
18
|
+
|
19
|
+
s.add_development_dependency('yard', '~> 0.6')
|
20
|
+
|
21
|
+
s.add_runtime_dependency 'hashie', '~> 1.0.0'
|
22
|
+
s.add_runtime_dependency 'faraday', '~> 0.6.1'
|
23
|
+
s.add_runtime_dependency 'faraday_middleware', '~> 0.6.3'
|
24
|
+
s.add_runtime_dependency 'multi_json', '~> 1.0.0'
|
25
|
+
s.add_runtime_dependency 'multi_xml', '~> 0.2.0'
|
26
|
+
s.add_runtime_dependency 'rash', '~> 0.3.0'
|
27
|
+
s.add_runtime_dependency 'simple_oauth', '~> 0.1.5'
|
28
|
+
end
|
data/lib/evrythng/api.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'evrythng/connection'
|
2
|
+
require 'evrythng/request'
|
3
|
+
require 'evrythng/authentication'
|
4
|
+
|
5
|
+
module Evrythng
|
6
|
+
# @private
|
7
|
+
class API
|
8
|
+
# @private
|
9
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
10
|
+
|
11
|
+
# Creates a new API
|
12
|
+
def initialize(options={})
|
13
|
+
options = Evrythng.options.merge(options)
|
14
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
15
|
+
send("#{key}=", options[key])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
include Connection
|
20
|
+
include Request
|
21
|
+
include Authentication
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Evrythng
|
2
|
+
# @private
|
3
|
+
module Authentication
|
4
|
+
private
|
5
|
+
|
6
|
+
# Authentication hash
|
7
|
+
#
|
8
|
+
# @return [Hash]
|
9
|
+
def authentication
|
10
|
+
{
|
11
|
+
:consumer_key => consumer_key,
|
12
|
+
:consumer_secret => consumer_secret,
|
13
|
+
:token => oauth_token,
|
14
|
+
:token_secret => oauth_token_secret
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
# Check whether user is authenticated
|
19
|
+
#
|
20
|
+
# @return [Boolean]
|
21
|
+
def authenticated?
|
22
|
+
authentication.values.all?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Evrythng
|
2
|
+
class Client
|
3
|
+
# Defines methods related to collections
|
4
|
+
module Collections
|
5
|
+
# Returns a list of collections
|
6
|
+
#
|
7
|
+
# @format :json, :xml
|
8
|
+
# @authenticated true
|
9
|
+
# @rate_limited true
|
10
|
+
# @param options [Hash] A customizable set of options.
|
11
|
+
# @return [Hashie::Rash] The requested list of collections.
|
12
|
+
# @see http://dev.evrythng.net/doc/get/collections
|
13
|
+
# @example Return the list of collections
|
14
|
+
# Evrythng.collections
|
15
|
+
def collections(options={})
|
16
|
+
response = get('collections', options)
|
17
|
+
format.to_s.downcase == 'xml' ? response['collections'] : response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Evrythng
|
2
|
+
class Client
|
3
|
+
# Defines methods related to things
|
4
|
+
module Things
|
5
|
+
# Returns a list of things
|
6
|
+
#
|
7
|
+
# @format :json, :xml
|
8
|
+
# @authenticated true
|
9
|
+
# @rate_limited true
|
10
|
+
# @param options [Hash] A customizable set of options.
|
11
|
+
# @return [Hashie::Rash] The requested list of things.
|
12
|
+
# @see http://dev.evrythng.net/doc/get/things
|
13
|
+
# @example Return the list of things
|
14
|
+
# Evrythng.things
|
15
|
+
def things(options={})
|
16
|
+
response = get('things', options)
|
17
|
+
format.to_s.downcase == 'xml' ? response['things'] : response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Evrythng
|
2
|
+
# Wrapper for the Evrythng REST API
|
3
|
+
class Client < API
|
4
|
+
# Require client method modules after initializing the Client class in
|
5
|
+
# order to avoid a superclass mismatch error, allowing those modules to be
|
6
|
+
# Client-namespaced.
|
7
|
+
require 'evrythng/client/things'
|
8
|
+
require 'evrythng/client/collections'
|
9
|
+
|
10
|
+
include Evrythng::Client::Things
|
11
|
+
include Evrythng::Client::Collections
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'evrythng/version'
|
3
|
+
|
4
|
+
module Evrythng
|
5
|
+
# Defines constants and methods related to configuration
|
6
|
+
module Configuration
|
7
|
+
# An array of valid keys in the options hash when configuring a {Evrythng::API}
|
8
|
+
VALID_OPTIONS_KEYS = [
|
9
|
+
:adapter,
|
10
|
+
:consumer_key,
|
11
|
+
:consumer_secret,
|
12
|
+
:api_endpoint,
|
13
|
+
:format,
|
14
|
+
:gateway,
|
15
|
+
:oauth_token,
|
16
|
+
:oauth_token_secret,
|
17
|
+
:proxy,
|
18
|
+
:search_endpoint,
|
19
|
+
:user_agent].freeze
|
20
|
+
|
21
|
+
# An array of valid request/response formats
|
22
|
+
#
|
23
|
+
# @note Not all methods support the XML format.
|
24
|
+
VALID_FORMATS = [
|
25
|
+
:json,
|
26
|
+
:xml].freeze
|
27
|
+
|
28
|
+
# The adapter that will be used to connect if none is set
|
29
|
+
#
|
30
|
+
# @note The default faraday adapter is Net::HTTP.
|
31
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
32
|
+
|
33
|
+
# By default, don't set an application key
|
34
|
+
DEFAULT_CONSUMER_KEY = nil
|
35
|
+
|
36
|
+
# By default, don't set an application secret
|
37
|
+
DEFAULT_CONSUMER_SECRET = nil
|
38
|
+
|
39
|
+
# The endpoint that will be used to connect if none is set
|
40
|
+
DEFAULT_ENDPOINT = 'http://evrythng.net/api/v1/'.freeze
|
41
|
+
|
42
|
+
# The response format appended to the path and sent in the 'Accept' header if none is set
|
43
|
+
#
|
44
|
+
# @note JSON is preferred over XML because it is more concise and faster to parse.
|
45
|
+
DEFAULT_FORMAT = :json
|
46
|
+
|
47
|
+
# By default, don't set a user oauth token
|
48
|
+
DEFAULT_OAUTH_TOKEN = nil
|
49
|
+
|
50
|
+
# By default, don't set a user oauth secret
|
51
|
+
DEFAULT_OAUTH_TOKEN_SECRET = nil
|
52
|
+
|
53
|
+
# By default, don't use a proxy server
|
54
|
+
DEFAULT_PROXY = nil
|
55
|
+
|
56
|
+
# The search endpoint that will be used to connect if none is set
|
57
|
+
DEFAULT_SEARCH_ENDPOINT = 'http://evrythng.net/api/v1/search'.freeze
|
58
|
+
|
59
|
+
# The user agent that will be sent to the API endpoint if none is set
|
60
|
+
DEFAULT_USER_AGENT = "Evrythng Ruby Gem #{Evrythng::VERSION}".freeze
|
61
|
+
|
62
|
+
DEFAULT_GATEWAY = nil
|
63
|
+
|
64
|
+
# @private
|
65
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
66
|
+
|
67
|
+
# When this module is extended, set all configuration options to their default values
|
68
|
+
def self.extended(base)
|
69
|
+
base.reset
|
70
|
+
end
|
71
|
+
|
72
|
+
# Convenience method to allow configuration options to be set in a block
|
73
|
+
def configure
|
74
|
+
yield self
|
75
|
+
end
|
76
|
+
|
77
|
+
# Create a hash of options and their values
|
78
|
+
def options
|
79
|
+
options = {}
|
80
|
+
VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
|
81
|
+
options
|
82
|
+
end
|
83
|
+
|
84
|
+
# Reset all configuration options to defaults
|
85
|
+
def reset
|
86
|
+
self.adapter = DEFAULT_ADAPTER
|
87
|
+
self.consumer_key = DEFAULT_CONSUMER_KEY
|
88
|
+
self.consumer_secret = DEFAULT_CONSUMER_SECRET
|
89
|
+
self.api_endpoint = DEFAULT_ENDPOINT
|
90
|
+
self.format = DEFAULT_FORMAT
|
91
|
+
self.oauth_token = DEFAULT_OAUTH_TOKEN
|
92
|
+
self.oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET
|
93
|
+
self.proxy = DEFAULT_PROXY
|
94
|
+
self.search_endpoint = DEFAULT_SEARCH_ENDPOINT
|
95
|
+
self.user_agent = DEFAULT_USER_AGENT
|
96
|
+
self.gateway = DEFAULT_GATEWAY
|
97
|
+
self
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday/request/multipart_with_file'
|
3
|
+
require 'faraday/request/gateway'
|
4
|
+
require 'faraday/request/evrythng_oauth'
|
5
|
+
require 'faraday/response/raise_http_4xx'
|
6
|
+
require 'faraday/response/raise_http_5xx'
|
7
|
+
|
8
|
+
module Evrythng
|
9
|
+
# @private
|
10
|
+
module Connection
|
11
|
+
private
|
12
|
+
|
13
|
+
def connection(raw=false)
|
14
|
+
options = {
|
15
|
+
:headers => {'Accept' => "application/#{format}", 'User-Agent' => user_agent},
|
16
|
+
:proxy => proxy,
|
17
|
+
:ssl => {:verify => false},
|
18
|
+
:url => api_endpoint,
|
19
|
+
}
|
20
|
+
|
21
|
+
Faraday.new(options) do |builder|
|
22
|
+
builder.use Faraday::Request::MultipartWithFile
|
23
|
+
builder.use Faraday::Request::EvrythngOAuth, authentication if authenticated?
|
24
|
+
builder.use Faraday::Request::Multipart
|
25
|
+
builder.use Faraday::Request::UrlEncoded
|
26
|
+
builder.use Faraday::Request::Gateway, gateway if gateway
|
27
|
+
builder.use Faraday::Response::RaiseHttp4xx
|
28
|
+
builder.use Faraday::Response::Rashify unless raw
|
29
|
+
unless raw
|
30
|
+
case format.to_s.downcase
|
31
|
+
when 'json'
|
32
|
+
builder.use Faraday::Response::ParseJson
|
33
|
+
when 'xml'
|
34
|
+
builder.use Faraday::Response::ParseXml
|
35
|
+
end
|
36
|
+
end
|
37
|
+
builder.use Faraday::Response::RaiseHttp5xx
|
38
|
+
builder.adapter(adapter)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Evrythng
|
2
|
+
# Custom error class for rescuing from all Evrythng errors
|
3
|
+
class Error < StandardError
|
4
|
+
attr_reader :http_headers
|
5
|
+
|
6
|
+
def initialize(message, http_headers)
|
7
|
+
@http_headers = Hash[http_headers]
|
8
|
+
super message
|
9
|
+
end
|
10
|
+
|
11
|
+
def ratelimit_reset
|
12
|
+
Time.at(@http_headers.values_at('x-ratelimit-reset', 'X-RateLimit-Reset').detect{|value| value}.to_i)
|
13
|
+
end
|
14
|
+
|
15
|
+
def ratelimit_limit
|
16
|
+
@http_headers.values_at('x-ratelimit-limit', 'X-RateLimit-Limit').detect{|value| value}.to_i
|
17
|
+
end
|
18
|
+
|
19
|
+
def ratelimit_remaining
|
20
|
+
@http_headers.values_at('x-ratelimit-remaining', 'X-RateLimit-Remaining').detect{|value| value}.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def retry_after
|
24
|
+
[(ratelimit_reset - Time.now).ceil, 0].max
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Raised when Evrythng returns the HTTP status code 400
|
29
|
+
class BadRequest < Error; end
|
30
|
+
|
31
|
+
# Raised when Evrythng returns the HTTP status code 401
|
32
|
+
class Unauthorized < Error; end
|
33
|
+
|
34
|
+
# Raised when Evrythng returns the HTTP status code 403
|
35
|
+
class Forbidden < Error; end
|
36
|
+
|
37
|
+
# Raised when Evrythng returns the HTTP status code 404
|
38
|
+
class NotFound < Error; end
|
39
|
+
|
40
|
+
# Raised when Evrythng returns the HTTP status code 406
|
41
|
+
class NotAcceptable < Error; end
|
42
|
+
|
43
|
+
# Raised when Evrythng returns the HTTP status code 420
|
44
|
+
class EnhanceYourCalm < Error
|
45
|
+
# The number of seconds your application should wait before requesting date from the Search API again
|
46
|
+
def retry_after
|
47
|
+
@http_headers.values_at('retry-after', 'Retry-After').detect {|value| value }.to_i
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Raised when Evrythng returns the HTTP status code 500
|
52
|
+
class InternalServerError < Error; end
|
53
|
+
|
54
|
+
# Raised when Evrythng returns the HTTP status code 502
|
55
|
+
class BadGateway < Error; end
|
56
|
+
|
57
|
+
# Raised when Evrythng returns the HTTP status code 503
|
58
|
+
class ServiceUnavailable < Error; end
|
59
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Evrythng
|
2
|
+
# Defines HTTP request methods
|
3
|
+
module Request
|
4
|
+
# Perform an HTTP GET request
|
5
|
+
def get(path, options={}, raw=false)
|
6
|
+
request(:get, path, options, raw)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Perform an HTTP POST request
|
10
|
+
def post(path, options={}, raw=false)
|
11
|
+
request(:post, path, options, raw)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Perform an HTTP PUT request
|
15
|
+
def put(path, options={}, raw=false)
|
16
|
+
request(:put, path, options, raw)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Perform an HTTP DELETE request
|
20
|
+
def delete(path, options={}, raw=false)
|
21
|
+
request(:delete, path, options, raw)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# Perform an HTTP request
|
27
|
+
def request(method, path, options, raw=false)
|
28
|
+
response = connection(raw).send(method) do |request|
|
29
|
+
case method
|
30
|
+
when :get, :delete
|
31
|
+
request.url(formatted_path(path), options)
|
32
|
+
when :post, :put
|
33
|
+
request.path = formatted_path(path)
|
34
|
+
request.body = options unless options.empty?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
raw ? response : response.body
|
38
|
+
end
|
39
|
+
|
40
|
+
def formatted_path(path)
|
41
|
+
[path, format].compact.join('.')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/evrythng.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'evrythng/error'
|
2
|
+
require 'evrythng/configuration'
|
3
|
+
require 'evrythng/api'
|
4
|
+
require 'evrythng/client'
|
5
|
+
|
6
|
+
module Evrythng
|
7
|
+
extend Configuration
|
8
|
+
|
9
|
+
# Alias for Evrythng::Client.new
|
10
|
+
#
|
11
|
+
# @return [Evrythng::Client]
|
12
|
+
def self.new(options={})
|
13
|
+
Evrythng::Client.new(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Delegate to Evrythng::Client
|
17
|
+
def self.method_missing(method, *args, &block)
|
18
|
+
return super unless new.respond_to?(method)
|
19
|
+
new.send(method, *args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.respond_to?(method, include_private = false)
|
23
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request::EvrythngOAuth < 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,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,30 @@
|
|
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), value.path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def mime_type(file)
|
22
|
+
case file.path
|
23
|
+
when /\.jpe?g/i then 'image/jpeg'
|
24
|
+
when /\.gif$/i then 'image/gif'
|
25
|
+
when /\.png$/i then 'image/png'
|
26
|
+
else 'application/octet-stream'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request::OAuth < 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 Evrythng::BadRequest.new(error_message(env), env[:response_headers])
|
11
|
+
when 401
|
12
|
+
raise Evrythng::Unauthorized.new(error_message(env), env[:response_headers])
|
13
|
+
when 403
|
14
|
+
raise Evrythng::Forbidden.new(error_message(env), env[:response_headers])
|
15
|
+
when 404
|
16
|
+
raise Evrythng::NotFound.new(error_message(env), env[:response_headers])
|
17
|
+
when 406
|
18
|
+
raise Evrythng::NotAcceptable.new(error_message(env), env[:response_headers])
|
19
|
+
when 420
|
20
|
+
raise Evrythng::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 Evrythng::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers])
|
11
|
+
when 502
|
12
|
+
raise Evrythng::BadGateway.new(error_message(env, "Evrythng is down or being upgraded."), env[:response_headers])
|
13
|
+
when 503
|
14
|
+
raise Evrythng::ServiceUnavailable.new(error_message(env, "(__-){ Evrythng 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(' ')} Check http://status.evrythng.net/ for updates on the status of the Evrythng service."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: evrythng
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- beawesomeinstead
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-06 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: yard
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0.6"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: hashie
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: faraday
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.6.1
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: faraday_middleware
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 0.6.3
|
57
|
+
type: :runtime
|
58
|
+
version_requirements: *id004
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: multi_json
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.0.0
|
68
|
+
type: :runtime
|
69
|
+
version_requirements: *id005
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: multi_xml
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 0.2.0
|
79
|
+
type: :runtime
|
80
|
+
version_requirements: *id006
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rash
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.3.0
|
90
|
+
type: :runtime
|
91
|
+
version_requirements: *id007
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
name: simple_oauth
|
94
|
+
prerelease: false
|
95
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ~>
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 0.1.5
|
101
|
+
type: :runtime
|
102
|
+
version_requirements: *id008
|
103
|
+
description: A Ruby wrapper for the Evrythng API.
|
104
|
+
email: graf.otodrakula@gmail.com
|
105
|
+
executables: []
|
106
|
+
|
107
|
+
extensions: []
|
108
|
+
|
109
|
+
extra_rdoc_files: []
|
110
|
+
|
111
|
+
files:
|
112
|
+
- README
|
113
|
+
- Rakefile
|
114
|
+
- evrythng.gemspec
|
115
|
+
- lib/evrythng.rb
|
116
|
+
- lib/evrythng/api.rb
|
117
|
+
- lib/evrythng/authentication.rb
|
118
|
+
- lib/evrythng/client.rb
|
119
|
+
- lib/evrythng/client/collections.rb
|
120
|
+
- lib/evrythng/client/things.rb
|
121
|
+
- lib/evrythng/configuration.rb
|
122
|
+
- lib/evrythng/connection.rb
|
123
|
+
- lib/evrythng/error.rb
|
124
|
+
- lib/evrythng/request.rb
|
125
|
+
- lib/evrythng/version.rb
|
126
|
+
- lib/faraday/request/evrythng_oauth.rb
|
127
|
+
- lib/faraday/request/gateway.rb
|
128
|
+
- lib/faraday/request/multipart_with_file.rb
|
129
|
+
- lib/faraday/request/oauth.rb
|
130
|
+
- lib/faraday/response/raise_http_4xx.rb
|
131
|
+
- lib/faraday/response/raise_http_5xx.rb
|
132
|
+
homepage: http://github.com/bai/evrythng
|
133
|
+
licenses: []
|
134
|
+
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: "0"
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
none: false
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: "0"
|
152
|
+
requirements: []
|
153
|
+
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 1.8.5
|
156
|
+
signing_key:
|
157
|
+
specification_version: 3
|
158
|
+
summary: A Ruby wrapper for the Evrythng API.
|
159
|
+
test_files: []
|
160
|
+
|