evrythng 0.0.5 → 0.1.0

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.
Files changed (42) hide show
  1. data/README.md +1 -2
  2. data/evrythng.gemspec +12 -16
  3. data/lib/evrythng/authenticatable.rb +21 -0
  4. data/lib/evrythng/client/collections.rb +6 -14
  5. data/lib/evrythng/client/properties.rb +45 -0
  6. data/lib/evrythng/client/search.rb +22 -0
  7. data/lib/evrythng/client/thngs.rb +20 -16
  8. data/lib/evrythng/client.rb +31 -6
  9. data/lib/evrythng/config.rb +69 -0
  10. data/lib/evrythng/connection.rb +22 -23
  11. data/lib/evrythng/core_ext/hash.rb +19 -0
  12. data/lib/evrythng/error/bad_gateway.rb +7 -0
  13. data/lib/evrythng/error/bad_request.rb +7 -0
  14. data/lib/evrythng/error/client_error.rb +7 -0
  15. data/lib/evrythng/error/enhance_your_calm.rb +11 -0
  16. data/lib/evrythng/error/forbidden.rb +7 -0
  17. data/lib/evrythng/error/internal_server_error.rb +7 -0
  18. data/lib/evrythng/error/not_acceptable.rb +7 -0
  19. data/lib/evrythng/error/not_found.rb +7 -0
  20. data/lib/evrythng/error/server_error.rb +7 -0
  21. data/lib/evrythng/error/service_unavailable.rb +7 -0
  22. data/lib/evrythng/error/unauthorized.rb +7 -0
  23. data/lib/evrythng/error.rb +10 -33
  24. data/lib/evrythng/request/gateway.rb +20 -0
  25. data/lib/evrythng/request/token_authentication.rb +22 -0
  26. data/lib/evrythng/request.rb +17 -17
  27. data/lib/evrythng/response/parse_json.rb +23 -0
  28. data/lib/evrythng/response/raise_client_error.rb +49 -0
  29. data/lib/evrythng/response/raise_server_error.rb +23 -0
  30. data/lib/evrythng/version.rb +1 -2
  31. data/lib/evrythng.rb +5 -7
  32. metadata +65 -96
  33. data/lib/evrythng/api.rb +0 -24
  34. data/lib/evrythng/authentication.rb +0 -25
  35. data/lib/evrythng/configuration.rb +0 -102
  36. data/lib/evrythng/search.rb +0 -173
  37. data/lib/faraday/request/basic_authentication.rb +0 -15
  38. data/lib/faraday/request/evrythng_oauth.rb +0 -24
  39. data/lib/faraday/request/gateway.rb +0 -18
  40. data/lib/faraday/request/multipart_with_file.rb +0 -34
  41. data/lib/faraday/response/raise_http_4xx.rb +0 -45
  42. data/lib/faraday/response/raise_http_5xx.rb +0 -24
data/README.md CHANGED
@@ -9,6 +9,5 @@ This is a Ruby wrapper for the Evrythng API. Do not consider it as stable, that'
9
9
  ## Usage
10
10
 
11
11
  Evrythng.configure do |config|
12
- config.username = 'username' # your Evrythng username
13
- config.password = 'passw0rd' # your Evrythng password
12
+ config.token = 't0ken' # your Evrythng authentication token
14
13
  end
data/evrythng.gemspec CHANGED
@@ -8,26 +8,22 @@ Gem::Specification.new do |s|
8
8
  s.version = Evrythng::VERSION.dup
9
9
  s.authors = ["beawesomeinstead"]
10
10
  s.email = "graf.otodrakula@gmail.com"
11
- s.homepage = "http://github.com/bai/evrythng-ruby"
12
- s.summary = "A Ruby wrapper for Evrythng API"
13
- s.description = "A Ruby wrapper for Evrythng API. Just proof of concept, do not consider this as a solid library."
11
+ s.homepage = "http://github.com/bai/evrythng"
12
+ s.summary = "Evrythng API wrapper"
13
+ s.description = "A Ruby wrapper for Evrythng API."
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.platform = Gem::Platform::RUBY
17
17
  s.require_path = 'lib'
18
18
 
19
- s.add_development_dependency 'maruku', '~> 0.6'
20
- s.add_development_dependency 'nokogiri', '~> 1.4'
21
- s.add_development_dependency 'rake', '~> 0.9'
22
- s.add_development_dependency 'rspec', '~> 2.6'
23
- s.add_development_dependency 'simplecov', '~> 0.4'
24
- s.add_development_dependency 'webmock', '~> 1.7'
25
- s.add_development_dependency 'yard', '~> 0.7'
26
- s.add_development_dependency 'ZenTest', '~> 4.5'
19
+ s.add_development_dependency 'rake'
20
+ s.add_development_dependency 'rspec'
21
+ s.add_development_dependency 'simplecov'
22
+ s.add_development_dependency 'webmock'
23
+ s.add_development_dependency 'yard'
24
+ s.add_development_dependency 'json'
27
25
 
28
- s.add_runtime_dependency 'hashie', '~> 1.1.0'
29
- s.add_runtime_dependency 'faraday', '~> 0.7.4'
30
- s.add_runtime_dependency 'faraday_middleware', '~> 0.7.0'
31
- s.add_runtime_dependency 'multi_json', '~> 1.0.0'
32
- s.add_runtime_dependency 'simple_oauth', '~> 0.1.5'
26
+ s.add_dependency 'activesupport', ['>= 2.3.9', '< 4']
27
+ s.add_dependency 'faraday', '~> 0.8.0.rc2'
28
+ s.add_dependency 'multi_json', '~> 1.0.0'
33
29
  end
@@ -0,0 +1,21 @@
1
+ module Evrythng
2
+ module Authenticatable
3
+
4
+ # Credentials hash
5
+ #
6
+ # @return [Hash]
7
+ def credentials
8
+ {
9
+ :token => token
10
+ }
11
+ end
12
+
13
+ # Check whether credentials are present
14
+ #
15
+ # @return [Boolean]
16
+ def credentials?
17
+ credentials.values.all?
18
+ end
19
+
20
+ end
21
+ end
@@ -4,34 +4,26 @@ module Evrythng
4
4
  module Collections
5
5
  # Returns a list of collections
6
6
  #
7
- # @format :json, :xml
8
- # @authenticated true
9
- # @rate_limited true
10
7
  # @param options [Hash] A customizable set of options.
11
- # @return [Hashie::Mash] The requested list of collections.
12
- # @see http://dev.evrythng.net/doc/get/collections
8
+ # @return [Array] The requested list of collections.
9
+ # @see http://dev.evrythng.com/collections
13
10
  # @example Return the list of collections
14
11
  # Evrythng.collections
15
12
  def collections(options={})
16
- response = get('collections', options)
17
- format.to_s.downcase == 'xml' ? response['collections'] : response
13
+ get('collections', options)
18
14
  end
19
15
 
20
16
  # Creates a collection
21
17
  #
22
- # @format :json, :xml
23
- # @authenticated true
24
- # @rate_limited false
25
18
  # @param name [String] The name of collection.
26
19
  # @param description [String] The description of collection.
27
20
  # @param options [Hash] A customizable set of options.
28
- # @return [Hashie::Mash] The created collection.
29
- # @see http://dev.evrythng.net/doc/post/collections
21
+ # @return [Hash] The created collection.
22
+ # @see http://dev.evrythng.com/collections
30
23
  # @example Create the authenticating user's collection
31
24
  # Evrythng.collection_create("This is a new collection!", "Here comes the description.")
32
25
  def collection_create(name, description=nil, options={})
33
- response = post('collections', options.merge(:collection => { :name => name, :description => description }))
34
- format.to_s.downcase == 'xml' ? response['collection'] : response
26
+ post('collections', options.merge(:name => name, :description => description))
35
27
  end
36
28
  end
37
29
  end
@@ -0,0 +1,45 @@
1
+ module Evrythng
2
+ class Client
3
+ # Defines methods related to properties
4
+ module Properties
5
+ # Returns a list of properties for a given thng
6
+ #
7
+ # @param thng_id [String] The id of the thng to get properties for.
8
+ # @param options [Hash] A customizable set of options.
9
+ # @return [Array] The requested list of properties.
10
+ # @see http://dev.evrythng.com/properties
11
+ # @example Return the list of properties for thng 4f2133f39f5c550c2000016a
12
+ # Evrythng.properties('4f2133f39f5c550c2000016a')
13
+ def properties(thng_id, options={})
14
+ get("thngs/#{thng_id}/properties", options)
15
+ end
16
+
17
+ # Returns a single property, specified by key
18
+ #
19
+ # @param thng_id [String] The id of the thng to get property for.
20
+ # @param key [String] The key of a property to fetch.
21
+ # @param options [Hash] A customizable set of options.
22
+ # @return [Hash] The requested property.
23
+ # @see http://dev.evrythng.com/properties
24
+ # @example Return the property with key 'Volume' for the thng with id 4f2133f39f5c550c2000016a
25
+ # Evrythng.property('4f2133f39f5c550c2000016a', 'Volume')
26
+ def property(thng_id, key, options={})
27
+ get("thngs/#{thng_id}/properties/#{key}", options)
28
+ end
29
+
30
+ # Creates a property
31
+ #
32
+ # @param thng_id [String] The id of the thng to create property for.
33
+ # @param key [String] The key of property.
34
+ # @param value [String] The value of property.
35
+ # @param options [Hash] A customizable set of options.
36
+ # @return [Hash] The created property.
37
+ # @see http://dev.evrythng.com/properties
38
+ # @example Create a property for a thng with id 4f2133f39f5c550c2000016a
39
+ # Evrythng.property_create('4f2133f39f5c550c2000016a', 'Volume', '30')
40
+ def property_create(thng_id, key, value, options={})
41
+ post("thngs/#{thng_id}/properties", options.merge(:key => key, :value => value))
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,22 @@
1
+ module Evrythng
2
+ class Client
3
+ # Defines methods related to Search
4
+ module Search
5
+
6
+ # Returns thngs that match a specified query.
7
+ #
8
+ # @see https://dev.evrythng.com/search
9
+ # @param q [String] A search term.
10
+ # @param options [Hash] A customizable set of options.
11
+ # @option options [String] :geocode Returns thngs located within a given radius of the given latitude/longitude. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers).
12
+ # @option options [Integer] :page The page number (starting at 1) to return.
13
+ # @option options [Integer] :per_page The number of thngs to return per page, up to a max of 100.
14
+ # @return [Array] Return thngs that match a specified query
15
+ # @example Returns thngs that contain 'bike' in their name or description
16
+ # Evrythng.search('bike')
17
+ def search(q, options={})
18
+ get('search', options.merge(:q => q))
19
+ end
20
+ end
21
+ end
22
+ end
@@ -4,34 +4,38 @@ module Evrythng
4
4
  module Thngs
5
5
  # Returns a list of thngs
6
6
  #
7
- # @format :json, :xml
8
- # @authenticated true
9
- # @rate_limited true
10
7
  # @param options [Hash] A customizable set of options.
11
- # @return [Hashie::Mash] The requested list of thngs.
12
- # @see http://developer.evrythng.net/thngs
8
+ # @return [Array] The requested list of thngs.
9
+ # @see http://dev.evrythng.com/thngs
13
10
  # @example Return the list of thngs
14
11
  # Evrythng.thngs
15
12
  def thngs(options={})
16
- response = get('thngs', options)
17
- format.to_s.downcase == 'xml' ? response['thngs'] : response
13
+ get('thngs', options)
14
+ end
15
+
16
+ # Returns a single thng, specified by id
17
+ #
18
+ # @param id [String] The id of the desired thng.
19
+ # @param options [Hash] A customizable set of options.
20
+ # @return [Hash] The requested thng.
21
+ # @see http://dev.evrythng.com/thngs
22
+ # @example Return the thng with the id 4f2133f39f5c550c2000016a
23
+ # Evrythng.thng('4f2133f39f5c550c2000016a')
24
+ def thng(id, options={})
25
+ get("thngs/#{id}", options)
18
26
  end
19
27
 
20
28
  # Creates a thng
21
29
  #
22
- # @format :json, :xml
23
- # @authenticated true
24
- # @rate_limited false
25
- # @param identifier [String] The identifier of thng.
30
+ # @param name [String] The name of thng.
26
31
  # @param description [String] The description of thng.
27
32
  # @param options [Hash] A customizable set of options.
28
- # @return [Hashie::Mash] The created thng.
29
- # @see http://developer.evrythng.net/thngs
33
+ # @return [Hash] The created thng.
34
+ # @see http://dev.evrythng.com/thngs
30
35
  # @example Create the authenticating user's thng
31
36
  # Evrythng.thng_create("my.test.thng", "Here comes the description.")
32
- def thng_create(identifier, description=nil, options={})
33
- response = post('thngs', options.merge(:thng => { :identifier => identifier, :description => description }))
34
- format.to_s.downcase == 'xml' ? response['thng'] : response
37
+ def thng_create(name, description=nil, options={})
38
+ post('thngs', options.merge(:name => name, :description => description))
35
39
  end
36
40
  end
37
41
  end
@@ -1,13 +1,38 @@
1
+ require 'evrythng/authenticatable'
2
+ require 'evrythng/config'
3
+ require 'evrythng/connection'
4
+ require 'evrythng/request'
5
+
1
6
  module Evrythng
2
7
  # 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/thngs'
8
- require 'evrythng/client/collections'
8
+ class Client
9
+ include Evrythng::Authenticatable
10
+ include Evrythng::Connection
11
+ include Evrythng::Request
9
12
 
13
+ require 'evrythng/client/thngs'
10
14
  include Evrythng::Client::Thngs
15
+
16
+ require 'evrythng/client/collections'
11
17
  include Evrythng::Client::Collections
18
+
19
+ require 'evrythng/client/properties'
20
+ include Evrythng::Client::Properties
21
+
22
+ require 'evrythng/client/search'
23
+ include Evrythng::Client::Search
24
+
25
+ attr_accessor *Config::VALID_OPTIONS_KEYS
26
+
27
+ # Initializes a new API object
28
+ #
29
+ # @param attrs [Hash]
30
+ # @return [Evrythng::Client]
31
+ def initialize(attrs={})
32
+ attrs = Evrythng.options.merge(attrs)
33
+ Config::VALID_OPTIONS_KEYS.each do |key|
34
+ instance_variable_set("@#{key}".to_sym, attrs[key])
35
+ end
36
+ end
12
37
  end
13
38
  end
@@ -0,0 +1,69 @@
1
+ require 'evrythng/version'
2
+
3
+ module Evrythng
4
+ # Defines constants and methods related to configuration
5
+ module Config
6
+ # An array of valid keys in the options hash when configuring a {Evrythng::API}
7
+ VALID_OPTIONS_KEYS = [
8
+ :adapter,
9
+ :connection_options,
10
+ :token,
11
+ :endpoint,
12
+ :gateway,
13
+ :proxy,
14
+ :user_agent]
15
+
16
+ # The adapter that will be used to connect if none is set
17
+ DEFAULT_ADAPTER = :net_http
18
+
19
+ # The Faraday connection options if none is set
20
+ DEFAULT_CONNECTION_OPTIONS = {}
21
+
22
+ # By default, don't set a token
23
+ DEFAULT_TOKEN = nil
24
+
25
+ # The endpoint that will be used to connect if none is set
26
+ DEFAULT_ENDPOINT = 'http://evrythng.com'
27
+
28
+ # By default, don't use a proxy server
29
+ DEFAULT_PROXY = nil
30
+
31
+ # The user agent that will be sent to the API endpoint if none is set
32
+ DEFAULT_USER_AGENT = "Evrythng Ruby Gem #{Evrythng::VERSION}"
33
+
34
+ # By default, don't use a gateway server
35
+ DEFAULT_GATEWAY = nil
36
+
37
+ # @private
38
+ attr_accessor *VALID_OPTIONS_KEYS
39
+
40
+ # When this module is extended, set all configuration options to their default values
41
+ def self.extended(base)
42
+ base.reset
43
+ end
44
+
45
+ # Convenience method to allow configuration options to be set in a block
46
+ def configure
47
+ yield self
48
+ end
49
+
50
+ # Create a hash of options and their values
51
+ def options
52
+ options = {}
53
+ VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
54
+ options
55
+ end
56
+
57
+ # Reset all configuration options to defaults
58
+ def reset
59
+ self.adapter = DEFAULT_ADAPTER
60
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
61
+ self.token = DEFAULT_TOKEN
62
+ self.endpoint = DEFAULT_ENDPOINT
63
+ self.proxy = DEFAULT_PROXY
64
+ self.user_agent = DEFAULT_USER_AGENT
65
+ self.gateway = DEFAULT_GATEWAY
66
+ self
67
+ end
68
+ end
69
+ end
@@ -1,38 +1,37 @@
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/request/basic_authentication'
6
- require 'faraday/response/raise_http_4xx'
7
- require 'faraday/response/raise_http_5xx'
1
+ require 'faraday'
2
+ require 'evrythng/core_ext/hash'
3
+ require 'evrythng/request/gateway'
4
+ require 'evrythng/request/token_authentication'
5
+ require 'evrythng/response/parse_json'
6
+ require 'evrythng/response/raise_client_error'
7
+ require 'evrythng/response/raise_server_error'
8
8
 
9
9
  module Evrythng
10
- # @private
11
10
  module Connection
12
11
  private
13
12
 
14
- def connection(format=format)
15
- options = {
13
+ # Returns a Faraday::Connection object
14
+ #
15
+ # @param options [Hash] A hash of options
16
+ # @return [Faraday::Connection]
17
+ def connection(options={})
18
+ default_options = {
16
19
  :headers => {
17
- :accept => "application/#{format}",
18
- :user_agent => user_agent
20
+ :accept => 'application/vnd.evrythng-v2+json',
21
+ :user_agent => user_agent,
19
22
  },
20
23
  :proxy => proxy,
21
24
  :ssl => {:verify => false},
22
- :url => api_endpoint,
25
+ :url => options.fetch(:endpoint, endpoint),
23
26
  }
24
-
25
- Faraday.new(options) do |builder|
26
- builder.use Faraday::Request::MultipartWithFile
27
- # builder.use Faraday::Request::EvrythngOAuth, authentication if authenticated?
28
- builder.use Faraday::Request::BasicAuthentication, username, password
27
+ Faraday.new(default_options.deep_merge(connection_options)) do |builder|
29
28
  builder.use Faraday::Request::Multipart
30
29
  builder.use Faraday::Request::UrlEncoded
31
- builder.use Faraday::Request::Gateway, gateway if gateway
32
- builder.use Faraday::Response::RaiseHttp4xx
33
- builder.use Faraday::Response::Mashify
34
- builder.use Faraday::Response::ParseJson
35
- builder.use Faraday::Response::RaiseHttp5xx
30
+ builder.use Evrythng::Request::Gateway, gateway if gateway
31
+ builder.use Evrythng::Request::TokenAuthentication, token
32
+ builder.use Evrythng::Response::RaiseClientError
33
+ builder.use Evrythng::Response::ParseJson unless options[:raw]
34
+ builder.use Evrythng::Response::RaiseServerError
36
35
  builder.adapter(adapter)
37
36
  end
38
37
  end
@@ -0,0 +1,19 @@
1
+ class Hash
2
+
3
+ # Merges self with another hash, recursively
4
+ #
5
+ # @param hash [Hash] The hash to merge
6
+ # @return [Hash]
7
+ def deep_merge(hash)
8
+ target = self.dup
9
+ hash.keys.each do |key|
10
+ if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
11
+ target[key] = target[key].deep_merge(hash[key])
12
+ next
13
+ end
14
+ target[key] = hash[key]
15
+ end
16
+ target
17
+ end
18
+
19
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/server_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 502
5
+ class Error::BadGateway < Evrythng::Error::ServerError
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/client_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 400
5
+ class Error::BadRequest < Evrythng::Error::ClientError
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns a 4xx HTTP status code
5
+ class Error::ClientError < Evrythng::Error
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'evrythng/error/client_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 420
5
+ class Error::EnhanceYourCalm < Evrythng::Error::ClientError
6
+ # The number of seconds your application should wait before requesting date from the Search API again
7
+ def retry_after
8
+ @http_headers.values_at('retry-after', 'Retry-After').detect{|value| value }.to_i
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/client_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 403
5
+ class Error::Forbidden < Evrythng::Error::ClientError
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/server_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 500
5
+ class Error::InternalServerError < Evrythng::Error::ServerError
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/client_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 406
5
+ class Error::NotAcceptable < Evrythng::Error::ClientError
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/client_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 404
5
+ class Error::NotFound < Evrythng::Error::ClientError
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns a 5xx HTTP status code
5
+ class Error::ServerError < Evrythng::Error
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/server_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 503
5
+ class Error::ServiceUnavailable < Evrythng::Error::ServerError
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'evrythng/error/client_error'
2
+
3
+ module Evrythng
4
+ # Raised when Evrythng returns the HTTP status code 401
5
+ class Error::Unauthorized < Evrythng::Error::ClientError
6
+ end
7
+ end
@@ -3,57 +3,34 @@ module Evrythng
3
3
  class Error < StandardError
4
4
  attr_reader :http_headers
5
5
 
6
+ # Initializes a new Error object
7
+ #
8
+ # @param message [String]
9
+ # @param http_headers [Hash]
10
+ # @return [Evrythng::Error]
6
11
  def initialize(message, http_headers)
7
12
  @http_headers = Hash[http_headers]
8
- super message
13
+ super(message)
9
14
  end
10
15
 
16
+ # @return [Time]
11
17
  def ratelimit_reset
12
18
  Time.at(@http_headers.values_at('x-ratelimit-reset', 'X-RateLimit-Reset').detect{|value| value}.to_i)
13
19
  end
14
20
 
21
+ # @return [Integer]
15
22
  def ratelimit_limit
16
23
  @http_headers.values_at('x-ratelimit-limit', 'X-RateLimit-Limit').detect{|value| value}.to_i
17
24
  end
18
25
 
26
+ # @return [Integer]
19
27
  def ratelimit_remaining
20
28
  @http_headers.values_at('x-ratelimit-remaining', 'X-RateLimit-Remaining').detect{|value| value}.to_i
21
29
  end
22
30
 
31
+ # @return [Integer]
23
32
  def retry_after
24
33
  [(ratelimit_reset - Time.now).ceil, 0].max
25
34
  end
26
35
  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
36
  end
@@ -0,0 +1,20 @@
1
+ require 'faraday'
2
+
3
+ module Evrythng
4
+ module Request
5
+ class Gateway < Faraday::Middleware
6
+
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
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ require 'faraday'
2
+
3
+ module Evrythng
4
+ module Request
5
+ class TokenAuthentication < Faraday::Middleware
6
+
7
+ def initialize(app, token, options={})
8
+ super(app)
9
+
10
+ @header_value = token
11
+ end
12
+
13
+ def call(env)
14
+ unless env[:request_headers]['X-Evrythng-Token']
15
+ env[:request_headers]['X-Evrythng-Token'] = @header_value
16
+ end
17
+ @app.call(env)
18
+ end
19
+
20
+ end
21
+ end
22
+ end