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,173 +0,0 @@
1
- require 'cgi'
2
-
3
- module Evrythng
4
- # Wrapper for the Evrythng Search API
5
- class Search < API
6
- include Enumerable
7
-
8
- # @private
9
- attr_reader :query
10
-
11
- # Creates a new search
12
- #
13
- # @example Initialize an Evrythng search
14
- # search = Evrythng::Search.new
15
- def initialize(*)
16
- clear
17
- super
18
- end
19
-
20
- # Clears all query filters and cached results
21
- #
22
- # @return [Evrythng::Search] self
23
- # @example Clear a search for "evrythng"
24
- # search = Evrythng::Search.new
25
- # search.containing("bike").fetch
26
- # search.clear
27
- # search.fetch_next_page #=> 403 Forbidden: You must enter a query.
28
- def clear
29
- @cache = nil
30
- @query = {}
31
- @query[:q] = []
32
- self
33
- end
34
-
35
- # @group Generic filters
36
-
37
- # Search query
38
- #
39
- # @param query [String] The search query.
40
- # @return [Evrythng::Search] self
41
- # @example Return an array of thngs containing "bike"
42
- # Evrythng::Search.new.containing("bike").fetch
43
- def containing(query)
44
- @query[:q] << query
45
- self
46
- end
47
-
48
- # Negative search query
49
- #
50
- # @param query [String] The negative search query.
51
- # @return [Evrythng::Search] self
52
- # @example Return an array of thngs containing "bike" but not "mountain"
53
- # Evrythng::Search.new.containing("bike").not_containing("mountain").fetch
54
- def not_containing(query)
55
- @query[:q] << "-#{query}"
56
- self
57
- end
58
-
59
- # Only include thngs from users in a given radius of a given location
60
- #
61
- # @param lat [Float] A latitude.
62
- # @param long [Float] A longitude.
63
- # @param radius [String] A search radius, specified in either 'mi' (miles) or 'km' (kilometers).
64
- # @return [Evrythng::Search] self
65
- # @example Return an array of thngs within a 1-kilometer radius of Barcelona
66
- # Evrythng::Search.new.containing("bike").geocode(41.38, 2.18, "1km").fetch
67
- def geocode(lat, long, radius)
68
- @query[:geocode] = [lat, long, radius].join(",")
69
- self
70
- end
71
-
72
- # @group User filters
73
-
74
- # Only include thngs owned by a given user, specified by screen_name
75
- #
76
- # @param screen_name [String] A Evrythng user name.
77
- # @return [Evrythng::Search] self
78
- # @example Return an array of thngs containing "bike" from "mike"
79
- # Evrythng::Search.new.containing("bike").from("mike").fetch
80
- def from(screen_name)
81
- @query[:q] << "from:#{screen_name}"
82
- self
83
- end
84
-
85
- # Exclude thngs from a given user, specified by screen_name
86
- #
87
- # @param screen_name [String] A Evrythng user name.
88
- # @return [Evrythng::Search] self
89
- # @example Return an array of thngs containing "bike" from everyone except "mike"
90
- # Evrythng::Search.new.containing("bike").not_from("mike").fetch
91
- def not_from(screen_name)
92
- @query[:q] << "-from:#{screen_name}"
93
- self
94
- end
95
-
96
- # @group Paging
97
-
98
- # Specify the number of thngs to return per page
99
- #
100
- # @param number [Integer] The number of thngs to return per page, up to a max of 100.
101
- # @return [Evrythng::Search] self
102
- # @example Return an array of 100 thngs containing "bike"
103
- # Evrythng::Search.new.containing("bike").per_page(100).fetch
104
- def per_page(number=15)
105
- @query[:per_page] = number
106
- self
107
- end
108
-
109
- # Specify the page number to return, up to a maximum of roughly 500 results
110
- #
111
- # @param number [Integer] The page number (starting at 1) to return, up to a max of roughly 500 results (based on {Evrythng::Client::Search#per_page} * {Evrythng::Client::Search#page}).
112
- # @return [Evrythng::Search] self
113
- # @example Return the second page of thngs containing "bike"
114
- # Evrythng::Search.new.containing("bike").page(2).fetch
115
- def page(number)
116
- @query[:page] = number
117
- self
118
- end
119
-
120
- # Indicates if there are additional results to be fetched
121
- #
122
- # @return [Boolean]
123
- # @example
124
- # search = Evrythng::Search.new.containing("bike").fetch
125
- # search.next_page? #=> true
126
- def next_page?
127
- fetch if @cache.nil?
128
- !!@cache["next_page"]
129
- end
130
-
131
- # @group Fetching
132
-
133
- # Fetch the next page of results of the query
134
- #
135
- # @return [Array] Thngs that match specified query.
136
- # @example Return the first two pages of results
137
- # search = Evrythng::Search.new.containing("bike").fetch
138
- # search.fetch_next_page
139
- def fetch_next_page
140
- if next_page?
141
- @cache = get("search", CGI.parse(@cache["next_page"][1..-1]), :json)
142
- @cache.results
143
- end
144
- end
145
-
146
- # Fetch the results of the query
147
- #
148
- # @param force [Boolean] Ignore the cache and hit the API again.
149
- # @return [Array] Thngs that match specified query.
150
- # @example Return an array of thngs containing "bike"
151
- # search = Evrythng::Search.new.containing("bike").fetch
152
- def fetch(force=false)
153
- if @cache.nil? || force
154
- options = query.dup
155
- options[:q] = options[:q].join(" ")
156
- @cache = get("search", options, :json)
157
- end
158
- @cache.results
159
- end
160
-
161
- # Calls block once for each element in self, passing that element as a parameter
162
- #
163
- # @yieldparam [Hashie::Mash] result Thngs that matches specified query.
164
- # @return [Array] Thngs that match specified query.
165
- # @example
166
- # Evrythng::Search.new.containing('cafe del mar').each do |result|
167
- # puts "#{result.identifier} by #{result.owner}"
168
- # end
169
- def each
170
- fetch.each{|result| yield result}
171
- end
172
- end
173
- end
@@ -1,15 +0,0 @@
1
- require 'base64'
2
-
3
- module Faraday
4
- class Request::BasicAuthentication < Faraday::Middleware
5
- def initialize(app, login, pass)
6
- super(app)
7
- @header_value = "Basic #{Base64.encode64([login, pass].join(':')).gsub("\n", '')}"
8
- end
9
-
10
- def call(env)
11
- env[:request_headers]['Authorization'] = @header_value
12
- @app.call(env)
13
- end
14
- end
15
- end
@@ -1,24 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,34 +0,0 @@
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
24
- 'image/jpeg'
25
- when /\.gif$/i
26
- 'image/gif'
27
- when /\.png$/i
28
- 'image/png'
29
- else
30
- 'application/octet-stream'
31
- end
32
- end
33
- end
34
- end
@@ -1,45 +0,0 @@
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
@@ -1,24 +0,0 @@
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