lieu 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +34 -0
  3. data/LICENSE +20 -0
  4. data/README.md +112 -0
  5. data/Rakefile +42 -0
  6. data/lib/lieu/client/actions.rb +105 -0
  7. data/lib/lieu/client/autocomplete.rb +21 -0
  8. data/lib/lieu/client/details.rb +21 -0
  9. data/lib/lieu/client/photos.rb +23 -0
  10. data/lib/lieu/client/query_autocomplete.rb +21 -0
  11. data/lib/lieu/client/search.rb +79 -0
  12. data/lib/lieu/client.rb +36 -0
  13. data/lib/lieu/configuration.rb +45 -0
  14. data/lib/lieu/connection.rb +28 -0
  15. data/lib/lieu/error.rb +58 -0
  16. data/lib/lieu/request.rb +53 -0
  17. data/lib/lieu/response/raise_error.rb +18 -0
  18. data/lib/lieu/version.rb +5 -0
  19. data/lib/lieu.rb +25 -0
  20. data/lieu.gemspec +26 -0
  21. data/spec/cassettes/Lieu_Client_Actions/_event_details/returns_an_event_details.yml +54 -0
  22. data/spec/cassettes/Lieu_Client_Autocomplete/_autocomplete/returns_an_array_of_places.yml +203 -0
  23. data/spec/cassettes/Lieu_Client_Details/_details/returns_a_place_details.yml +220 -0
  24. data/spec/cassettes/Lieu_Client_Photos/_photo/returns_a_photo_image.yml +111 -0
  25. data/spec/cassettes/Lieu_Client_QueryAutocomplete/_query_autocomplete/returns_an_array_of_places.yml +203 -0
  26. data/spec/cassettes/Lieu_Client_Search/_nearby_search/returns_an_array_of_places.yml +517 -0
  27. data/spec/cassettes/Lieu_Client_Search/_radarsearch/returns_an_array_of_places.yml +1683 -0
  28. data/spec/cassettes/Lieu_Client_Search/_textsearch/returns_an_array_of_places.yml +560 -0
  29. data/spec/cassettes/Lieu_Request/given_a_GET_request/with_options/sets_options_as_url_parameters.yml +63 -0
  30. data/spec/cassettes/Lieu_Request/given_a_POST_request/with_options/sets_options_as_body_parameters.yml +63 -0
  31. data/spec/lib/lieu/client/actions_spec.rb +178 -0
  32. data/spec/lib/lieu/client/autocomplete_spec.rb +11 -0
  33. data/spec/lib/lieu/client/details_spec.rb +11 -0
  34. data/spec/lib/lieu/client/photo_spec.rb +17 -0
  35. data/spec/lib/lieu/client/query_autocomplete_spec.rb +11 -0
  36. data/spec/lib/lieu/client/search_spec.rb +39 -0
  37. data/spec/lib/lieu/client_spec.rb +70 -0
  38. data/spec/lib/lieu/request_spec.rb +35 -0
  39. data/spec/lib/lieu_spec.rb +32 -0
  40. data/spec/spec_helper.rb +65 -0
  41. data/spec/support/shared_examples/error.rb +33 -0
  42. metadata +120 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e394c13ae4c6fff4a3931320af5dbe933017417e
4
+ data.tar.gz: 9e3b609e92d29e93fee60ce0bb1ece5d4bf1981a
5
+ SHA512:
6
+ metadata.gz: 793d79dccd5240f699cb4261f5c714a3a34402a5c88c1d2052158a1593699dade407cff69322c1a34bab82e3b033a12aee502586a078ae4cda9fc0ba472052bf
7
+ data.tar.gz: df5a98efd715f561f8d0da18788ea6038aab058a6e95de37d445ae397f19bb721a37b9eca40f1ac96b5892dd35934265b974bc84909f534988786221774d7bda
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,34 @@
1
+ # Contributing
2
+
3
+ ## Running the test suite
4
+
5
+ bundle exec rake spec
6
+
7
+ ### Code quality
8
+
9
+ Code quality is checked with [cane](https://github.com/square/cane) configured in `Rakefile`.
10
+
11
+ Build fails if code quality thresholds **are not met**.
12
+
13
+ ### Code coverage
14
+
15
+ When running the test suite a test coverage report is generated with [SimpleCov](https://github.com/colszowka/simplecov) in `coverage/index.html`.
16
+
17
+ Build fails if coverage is below **99%**.
18
+
19
+ ## Writing new tests
20
+ Lieu uses [VCR](https://github.com/vcr/vcr) for recording and playing back API fixtures (in `spec/cassettes`) during test runs.
21
+
22
+ If you need to record new cassettes you have to provide your Google Places Api key in `ENV['GOOGLE_PLACES_API_KEY']` (stripped automaticaly from the cassette before being saved on disk).
23
+
24
+ ## Documentation
25
+
26
+ Documentation is generated with [YARD](http://yardoc.org/).
27
+
28
+ ## Workflow
29
+
30
+ 1. Fork [Lieu](https://github.com/sush/lieu)
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) [2013] [Aylic Petit]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ ![Lieu](https://raw.github.com/sush/lieu/master/lieu.png)
2
+
3
+ > The word *lieu* originally comes from the Latin *locus*, meaning *place*, and its meaning has stayed true to its origins ever since.
4
+
5
+ Simple Ruby wrapper for the [Google Places API](https://developers.google.com/places/documentation).
6
+
7
+ **Current version**: [![Gem Version](https://badge.fury.io/rb/lieu.png)](http://badge.fury.io/rb/lieu)
8
+
9
+ **Build status**:        [![Build Status](https://secure.travis-ci.org/Sush/lieu.png?branch=master)](http://travis-ci.org/Sush/lieu)
10
+
11
+ **Code metrics**:
12
+      [![Code Climate](https://codeclimate.com/github/Sush/lieu.png)](https://codeclimate.com/github/Sush/lieu)
13
+ [![Coverage Status](https://coveralls.io/repos/Sush/lieu/badge.png?branch=master)](https://coveralls.io/r/Sush/lieu?branch=master)
14
+
15
+ **Ruby support**:
16
+
17
+ - 1.9.2
18
+ - 1.9.3
19
+ - 2.0.0
20
+
21
+ ## Installation
22
+
23
+ Install via Rubygems
24
+
25
+ gem install lieu
26
+
27
+ or add to your Gemfile
28
+
29
+ gem 'lieu', '~> 1.0'
30
+
31
+ ### Configuration
32
+
33
+ API methods are available as module methods
34
+
35
+ ```ruby
36
+ Lieu.configure do |c|
37
+ c.api_endpoint = 'http://newendpoint/'
38
+ c.api_key = 'YOUR_API_KEY'
39
+ c.sensor = true
40
+ end
41
+ ```
42
+
43
+ or as client instance methods
44
+
45
+ ```ruby
46
+ Lieu::Client.new(
47
+ api_endpoint: 'http://newendpoint/',
48
+ api_key: 'YOUR_API_KEY',
49
+ sensor: true
50
+ )
51
+ ```
52
+
53
+ The `sensor` parameter can be set globally (default to `false`) or can be provided for every request by passing the optional parameter `sensor: true`.
54
+
55
+ ## Authentication
56
+
57
+ Lieu only supports authentication via an API key.
58
+
59
+ You can request one following these [steps](https://developers.google.com/places/documentation/#Authentication).
60
+
61
+ ## Usage
62
+
63
+
64
+ ```ruby
65
+ place = Lieu.details('CmRYAAAA...')
66
+ puts place.name
67
+ # => "Google Sydney"
68
+
69
+ places = Lieu.text_search('Google Sydney')
70
+ places.first.formatted_address
71
+ # => "529 Kent Street, Sydney NSW, Australia"
72
+
73
+ # etc…
74
+ ```
75
+
76
+ ## Features
77
+
78
+ Lieu supports all the Google Places API methods:
79
+
80
+ - [Search](http://rubydoc.info/gems/lieu/Lieu/Client/Search)
81
+ - [Details](http://rubydoc.info/gems/lieu/Lieu/Client/Details)
82
+ - [Actions](http://rubydoc.info/gems/lieu/Lieu/Client/Actions)
83
+ - [Photos](http://rubydoc.info/gems/lieu/Lieu/Client/Photos)
84
+ - [Autocomplete](http://rubydoc.info/gems/lieu/Lieu/Client/Autocomplete)
85
+ - [Query autocomplete](http://rubydoc.info/gems/lieu/Lieu/Client/QueryAutocomplete)
86
+
87
+ Complete Lieu public API's documentation [here](http://rubydoc.info/gems/lieu/frames).
88
+
89
+
90
+ ## Similar libraries
91
+
92
+ - [google_places](https://github.com/marceldegraaf/google_places)
93
+
94
+ ## Versioning
95
+ Lieu follows the principles of [semantic versioning](http://semver.org).
96
+
97
+ 1. Patch level releases contain only bug fixes.
98
+ 2. Minor releases contain backward-compatible new features.
99
+ 3. Major new releases contain backwards-incompatible changes to the public API.
100
+
101
+ ## Contributing
102
+
103
+ Pull Requests are welcome !
104
+
105
+ Please refer to the [Contributing guide](https://github.com/sush/lieu/master/CONTRIBUTING.md) for more details on how to run the test suite and to contribute.
106
+
107
+
108
+ ## Copyright
109
+
110
+ Copyright © 2013 Aylic Petit
111
+
112
+ Released under the terms of the MIT licence. See the [LICENSE](https://github.com/sush/lieu/master/LICENSE) file for more details.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ begin
8
+ require 'cane/rake_task'
9
+
10
+ desc 'Run cane to check quality metrics'
11
+ Cane::RakeTask.new(:quality) do |cane|
12
+ cane.add_threshold 'coverage/.last_run.json', :>=, 99
13
+ cane.abc_glob = '{lib}/**/*.rb'
14
+ cane.abc_max = 15
15
+ cane.style_measure = 105
16
+ cane.style_exclude = %w[spec/spec_helper.rb]
17
+ cane.canefile = '.cane'
18
+ end
19
+ rescue LoadError
20
+ end
21
+
22
+ namespace :doc do
23
+ begin
24
+ require 'yard'
25
+
26
+ YARD::Rake::YardocTask.new do |task|
27
+ task.files = ['README.md', 'LICENSE.md', 'lib/**/*.rb']
28
+ task.options = [
29
+ '--output-dir', 'doc/yard',
30
+ '--markup', 'markdown'
31
+ ]
32
+ end
33
+ rescue LoadError
34
+ end
35
+ end
36
+
37
+ Rake::Task[:spec].enhance do
38
+ Rake::Task[:quality].invoke
39
+ end
40
+
41
+ task :test => :spec
42
+ task :default => :spec
@@ -0,0 +1,105 @@
1
+ module Lieu
2
+ class Client
3
+ # Methods for the Place Actions API
4
+ #
5
+ # @see https://developers.google.com/places/documentation/actions
6
+ module Actions
7
+ # Add a place.
8
+ # @param options [Hash] a customizable set of options
9
+ # @option options [Hash] :location the location of place
10
+ # * :lat (String) the place latitude
11
+ # * :lng (String) the place longitude
12
+ # @option options [String] :accuracy the accuracy of location
13
+ # @option options [String] :name the place name
14
+ # @option options [Array] :types the place types
15
+ # @option options [String] :language the place name language
16
+ # @return [Hashie::Mash] the created place details
17
+ # @see https://developers.google.com/places/documentation/actions#PlaceReportRequests
18
+ # @example
19
+ # Lieu.add(
20
+ # reference: 'place_reference',
21
+ # location: {
22
+ # lat: '0.00',
23
+ # lng: '0.00'
24
+ # },
25
+ # name: 'Fuubar',
26
+ # types: [
27
+ # 'shoe_store'
28
+ # ],
29
+ # language: 'fr'
30
+ # )
31
+ def add(options={})
32
+ post('add', options)
33
+ end
34
+
35
+ # Delete a place.
36
+ # @param options [Hash] a customizable set of options
37
+ # @option options [String] :reference the place reference
38
+ # @return [Boolean] true if removal was successful, false otherwise
39
+ # @see https://developers.google.com/places/documentation/actions#PlaceReportRequests
40
+ # @example
41
+ # Lieu.delete(reference: 'place_reference')
42
+ def delete(options={})
43
+ boolean_from_response(:post, 'delete', options)
44
+ end
45
+
46
+ # Bump a place or an event.
47
+ # @param options [Hash] a customizable set of options
48
+ # @option options [String] :reference the place reference
49
+ # @option options [String] :event_id the event id
50
+ # @return [Boolean] true if bump was successful, false otherwise
51
+ # @see https://developers.google.com/places/documentation/actions#bump
52
+ # @example Bump a place
53
+ # Lieu.bump(reference: 'place_reference')
54
+ # @example Bump an event
55
+ # Lieu.bump(reference: 'place_reference', event_id: 'event_id')
56
+ def bump(options={})
57
+ boolean_from_response(:post, 'bump', options)
58
+ end
59
+
60
+ # Add an event to a place.
61
+ # @param options [Hash] a customizable set of options
62
+ # @option options [String] :reference the place reference
63
+ # @option options [String] :duration the event duration
64
+ # @option options [String] :language the event language
65
+ # @option options [String] :summary the event summary
66
+ # @option options [String] :url the event url
67
+ # @return [Hashie::Mash] the created event details
68
+ # @see https://developers.google.com/places/documentation/actions#event_intro
69
+ # @example
70
+ # Lieu.add_event(
71
+ # reference: 'place_reference',
72
+ # duration: '15',
73
+ # language: 'fr',
74
+ # summary: 'Nice event',
75
+ # url: 'http://fuubar.com/events/fuubar/'
76
+ # )
77
+ def add_event(options={})
78
+ post('event/add', options)
79
+ end
80
+
81
+ # Delete an event from a place.
82
+ # @param options [Hash] a customizable set of options
83
+ # @option options [String] :reference the place reference
84
+ # @option options [String] :event_id the event id
85
+ # @return [Boolean] true if deletion was successful, false otherwise
86
+ # @see https://developers.google.com/places/documentation/actions#event_intro
87
+ # @example
88
+ # Lieu.delete_event(reference: 'place_reference', event_id: 'event_id')
89
+ def delete_event(options={})
90
+ boolean_from_response(:post, 'event/delete', options)
91
+ end
92
+
93
+ # Get an event details.
94
+ # @param reference [String] the event place reference
95
+ # @param event_id [String] the event id
96
+ # @param options [Hash] optionnal parameters
97
+ # @return [Hashie::Mash] the event details
98
+ def event_details(reference, event_id, options={})
99
+ options.merge!(reference: reference, event_id: event_id)
100
+
101
+ get('event/details', options).result
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,21 @@
1
+ module Lieu
2
+ class Client
3
+ # Methods for the Place Autocomplete API
4
+ #
5
+ # @see https://developers.google.com/places/documentation/autocomplete
6
+ module Autocomplete
7
+ # Search a place.
8
+ # @param input [String] the search term
9
+ # @param options [Hash] optional parameters
10
+ # @return [Array<Hashie::Mash>] a list of predicted places
11
+ # @see https://developers.google.com/places/documentation/autocomplete#place_autocomplete_requests
12
+ # @example
13
+ # Lieu.autocomplete('Le Zenith Paris')
14
+ def autocomplete(input, options={})
15
+ options.merge!(input: input)
16
+
17
+ get('autocomplete', options).predictions
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Lieu
2
+ class Client
3
+ # Methods for the Place Details API
4
+ #
5
+ # @see https://developers.google.com/places/documentation/details
6
+ module Details
7
+ # Get a place details.
8
+ # @param reference [String] the place reference
9
+ # @param options [Hash] optional parameters
10
+ # @return [Hashie::Mash] the place details
11
+ # @see https://developers.google.com/places/documentation/details#PlaceDetailsRequests
12
+ # @example
13
+ # Lieu.details('place_reference')
14
+ def details(reference, options={})
15
+ options.merge!(reference: reference)
16
+
17
+ get('details', options).result
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module Lieu
2
+ class Client
3
+ # Methods for the Place Photos API
4
+ #
5
+ # @see https://developers.google.com/places/documentation/photos
6
+ module Photos
7
+ # Get a photo.
8
+ # @param photo_reference [String] the photo reference
9
+ # @param options [Hash] a customizable set of options
10
+ # @option options [String] :maxheight the maximum desired height in pixels
11
+ # @option options [String] :maxwidth the maximum desired width in pixels
12
+ # @return [String] the raw image
13
+ # @see https://developers.google.com/places/documentation/photos#place_photo_requests
14
+ # @example
15
+ # Lieu.photo('photo_reference', maxheight: '15', maxwidth: '15')
16
+ def photo(photo_reference, options={})
17
+ options.merge!(photoreference: photo_reference, json: false)
18
+
19
+ get('photo', options)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Lieu
2
+ class Client
3
+ # Methods for the Query Autocomplete API
4
+ #
5
+ # @see https://developers.google.com/places/documentation/query
6
+ module QueryAutocomplete
7
+ # Perform a categorical search.
8
+ # @param input [String] the search term
9
+ # @param options [Hash] optional parameters
10
+ # @return [Array<Hashie::Mash>] a list of predicted places
11
+ # @see https://developers.google.com/places/documentation/query#query_autocomplete_requests
12
+ # @example
13
+ # Lieu.query_autocomplete('Pizza Paris')
14
+ def query_autocomplete(input, options={})
15
+ options.merge!(input: input)
16
+
17
+ get('queryautocomplete', options).predictions
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,79 @@
1
+ module Lieu
2
+ class Client
3
+ # Methods for the Place Search API
4
+ #
5
+ # @see https://developers.google.com/places/documentation/search
6
+ module Search
7
+ # Search places within an area.
8
+ # @param options [Hash] a customizable set of options
9
+ # @option options [Hash] :location the location to find around
10
+ # * :lat (String) the latitude to find around
11
+ # * :lng (String) the longitude to find around
12
+ # @option options [String] :radius the distance (in meters) to search in
13
+ # @return [Array<Hashie::Mash>] a list of places details
14
+ # @see https://developers.google.com/places/documentation/search#PlaceSearchRequests
15
+ # @example
16
+ # Lieu.nearby_search(location: {
17
+ # lat: '0.00',
18
+ # lng: '0.00'
19
+ # },
20
+ # radius: '500'
21
+ # )
22
+ def nearby_search(options={})
23
+ options[:location] = format_location(options[:location])
24
+
25
+ get('nearbysearch', options).results
26
+ end
27
+
28
+ # Text search places.
29
+ # @param query [String] the text search term
30
+ # @param options [Hash] optional parameters
31
+ # @return [Array<Hashie::Mash>] a list of places details
32
+ # @see https://developers.google.com/places/documentation/search#TextSearchRequests
33
+ # @example
34
+ # Lieu.text_search('Tour Eiffel')
35
+ def text_search(query, options={})
36
+ options.merge!(query: query)
37
+
38
+ get('textsearch', options).results
39
+ end
40
+
41
+ # Radar search places within an area.
42
+ # @param options [Hash] a customizable set of options
43
+ # @option options [Hash] :location the location to find around
44
+ # * :lat (String) the latitude to find around
45
+ # * :lng (String) the longitude to find around
46
+ # @option options [String] :radius the distance (in meters) to search in
47
+ # @option options [String] :keyword a term to be matched against all content for this place
48
+ # @option options [String] :name a term to be matched against the names of places
49
+ # @option options [Array<String>] :types types to restrict the results to
50
+ # @return [Array<Hashie::Mash>] a list of places minimal details
51
+ # @see https://developers.google.com/places/documentation/search#RadarSearchRequests
52
+ # @example
53
+ # Lieu.radar_search(location: {
54
+ # lat: '0.00',
55
+ # lng: '0.00'
56
+ # },
57
+ # radius: '500',
58
+ # keyword: 'Pizza',
59
+ # name: 'Express'
60
+ # )
61
+ def radar_search(options={})
62
+ options[:location] = format_location(options[:location])
63
+ options[:types] = format_types(options[:types]) if options[:types]
64
+
65
+ get('radarsearch', options).results
66
+ end
67
+
68
+ private
69
+
70
+ def format_location(location)
71
+ "#{location[:lat]},#{location[:lng]}"
72
+ end
73
+
74
+ def format_types(types)
75
+ types.join('|')
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,36 @@
1
+ require 'lieu/connection'
2
+ require 'lieu/request'
3
+
4
+ require 'lieu/client/details'
5
+ require 'lieu/client/search'
6
+ require 'lieu/client/autocomplete'
7
+ require 'lieu/client/query_autocomplete'
8
+ require 'lieu/client/photos'
9
+ require 'lieu/client/actions'
10
+
11
+ module Lieu
12
+ # Client for the Google Places API
13
+ #
14
+ # @see https://developers.google.com/places/documentation
15
+ class Client
16
+ attr_accessor(*Configuration::VALID_OPTIONS)
17
+
18
+ def initialize(options={})
19
+ options = Lieu.options.merge(options)
20
+
21
+ Configuration::VALID_OPTIONS.each do |key|
22
+ send("#{key}=", options[key])
23
+ end
24
+ end
25
+
26
+ include Lieu::Connection
27
+ include Lieu::Request
28
+
29
+ include Lieu::Client::Details
30
+ include Lieu::Client::Search
31
+ include Lieu::Client::Autocomplete
32
+ include Lieu::Client::QueryAutocomplete
33
+ include Lieu::Client::Photos
34
+ include Lieu::Client::Actions
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ module Lieu
2
+ # Methods for Lieu configuration
3
+ module Configuration
4
+ # Configurable options
5
+ VALID_OPTIONS = [
6
+ :api_endpoint,
7
+ :api_key,
8
+ :sensor
9
+ ].freeze
10
+
11
+ # Default Google Place API endpoint
12
+ DEFAULT_API_ENDPOINT = 'https://maps.googleapis.com/maps/api/place/'
13
+
14
+ attr_accessor(*VALID_OPTIONS)
15
+
16
+ # @private
17
+ def self.extended(base_obj)
18
+ base_obj.initialize_default_options
19
+ end
20
+
21
+ # Set configuration options using a block
22
+ def configure
23
+ yield self
24
+ end
25
+
26
+ # Default options
27
+ def options
28
+ VALID_OPTIONS.inject({}){ |o, k| o.merge!(k => send(k)) }
29
+ end
30
+
31
+ # Initialize default options
32
+ def initialize_default_options
33
+ self.api_endpoint = DEFAULT_API_ENDPOINT
34
+ self.api_key = nil
35
+ self.sensor = false
36
+ end
37
+
38
+ # Reset configuration options to default values
39
+ def reset!
40
+ initialize_default_options
41
+
42
+ self
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,28 @@
1
+ require 'faraday_middleware'
2
+ require 'lieu/response/raise_error'
3
+
4
+ module Lieu
5
+ # Faraday connection methods
6
+ module Connection
7
+ private
8
+
9
+ def connection
10
+ options = {
11
+ ssl: { verify: false },
12
+ url: self.api_endpoint
13
+ }
14
+
15
+ connection = Faraday.new(options) do |conn|
16
+ conn.response(:mashify)
17
+ conn.response(:json, content_type: /\bjson$/)
18
+
19
+ conn.use(Lieu::Response::RaiseError)
20
+ conn.use(FaradayMiddleware::FollowRedirects, limit: 3)
21
+
22
+ conn.adapter(Faraday.default_adapter)
23
+ end
24
+
25
+ connection
26
+ end
27
+ end
28
+ end
data/lib/lieu/error.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'multi_json'
2
+
3
+ module Lieu
4
+ # Custom error class
5
+ class Error < StandardError
6
+ # Returns the Error based on status and response message.
7
+ #
8
+ # @param [Hash] response HTTP response
9
+ # @return [Lieu::Error]
10
+ def self.from_response(response)
11
+ content_type = response[:response_headers]['content-type']
12
+
13
+ status =
14
+ if content_type =~ /json/
15
+ ::MultiJson.load(response[:body])['status']
16
+ else
17
+ response[:status]
18
+ end
19
+
20
+ case status
21
+ when 'INVALID_REQUEST'
22
+ raise Lieu::InvalidRequest, status
23
+ when 'REQUEST_DENIED'
24
+ raise Lieu::RequestDenied, status
25
+ when 'NOT_FOUND'
26
+ raise Lieu::NotFound, status
27
+ when 'UNKNOWN_ERROR'
28
+ raise Lieu::UnknownError, status
29
+ when 'ZERO_RESULTS'
30
+ raise Lieu::ZeroResults, status
31
+ when 'OVER_QUERY_LIMIT'
32
+ raise Lieu::OverQueryLimit, status
33
+ when 400
34
+ raise Lieu::InvalidRequest, status
35
+ when 403
36
+ raise Lieu::OverQueryLimit, status
37
+ end
38
+ end
39
+ end
40
+
41
+ # Raised when API returns INVALID_REQUEST or 400 HTTP code
42
+ class InvalidRequest < Error; end
43
+
44
+ # Raised when API returns REQUEST_DENIED
45
+ class RequestDenied < Error; end
46
+
47
+ # Raised when API returns NOT_FOUND
48
+ class NotFound < Error; end
49
+
50
+ # Raised when API returns UNKNOWN_ERROR
51
+ class UnknownError < Error; end
52
+
53
+ # Raised when API returns ZERO_RESULTS
54
+ class ZeroResults < Error; end
55
+
56
+ # Raised when API returns OVER_QUERY_LIMIT or 403 HTTP code
57
+ class OverQueryLimit < Error; end
58
+ end