evrythng 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,40 +1,40 @@
1
1
  module Evrythng
2
2
  # Defines HTTP request methods
3
3
  module Request
4
+ # Perform an HTTP DELETE request
5
+ def delete(path, params={}, options={})
6
+ request(:delete, path, params, options)
7
+ end
8
+
4
9
  # Perform an HTTP GET request
5
- def get(path, options={}, format=format)
6
- request(:get, path, options, format)
10
+ def get(path, params={}, options={})
11
+ request(:get, path, params, options)
7
12
  end
8
13
 
9
14
  # Perform an HTTP POST request
10
- def post(path, options={}, format=format)
11
- request(:post, path, options, format)
15
+ def post(path, params={}, options={})
16
+ request(:post, path, params, options)
12
17
  end
13
18
 
14
19
  # Perform an HTTP PUT request
15
- def put(path, options={}, format=format)
16
- request(:put, path, options, format)
17
- end
18
-
19
- # Perform an HTTP DELETE request
20
- def delete(path, options={}, format=format)
21
- request(:delete, path, options, format)
20
+ def put(path, params={}, options={})
21
+ request(:put, path, params, options)
22
22
  end
23
23
 
24
24
  private
25
25
 
26
26
  # Perform an HTTP request
27
- def request(method, path, options, format)
28
- response = connection(format).send(method) do |request|
27
+ def request(method, path, params, options)
28
+ response = connection(options).send(method) do |request|
29
29
  case method.to_sym
30
- when :get, :delete
31
- request.url(path, options)
30
+ when :delete, :get
31
+ request.url(path, params)
32
32
  when :post, :put
33
33
  request.path = path
34
- request.body = options unless options.empty?
34
+ request.body = params unless params.empty?
35
35
  end
36
36
  end
37
- 'raw' == format.to_s.downcase ? response : response.body
37
+ options[:raw] ? response : response.body
38
38
  end
39
39
  end
40
40
  end
@@ -0,0 +1,23 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ module Evrythng
5
+ module Response
6
+ class ParseJson < Faraday::Response::Middleware
7
+
8
+ def parse(body)
9
+ case body
10
+ when ''
11
+ nil
12
+ when 'true'
13
+ true
14
+ when 'false'
15
+ false
16
+ else
17
+ ::MultiJson.decode(body)
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ require 'faraday'
2
+ require 'evrythng/error/bad_request'
3
+ require 'evrythng/error/enhance_your_calm'
4
+ require 'evrythng/error/forbidden'
5
+ require 'evrythng/error/not_acceptable'
6
+ require 'evrythng/error/not_found'
7
+ require 'evrythng/error/unauthorized'
8
+
9
+ module Evrythng
10
+ module Response
11
+ class RaiseClientError < Faraday::Response::Middleware
12
+
13
+ def on_complete(env)
14
+ case env[:status].to_i
15
+ when 400
16
+ raise Evrythng::Error::BadRequest.new(error_body(env[:body]), env[:response_headers])
17
+ when 401
18
+ raise Evrythng::Error::Unauthorized.new(error_body(env[:body]), env[:response_headers])
19
+ when 403
20
+ raise Evrythng::Error::Forbidden.new(error_body(env[:body]), env[:response_headers])
21
+ when 404
22
+ raise Evrythng::Error::NotFound.new(error_body(env[:body]), env[:response_headers])
23
+ when 406
24
+ raise Evrythng::Error::NotAcceptable.new(error_body(env[:body]), env[:response_headers])
25
+ when 420
26
+ raise Evrythng::Error::EnhanceYourCalm.new(error_body(env[:body]), env[:response_headers])
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def error_body(body)
33
+ if body.nil?
34
+ ''
35
+ elsif body['error']
36
+ body['error']
37
+ elsif body['errors']
38
+ first = Array(body['errors']).first
39
+ if first.kind_of?(Hash)
40
+ first['message'].chomp
41
+ else
42
+ first.chomp
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ require 'faraday'
2
+ require 'evrythng/error/bad_gateway'
3
+ require 'evrythng/error/internal_server_error'
4
+ require 'evrythng/error/service_unavailable'
5
+
6
+ module Evrythng
7
+ module Response
8
+ class RaiseServerError < Faraday::Response::Middleware
9
+
10
+ def on_complete(env)
11
+ case env[:status].to_i
12
+ when 500
13
+ raise Evrythng::Error::InternalServerError.new("Something is technically wrong.", env[:response_headers])
14
+ when 502
15
+ raise Evrythng::Error::BadGateway.new("Evrythng is down or being upgraded.", env[:response_headers])
16
+ when 503
17
+ raise Evrythng::Error::ServiceUnavailable.new("Evrythng is over capacity.", env[:response_headers])
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -1,4 +1,3 @@
1
1
  module Evrythng
2
- # The version of the gem
3
- VERSION = '0.0.5'.freeze unless defined?(::Evrythng::VERSION)
2
+ VERSION = '0.1.0'
4
3
  end
data/lib/evrythng.rb CHANGED
@@ -1,20 +1,18 @@
1
- require 'evrythng/api'
2
1
  require 'evrythng/client'
3
- require 'evrythng/configuration'
4
- require 'evrythng/error'
5
- require 'evrythng/search'
2
+ require 'evrythng/config'
6
3
 
7
4
  module Evrythng
8
- extend Configuration
5
+ extend Config
6
+
9
7
  class << self
10
8
  # Alias for Evrythng::Client.new
11
9
  #
12
10
  # @return [Evrythng::Client]
13
- def new(options={})
11
+ def new(options = {})
14
12
  Evrythng::Client.new(options)
15
13
  end
16
14
 
17
- # Delegate to evrythng::Client
15
+ # Delegate to Evrythng::Client
18
16
  def method_missing(method, *args, &block)
19
17
  return super unless new.respond_to?(method)
20
18
  new.send(method, *args, &block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evrythng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,132 +9,102 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-25 00:00:00.000000000 Z
12
+ date: 2012-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: maruku
16
- requirement: &70184618669300 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '0.6'
22
- type: :development
23
- prerelease: false
24
- version_requirements: *70184618669300
25
- - !ruby/object:Gem::Dependency
26
- name: nokogiri
27
- requirement: &70184618668780 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ~>
31
- - !ruby/object:Gem::Version
32
- version: '1.4'
33
- type: :development
34
- prerelease: false
35
- version_requirements: *70184618668780
36
14
  - !ruby/object:Gem::Dependency
37
15
  name: rake
38
- requirement: &70184618668220 !ruby/object:Gem::Requirement
16
+ requirement: &70257168078720 !ruby/object:Gem::Requirement
39
17
  none: false
40
18
  requirements:
41
- - - ~>
19
+ - - ! '>='
42
20
  - !ruby/object:Gem::Version
43
- version: '0.9'
21
+ version: '0'
44
22
  type: :development
45
23
  prerelease: false
46
- version_requirements: *70184618668220
24
+ version_requirements: *70257168078720
47
25
  - !ruby/object:Gem::Dependency
48
26
  name: rspec
49
- requirement: &70184618667680 !ruby/object:Gem::Requirement
27
+ requirement: &70257168078300 !ruby/object:Gem::Requirement
50
28
  none: false
51
29
  requirements:
52
- - - ~>
30
+ - - ! '>='
53
31
  - !ruby/object:Gem::Version
54
- version: '2.6'
32
+ version: '0'
55
33
  type: :development
56
34
  prerelease: false
57
- version_requirements: *70184618667680
35
+ version_requirements: *70257168078300
58
36
  - !ruby/object:Gem::Dependency
59
37
  name: simplecov
60
- requirement: &70184618667060 !ruby/object:Gem::Requirement
38
+ requirement: &70257168077880 !ruby/object:Gem::Requirement
61
39
  none: false
62
40
  requirements:
63
- - - ~>
41
+ - - ! '>='
64
42
  - !ruby/object:Gem::Version
65
- version: '0.4'
43
+ version: '0'
66
44
  type: :development
67
45
  prerelease: false
68
- version_requirements: *70184618667060
46
+ version_requirements: *70257168077880
69
47
  - !ruby/object:Gem::Dependency
70
48
  name: webmock
71
- requirement: &70184618666540 !ruby/object:Gem::Requirement
49
+ requirement: &70257168077460 !ruby/object:Gem::Requirement
72
50
  none: false
73
51
  requirements:
74
- - - ~>
52
+ - - ! '>='
75
53
  - !ruby/object:Gem::Version
76
- version: '1.7'
54
+ version: '0'
77
55
  type: :development
78
56
  prerelease: false
79
- version_requirements: *70184618666540
57
+ version_requirements: *70257168077460
80
58
  - !ruby/object:Gem::Dependency
81
59
  name: yard
82
- requirement: &70184618665940 !ruby/object:Gem::Requirement
60
+ requirement: &70257168077040 !ruby/object:Gem::Requirement
83
61
  none: false
84
62
  requirements:
85
- - - ~>
63
+ - - ! '>='
86
64
  - !ruby/object:Gem::Version
87
- version: '0.7'
65
+ version: '0'
88
66
  type: :development
89
67
  prerelease: false
90
- version_requirements: *70184618665940
68
+ version_requirements: *70257168077040
91
69
  - !ruby/object:Gem::Dependency
92
- name: ZenTest
93
- requirement: &70184618665460 !ruby/object:Gem::Requirement
70
+ name: json
71
+ requirement: &70257168076620 !ruby/object:Gem::Requirement
94
72
  none: false
95
73
  requirements:
96
- - - ~>
74
+ - - ! '>='
97
75
  - !ruby/object:Gem::Version
98
- version: '4.5'
76
+ version: '0'
99
77
  type: :development
100
78
  prerelease: false
101
- version_requirements: *70184618665460
79
+ version_requirements: *70257168076620
102
80
  - !ruby/object:Gem::Dependency
103
- name: hashie
104
- requirement: &70184618665000 !ruby/object:Gem::Requirement
81
+ name: activesupport
82
+ requirement: &70257168076080 !ruby/object:Gem::Requirement
105
83
  none: false
106
84
  requirements:
107
- - - ~>
85
+ - - ! '>='
108
86
  - !ruby/object:Gem::Version
109
- version: 1.1.0
110
- type: :runtime
111
- prerelease: false
112
- version_requirements: *70184618665000
113
- - !ruby/object:Gem::Dependency
114
- name: faraday
115
- requirement: &70184618664480 !ruby/object:Gem::Requirement
116
- none: false
117
- requirements:
118
- - - ~>
87
+ version: 2.3.9
88
+ - - <
119
89
  - !ruby/object:Gem::Version
120
- version: 0.7.4
90
+ version: '4'
121
91
  type: :runtime
122
92
  prerelease: false
123
- version_requirements: *70184618664480
93
+ version_requirements: *70257168076080
124
94
  - !ruby/object:Gem::Dependency
125
- name: faraday_middleware
126
- requirement: &70184618664000 !ruby/object:Gem::Requirement
95
+ name: faraday
96
+ requirement: &70257168075320 !ruby/object:Gem::Requirement
127
97
  none: false
128
98
  requirements:
129
99
  - - ~>
130
100
  - !ruby/object:Gem::Version
131
- version: 0.7.0
101
+ version: 0.8.0.rc2
132
102
  type: :runtime
133
103
  prerelease: false
134
- version_requirements: *70184618664000
104
+ version_requirements: *70257168075320
135
105
  - !ruby/object:Gem::Dependency
136
106
  name: multi_json
137
- requirement: &70184618663520 !ruby/object:Gem::Requirement
107
+ requirement: &70257168074860 !ruby/object:Gem::Requirement
138
108
  none: false
139
109
  requirements:
140
110
  - - ~>
@@ -142,20 +112,8 @@ dependencies:
142
112
  version: 1.0.0
143
113
  type: :runtime
144
114
  prerelease: false
145
- version_requirements: *70184618663520
146
- - !ruby/object:Gem::Dependency
147
- name: simple_oauth
148
- requirement: &70184618662980 !ruby/object:Gem::Requirement
149
- none: false
150
- requirements:
151
- - - ~>
152
- - !ruby/object:Gem::Version
153
- version: 0.1.5
154
- type: :runtime
155
- prerelease: false
156
- version_requirements: *70184618662980
157
- description: A Ruby wrapper for Evrythng API. Just proof of concept, do not consider
158
- this as a solid library.
115
+ version_requirements: *70257168074860
116
+ description: A Ruby wrapper for Evrythng API.
159
117
  email: graf.otodrakula@gmail.com
160
118
  executables: []
161
119
  extensions: []
@@ -167,24 +125,35 @@ files:
167
125
  - Rakefile
168
126
  - evrythng.gemspec
169
127
  - lib/evrythng.rb
170
- - lib/evrythng/api.rb
171
- - lib/evrythng/authentication.rb
128
+ - lib/evrythng/authenticatable.rb
172
129
  - lib/evrythng/client.rb
173
130
  - lib/evrythng/client/collections.rb
131
+ - lib/evrythng/client/properties.rb
132
+ - lib/evrythng/client/search.rb
174
133
  - lib/evrythng/client/thngs.rb
175
- - lib/evrythng/configuration.rb
134
+ - lib/evrythng/config.rb
176
135
  - lib/evrythng/connection.rb
136
+ - lib/evrythng/core_ext/hash.rb
177
137
  - lib/evrythng/error.rb
138
+ - lib/evrythng/error/bad_gateway.rb
139
+ - lib/evrythng/error/bad_request.rb
140
+ - lib/evrythng/error/client_error.rb
141
+ - lib/evrythng/error/enhance_your_calm.rb
142
+ - lib/evrythng/error/forbidden.rb
143
+ - lib/evrythng/error/internal_server_error.rb
144
+ - lib/evrythng/error/not_acceptable.rb
145
+ - lib/evrythng/error/not_found.rb
146
+ - lib/evrythng/error/server_error.rb
147
+ - lib/evrythng/error/service_unavailable.rb
148
+ - lib/evrythng/error/unauthorized.rb
178
149
  - lib/evrythng/request.rb
179
- - lib/evrythng/search.rb
150
+ - lib/evrythng/request/gateway.rb
151
+ - lib/evrythng/request/token_authentication.rb
152
+ - lib/evrythng/response/parse_json.rb
153
+ - lib/evrythng/response/raise_client_error.rb
154
+ - lib/evrythng/response/raise_server_error.rb
180
155
  - lib/evrythng/version.rb
181
- - lib/faraday/request/basic_authentication.rb
182
- - lib/faraday/request/evrythng_oauth.rb
183
- - lib/faraday/request/gateway.rb
184
- - lib/faraday/request/multipart_with_file.rb
185
- - lib/faraday/response/raise_http_4xx.rb
186
- - lib/faraday/response/raise_http_5xx.rb
187
- homepage: http://github.com/bai/evrythng-ruby
156
+ homepage: http://github.com/bai/evrythng
188
157
  licenses: []
189
158
  post_install_message:
190
159
  rdoc_options: []
@@ -204,8 +173,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
204
173
  version: '0'
205
174
  requirements: []
206
175
  rubyforge_project:
207
- rubygems_version: 1.8.7
176
+ rubygems_version: 1.8.15
208
177
  signing_key:
209
178
  specification_version: 3
210
- summary: A Ruby wrapper for Evrythng API
179
+ summary: Evrythng API wrapper
211
180
  test_files: []
data/lib/evrythng/api.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'evrythng/authentication'
2
- require 'evrythng/configuration'
3
- require 'evrythng/connection'
4
- require 'evrythng/request'
5
-
6
- module Evrythng
7
- # @private
8
- class API
9
- include Connection
10
- include Request
11
- include Authentication
12
-
13
- # @private
14
- attr_accessor *Configuration::VALID_OPTIONS_KEYS
15
-
16
- # Creates a new API
17
- def initialize(options={})
18
- options = Evrythng.options.merge(options)
19
- Configuration::VALID_OPTIONS_KEYS.each do |key|
20
- send("#{key}=", options[key])
21
- end
22
- end
23
- end
24
- end
@@ -1,25 +0,0 @@
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
@@ -1,102 +0,0 @@
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
- :username,
13
- :password,
14
- :api_endpoint,
15
- :format,
16
- :gateway,
17
- :oauth_token,
18
- :oauth_token_secret,
19
- :proxy,
20
- :search_endpoint,
21
- :user_agent].freeze
22
-
23
- # An array of valid request/response formats
24
- VALID_FORMATS = [
25
- 'vnd.evrythng-v1+json'
26
- ].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
- # By default, don't set a username
40
- DEFAULT_USERNAME = nil
41
-
42
- # By default, don't set password
43
- DEFAULT_PASSWORD = nil
44
-
45
- # The endpoint that will be used to connect if none is set
46
- DEFAULT_ENDPOINT = 'http://evrythng.net'.freeze
47
-
48
- # The response format appended to the path and sent in the 'Accept' header if none is set
49
- DEFAULT_FORMAT = 'vnd.evrythng-v1+json'.freeze
50
-
51
- # By default, don't set a user oauth token
52
- DEFAULT_OAUTH_TOKEN = nil
53
-
54
- # By default, don't set a user oauth secret
55
- DEFAULT_OAUTH_TOKEN_SECRET = nil
56
-
57
- # By default, don't use a proxy server
58
- DEFAULT_PROXY = nil
59
-
60
- # The user agent that will be sent to the API endpoint if none is set
61
- DEFAULT_USER_AGENT = "Evrythng Ruby Gem #{Evrythng::VERSION}".freeze
62
-
63
- DEFAULT_GATEWAY = nil
64
-
65
- # @private
66
- attr_accessor *VALID_OPTIONS_KEYS
67
-
68
- # When this module is extended, set all configuration options to their default values
69
- def self.extended(base)
70
- base.reset
71
- end
72
-
73
- # Convenience method to allow configuration options to be set in a block
74
- def configure
75
- yield self
76
- end
77
-
78
- # Create a hash of options and their values
79
- def options
80
- options = {}
81
- VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
82
- options
83
- end
84
-
85
- # Reset all configuration options to defaults
86
- def reset
87
- self.adapter = DEFAULT_ADAPTER
88
- self.consumer_key = DEFAULT_CONSUMER_KEY
89
- self.consumer_secret = DEFAULT_CONSUMER_SECRET
90
- self.username = DEFAULT_USERNAME
91
- self.password = DEFAULT_PASSWORD
92
- self.api_endpoint = DEFAULT_ENDPOINT
93
- self.format = DEFAULT_FORMAT
94
- self.oauth_token = DEFAULT_OAUTH_TOKEN
95
- self.oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET
96
- self.proxy = DEFAULT_PROXY
97
- self.user_agent = DEFAULT_USER_AGENT
98
- self.gateway = DEFAULT_GATEWAY
99
- self
100
- end
101
- end
102
- end