radar-io 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f57360aea1cc9099cfb5cb3a95c1b1f2dce649274bc6b2ae14e9910a133e1ad7
4
+ data.tar.gz: f4cf43dd8bea3007fb81e5974eb3f6296db7389ec6f87019296a20ca6660dd0f
5
+ SHA512:
6
+ metadata.gz: 35e3d5ce733dd8887685812edfc2229d772cafcd3f1a9a0158c456b6d7d02c7b987b9f4566e14997bec4bbc4070387820abc0b2f2b9e395cafb54263b8de87e2
7
+ data.tar.gz: e2a868b107058b843c92c3dbe1fb0cb7a80cda0eadf9ddf445d938045cea4a20ce0a8f0115e8775ccbbf6c579bc9e0aaf2b32c4c342c4e1fc07bb62e18e96f05
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at german.garcia@treasure-data.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in radar.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ radar-io (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ ast (2.4.2)
12
+ coderay (1.1.3)
13
+ crack (0.4.5)
14
+ rexml
15
+ diff-lcs (1.4.3)
16
+ hashdiff (1.0.1)
17
+ method_source (1.0.0)
18
+ parallel (1.20.1)
19
+ parser (3.0.2.0)
20
+ ast (~> 2.4.1)
21
+ pry (0.14.1)
22
+ coderay (~> 1.1)
23
+ method_source (~> 1.0)
24
+ public_suffix (4.0.6)
25
+ rainbow (3.0.0)
26
+ rake (12.3.3)
27
+ regexp_parser (2.1.1)
28
+ rexml (3.2.5)
29
+ rspec (3.9.0)
30
+ rspec-core (~> 3.9.0)
31
+ rspec-expectations (~> 3.9.0)
32
+ rspec-mocks (~> 3.9.0)
33
+ rspec-core (3.9.2)
34
+ rspec-support (~> 3.9.3)
35
+ rspec-expectations (3.9.2)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-mocks (3.9.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-support (3.9.3)
42
+ rubocop (1.19.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.0.0.0)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml
48
+ rubocop-ast (>= 1.9.1, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 3.0)
51
+ rubocop-ast (1.10.0)
52
+ parser (>= 3.0.1.1)
53
+ ruby-progressbar (1.11.0)
54
+ unicode-display_width (2.0.0)
55
+ vcr (6.0.0)
56
+ webmock (3.14.0)
57
+ addressable (>= 2.8.0)
58
+ crack (>= 0.3.2)
59
+ hashdiff (>= 0.4.0, < 2.0.0)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ bundler
66
+ pry
67
+ radar-io!
68
+ rake
69
+ rspec
70
+ rubocop
71
+ vcr
72
+ webmock
73
+
74
+ BUNDLED WITH
75
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 German Garcia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Radar
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/radar`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'radar'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install radar
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/radar. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/radar/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Radar project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/radar/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "radar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,137 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module Radar
6
+ module API
7
+ class Client
8
+ VERB_MAP = {
9
+ get: Net::HTTP::Get,
10
+ post: Net::HTTP::Post,
11
+ put: Net::HTTP::Put,
12
+ patch: Net::HTTP::Patch,
13
+ delete: Net::HTTP::Delete
14
+ }.freeze
15
+
16
+ def initialize
17
+ uri = URI.parse(base_uri)
18
+ @http = Net::HTTP.new(uri.host, uri.port)
19
+ @http.use_ssl = Radar.use_ssl
20
+ end
21
+
22
+ attr_reader :http
23
+
24
+ def get(path, params: {})
25
+ request :get, path, params
26
+ end
27
+
28
+ def post(path, params: {})
29
+ request :post, path, params
30
+ end
31
+
32
+ def put(path, params: {})
33
+ request :put, path, params
34
+ end
35
+
36
+ def patch(path, params: {})
37
+ request :patch, path, params
38
+ end
39
+
40
+ def delete(path)
41
+ request :delete, path
42
+ end
43
+
44
+ def parsed_response(response, object_class:)
45
+ response_hash = JSON.parse(response.body)
46
+ response_resource = response_hash[object_class::RESOURCE_NAME[:plural]]
47
+ if collection?(response_resource)
48
+ response_resource.map { |json_item| object_class.new(json_item) }
49
+ else
50
+ object_class.new(response_hash[object_class::RESOURCE_NAME[:singular]])
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def base_uri
57
+ @base_uri ||= Radar.use_ssl ? "https://#{Radar.api_host}/" : "http://#{Radar.api_host}/"
58
+ end
59
+
60
+ def collection?(response_body)
61
+ response_body && response_body.is_a?(Array)
62
+ end
63
+
64
+ def request(method, path, params = {})
65
+ case method
66
+ when :get
67
+ request = VERB_MAP[method].new(encode_path(path, params), headers)
68
+ else
69
+ request = VERB_MAP[method].new(encode_path(path), headers)
70
+ request.body = parameterize(params).to_json
71
+ end
72
+ handle_response(http.request(request))
73
+ end
74
+
75
+ def handle_response(response)
76
+ return response if %w[200 201 202 204].include?(response.code)
77
+ handle_error(response)
78
+ end
79
+
80
+ def handle_error(response)
81
+ error_params = JSON.parse(response.body, symbolize_names: true)[:meta]
82
+ case response.code
83
+ when '400'
84
+ raise Radar::API::BadRequestError.new(error_params)
85
+ # raise Radar::API::UnprocessableEntityError.new(error_params)
86
+ when '401'
87
+ raise Radar::API::UnauthorizedError.new(error_params)
88
+ when '402'
89
+ raise Radar::API::UsageError.new(error_params)
90
+ when '403'
91
+ raise Radar::API::ForbiddenError.new(error_params)
92
+ when '404'
93
+ raise Radar::API::NotFoundError.new(error_params)
94
+ when '409'
95
+ raise Radar::API::ConflictError.new(error_params)
96
+ when '422'
97
+ raise Radar::API::UnprocessableEntityError.new(error_params)
98
+ when '429'
99
+ raise Radar::API::TooManyRequestsError.new(error_params)
100
+ when '451'
101
+ raise Radar::API::UnavailableError.new(error_params)
102
+ else
103
+ raise Radar::API::Error.new(error_params)
104
+ end
105
+ end
106
+
107
+ def encode_path(path, params = nil)
108
+ encoded_path = URI.encode(path)
109
+ return path if params.nil?
110
+
111
+ encoded_params = URI.encode_www_form(params)
112
+ [encoded_path, encoded_params].join('?')
113
+ end
114
+
115
+ def headers
116
+ @headers ||= { 'Authorization' => "#{Radar.secret_token}", 'Content-Type' => 'application/json' }
117
+ end
118
+
119
+ def parameterize(object)
120
+ object.tap do |obj|
121
+ return object.map { |item| parameterize(item) } if object.is_a?(Array)
122
+
123
+ obj.keys.each do |key|
124
+ obj[key] = parameterize_object(obj[key]) unless (obj[key].is_a?(String) || obj[key].is_a?(Numeric) || obj[key].is_a?(Boolean))
125
+ obj[key.to_s.to_camel_case] = obj.delete(key)
126
+ end
127
+ end
128
+ end
129
+
130
+ def parameterize_object(object)
131
+ return parameterize(object.to_h) if Radar::API::Resource.descendants.include?(object.class)
132
+ return parameterize(object) if object.is_a?(Hash)
133
+ return object.map { |item| item.is_a?(String) ? item : parameterize_object(item) } if object.is_a?(Array)
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class BadRequestError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class ConflictError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ module Radar
2
+ module API
3
+ class Error < StandardError
4
+ attr_reader :code, :param, :message
5
+
6
+ def initialize(**error_params)
7
+ @code = error_params.dig(:code)
8
+ @param = error_params.dig(:param)
9
+ @message = error_params.dig(:message)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class ForbiddenError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class NotFoundError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class TooManyRequestsError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class UnauthorizedError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class UnavailableError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class UnprocessableEntityError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Radar
2
+ module API
3
+ class UsageError < Radar::API::Error; end
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module Radar
2
+ module API
3
+ class NestedResource < Resource
4
+ def self.resource_base_path(path_base: 'v1')
5
+ raise NotImplementedError, 'Radar::API::NestedResource is an abstract class' if self == NestedResource
6
+
7
+ path_base + self::PARENT_CLASS::RESOURCE_NAME[:plural]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,96 @@
1
+ module Radar
2
+ module API
3
+ class Resource
4
+ def initialize(data = {})
5
+ convert_to_obj(data)
6
+ end
7
+
8
+ def self.api_client
9
+ @api_client ||= Radar::API::Client.new
10
+ end
11
+
12
+ def self.class_name
13
+ name.split('::')[-1]
14
+ end
15
+
16
+ def self.resource_base_path(path_base: '/v1/')
17
+ raise NotImplementedError, 'Radar::API::Resource is an abstract class' if self == Resource
18
+
19
+ path_base + self::RESOURCE_NAME[:plural]
20
+ end
21
+
22
+ def self.descendants
23
+ ObjectSpace.each_object(Class).select do |klass|
24
+ klass < self && klass != Radar::API::NestedResource
25
+ end
26
+ end
27
+
28
+ def to_h
29
+ instance_variables.each_with_object({}) do |instance_variable, h|
30
+ key = instance_variable[1..instance_variable.length]
31
+ value = instance_variable_get(instance_variable)
32
+ h[key.to_sym] = build_hash_value_from_attribute_value(value)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def build_hash_value_from_attribute_value(attr_value)
39
+ return attr_value.to_h if known_resource_class?(attr_value.class)
40
+ return attr_value.map(&:to_h) if collection_of_known_resource_class?(attr_value)
41
+
42
+ attr_value
43
+ end
44
+
45
+ def collection_of_known_resource_class?(object)
46
+ object.is_a?(Array) && object.all? { |item| known_resource_class?(item.class) }
47
+ end
48
+
49
+ def known_resource_class?(object_class)
50
+ Radar.api_resources.value?(object_class)
51
+ end
52
+
53
+ def convert_to_obj(hash)
54
+ sanitized_hash(hash).each do |key, value|
55
+ object = build_object(key, value)
56
+ instance_variable_set("@#{key}", object)
57
+ self.class.send(:attr_accessor, key)
58
+ end
59
+ end
60
+
61
+ def build_object(key, value)
62
+ return build_resource_collection(key, value) if nested_resource_collection?(key, value)
63
+ return build_resource(key, value) if nested_resource?(key, value)
64
+
65
+ value
66
+ end
67
+
68
+ def build_resource_collection(key, value)
69
+ resource_collection = value.map { |resource_attributes| build_resource(key, resource_attributes) }
70
+ instance_variable_set("@#{key}", resource_collection)
71
+ end
72
+
73
+ def build_resource(key, resource_attributes)
74
+ resource_class(key).new(resource_attributes)
75
+ end
76
+
77
+ def nested_resource_collection?(key, value)
78
+ value.is_a?(Array) && value.all? { |obj| nested_resource?(key, obj) }
79
+ end
80
+
81
+ def nested_resource?(key, value)
82
+ Radar.api_resources.key?(key) && value.is_a?(Hash)
83
+ end
84
+
85
+ def resource_class(resource)
86
+ Radar.api_resources[resource]
87
+ end
88
+
89
+ def sanitized_hash(hash)
90
+ hash.tap do |h|
91
+ h.keys.each { |key| h[key.to_snake_case] = h.delete(key) }
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,17 @@
1
+ module Radar
2
+ class Event < API::Resource
3
+ RESOURCE_NAME = { singular: 'event', plural: 'events' }.freeze
4
+
5
+ def self.all(params: nil)
6
+ path = resource_base_path
7
+ response = api_client.get(path, params: params)
8
+ api_client.parsed_response(response, object_class: self)
9
+ end
10
+
11
+ def self.find(id:, params: nil)
12
+ path = [resource_base_path, id].join('/')
13
+ response = api_client.get(path)
14
+ api_client.parsed_response(response, object_class: self)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ module Radar
2
+ class Geofence < API::Resource
3
+ RESOURCE_NAME = { singular: 'geofence', plural: 'geofences' }.freeze
4
+
5
+ def self.all
6
+ path = resource_base_path
7
+ response = api_client.get(path, params: nil)
8
+ api_client.parsed_response(response, object_class: self)
9
+ end
10
+
11
+ def self.find(id:)
12
+ path = [resource_base_path, id].join('/')
13
+ response = api_client.get(path, params: nil)
14
+ api_client.parsed_response(response, object_class: self)
15
+ end
16
+
17
+ def self.upsert(tag:, external_id:, params:)
18
+ path = [resource_base_path, tag, external_id].join('/')
19
+ response = api_client.put(path, params: params)
20
+ api_client.parsed_response(response, object_class: self)
21
+ end
22
+
23
+ def self.delete(id:)
24
+ path = [resource_base_path, id].join('/')
25
+ api_client.delete(path)
26
+ end
27
+
28
+ def self.delete_by(tag:, external_id:)
29
+ path = [resource_base_path, tag, external_id].join('/')
30
+ api_client.delete(path)
31
+ end
32
+
33
+ def self.users(tag:, external_id:, params:)
34
+ path = [resource_base_path, tag, external_id, 'users'].join('/')
35
+ response = api_client.get(path, params: params)
36
+ api_client.parsed_response(response, object_class: self)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ module Radar
2
+ class Track < API::Resource
3
+ RESOURCE_NAME = { singular: 'user', plural: 'track' }.freeze
4
+
5
+ def self.create(params:)
6
+ path = resource_base_path
7
+ response = api_client.post(path, params: params)
8
+ api_client.parsed_response(response, object_class: self)
9
+ end
10
+ end
11
+ end
data/lib/radar/trip.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Radar
2
+ class Trip < API::Resource
3
+ RESOURCE_NAME = { singular: 'trip', plural: 'trips' }.freeze
4
+
5
+ def self.all(params: nil)
6
+ path = resource_base_path
7
+ response = api_client.get(path, params: params)
8
+ api_client.parsed_response(response, object_class: self)
9
+ end
10
+
11
+ def self.start(params:)
12
+ Radar::Track.create(params: params)
13
+ end
14
+
15
+ def self.restart(id:)
16
+ path = [resource_base_path, id].join('/')
17
+ response = api_client.patch(path, params: { status: 'started' })
18
+ api_client.parsed_response(response, object_class: self)
19
+ end
20
+
21
+ def self.stop(id:)
22
+ path = [resource_base_path, id].join('/')
23
+ response = api_client.patch(path, params: { status: 'canceled' })
24
+ api_client.parsed_response(response, object_class: self)
25
+ end
26
+ end
27
+ end
data/lib/radar/user.rb ADDED
@@ -0,0 +1,22 @@
1
+ module Radar
2
+ class User < API::Resource
3
+ RESOURCE_NAME = { singular: 'user', plural: 'users' }.freeze
4
+
5
+ def self.all(params: nil)
6
+ path = resource_base_path
7
+ response = api_client.get(path, params: params)
8
+ api_client.parsed_response(response, object_class: self)
9
+ end
10
+
11
+ def self.find(id:, params: nil)
12
+ path = [resource_base_path, id].join('/')
13
+ response = api_client.get(path)
14
+ api_client.parsed_response(response, object_class: self)
15
+ end
16
+
17
+ def self.delete(id:)
18
+ path = [resource_base_path, id].join('/')
19
+ api_client.delete(path)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module Boolean
2
+ end
3
+
4
+ class TrueClass
5
+ include Boolean
6
+ end
7
+
8
+ class FalseClass
9
+ include Boolean
10
+ end
@@ -0,0 +1,16 @@
1
+ class String
2
+ def to_snake_case
3
+ gsub(/::/, '/')
4
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
5
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
6
+ .tr('-', '_')
7
+ .downcase
8
+ end
9
+
10
+ def to_camel_case
11
+ split('_').each_with_object([]).with_index do |(string_part, string_parts), index|
12
+ string_part.capitalize! if index > 0
13
+ string_parts << string_part
14
+ end.join
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Radar
2
+ VERSION = '0.1.1'.freeze
3
+ end
data/lib/radar.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'radar/version'
2
+
3
+ # Utils
4
+ require 'radar/utils/string'
5
+ require 'radar/utils/boolean'
6
+
7
+ # Error handling
8
+ require 'radar/api/errors/error'
9
+ require 'radar/api/errors/bad_request_error'
10
+ require 'radar/api/errors/conflict_error'
11
+ require 'radar/api/errors/forbidden_error'
12
+ require 'radar/api/errors/not_found_error'
13
+ require 'radar/api/errors/too_many_requests_error'
14
+ require 'radar/api/errors/unauthorized_error'
15
+ require 'radar/api/errors/unavailable_error'
16
+ require 'radar/api/errors/unprocessable_entity_error'
17
+ require 'radar/api/errors/usage_error'
18
+
19
+ # Api Client
20
+ require 'radar/api/client'
21
+ require 'radar/api/resource'
22
+ require 'radar/api/nested_resource'
23
+
24
+ # Api Resources
25
+ require 'radar/track'
26
+ require 'radar/trip'
27
+ require 'radar/geofence'
28
+ require 'radar/user'
29
+ require 'radar/event'
30
+
31
+ module Radar
32
+ @api_host = 'api.radar.io'.freeze
33
+ @use_ssl = true
34
+ @secret_token = 'provide_your_test_secret_key'
35
+
36
+ class << self
37
+ attr_accessor :api_host, :use_ssl, :secret_token
38
+
39
+ def api_resources
40
+ @api_resources ||= API::Resource.descendants.each_with_object({}) do |descendant, resources|
41
+ resources[descendant::RESOURCE_NAME[:plural]] = descendant
42
+ end.freeze
43
+ end
44
+ end
45
+ end
data/radar.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ require_relative 'lib/radar/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "radar-io"
5
+ spec.version = Radar::VERSION
6
+ spec.authors = ["German Garcia"]
7
+ spec.email = ["german.garcia.d@gmail.com"]
8
+
9
+ spec.summary = "Radar.io Ruby API Client"
10
+ spec.description = "Light API client to communicate with Radar.io API"
11
+ spec.license = "MIT"
12
+ spec.homepage = "https://github.com/gealgaro/radar"
13
+
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/gealgaro/radar"
18
+ spec.metadata["changelog_uri"] = "https://github.com/gealgaro/radar"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency 'bundler'
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'rspec'
33
+ spec.add_development_dependency 'rubocop'
34
+ spec.add_development_dependency 'vcr'
35
+ spec.add_development_dependency 'webmock'
36
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radar-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - German Garcia
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Light API client to communicate with Radar.io API
112
+ email:
113
+ - german.garcia.d@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/radar.rb
130
+ - lib/radar/api/client.rb
131
+ - lib/radar/api/errors/bad_request_error.rb
132
+ - lib/radar/api/errors/conflict_error.rb
133
+ - lib/radar/api/errors/error.rb
134
+ - lib/radar/api/errors/forbidden_error.rb
135
+ - lib/radar/api/errors/not_found_error.rb
136
+ - lib/radar/api/errors/too_many_requests_error.rb
137
+ - lib/radar/api/errors/unauthorized_error.rb
138
+ - lib/radar/api/errors/unavailable_error.rb
139
+ - lib/radar/api/errors/unprocessable_entity_error.rb
140
+ - lib/radar/api/errors/usage_error.rb
141
+ - lib/radar/api/nested_resource.rb
142
+ - lib/radar/api/resource.rb
143
+ - lib/radar/event.rb
144
+ - lib/radar/geofence.rb
145
+ - lib/radar/track.rb
146
+ - lib/radar/trip.rb
147
+ - lib/radar/user.rb
148
+ - lib/radar/utils/boolean.rb
149
+ - lib/radar/utils/string.rb
150
+ - lib/radar/version.rb
151
+ - radar.gemspec
152
+ homepage: https://github.com/gealgaro/radar
153
+ licenses:
154
+ - MIT
155
+ metadata:
156
+ homepage_uri: https://github.com/gealgaro/radar
157
+ source_code_uri: https://github.com/gealgaro/radar
158
+ changelog_uri: https://github.com/gealgaro/radar
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: 2.3.0
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubygems_version: 3.0.3
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: Radar.io Ruby API Client
178
+ test_files: []