mapon_client 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: c3a0641f5b7c2ec708af18504b677b247d6cc41463f43535da3f2584c88845aa
4
- data.tar.gz: '0758faa99ebcdc7ca98410ba8b0153560f864ab093991d586771ecba0b924a46'
2
+ SHA1:
3
+ metadata.gz: 5ace0e60119c34fd750dfa62be9e2901bfde2958
4
+ data.tar.gz: 92f09406412f7f156ec9e889043a061afbed7d5b
5
5
  SHA512:
6
- metadata.gz: 2b075513dd784dcc08b94ff3c7fe92989011aaed1d63829fa789ec2a6b6c8f5b70ae6737134afab8e8095a3e5adf4ede4dab3d30ba24360f053cff9cc4574694
7
- data.tar.gz: 62ebddfdc128affcb48d16445edaf3b7e78d218b6387161cbe9ce4ecf862db706102de55142a8ce1c8cf25f5bf2a3053acbe6a9cbd35f6cc51c24d0ffd25e86c
6
+ metadata.gz: 7bef67e3ea0cbeac3368b04ed0f744ad0558c8abfc54a834cecd0c3d73d0fc2fa3b86f3afc488cb9bd8cf14c80d264f4b08001dc31a387bc9ca8d80462eb934e
7
+ data.tar.gz: d7c128bd0238508c851d06df2ebb4510935a7d14f1cc2f98c43df82213ff14028ba852d8e8f4d7fb45c784fee256308b84bffd1323fb5368d4aa6b1edc0d7e9f
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /tmp/
9
9
 
10
10
  .env
11
+ *.gem
11
12
 
12
13
  # rspec failure tracking
13
14
  .rspec_status
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mapon_client (0.1.0)
5
- rest-client (> 1)
4
+ mapon_client (0.2.0)
5
+ rest-client (~> 2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -31,20 +31,30 @@ Or install it yourself as:
31
31
  ## Usage
32
32
 
33
33
  ### Instantiate client:
34
- * api_key - create it under Settings/API keys, if blank, will try to read environment variable MAPON_CLIENT_API_KEY on the [site](https://mapon.com/new/settings#9)
35
- * base_url - defaults to https://mapon.com/api/v1/
36
- * format - json/xml, defaults to json
34
+ * *api_key* - create it under Settings/API keys, if left blank, will try to utilize environment variable *MAPON_CLIENT_API_KEY*
35
+ * *base_url* - defaults to https://mapon.com/api/v1/
36
+ * *format* - json/xml/structure/object, defaults to json
37
+ - json - raw json string from response,
38
+ - xml - raw xml string from response,
39
+ - structure - ruby Array and Hash structure (HTTP request gets made with format json),
40
+ - object - ruby OpenStruct objects (HTTP request gets made with format json)
37
41
 
38
42
  ```ruby
39
43
  mapon_client = MaponClient::Client.new
40
44
  # OR
41
45
  mapon_client = MaponClient::Client.new(
42
- api_key: ENV['BY_DIFFERENT_NAME_API_KEY'],
43
- base_url: 'https://mapon.com/api/v300/not-the-default-endpoint',
46
+ api_key: 'some-api-key-goes-here',
47
+ base_url: 'https://mapon.com/api/v2/',
44
48
  format: 'xml'
45
49
  )
46
50
  ```
47
51
 
52
+ ### Structure and Object formats
53
+
54
+ For format structure and object all requests are still made using json, but response gets parsed.
55
+ In case of structure it is `JSON.parse(response)`.
56
+ In case of object it is `JSON.parse(response, object_class: OpenStruct)`.
57
+
48
58
  ### Request resources
49
59
 
50
60
  Mapon API documentation can be found [here](https://mapon.com/api).
@@ -57,8 +67,8 @@ Look there to find out required params in case error messages are not clear enou
57
67
 
58
68
  #### Unit list
59
69
  ```ruby
60
- @mapon_client.resources[:unit].list
61
- @mapon_client.resources[:unit].list(
70
+ @mapon_client.unit.list
71
+ @mapon_client.unit.list(
62
72
  unit_id: [80669', 85113], car_number: ['VY92278', 'DK56625'], empty_box_id: true,
63
73
  include: [
64
74
  'in_objects', 'io_din', 'fuel', 'can', 'reefer', 'drivers', 'temperature', 'ambienttemp', 'device', 'supply_voltage'
@@ -68,122 +78,122 @@ Look there to find out required params in case error messages are not clear enou
68
78
 
69
79
  #### Unit group
70
80
  ```ruby
71
- @mapon_client.resources[:unit_group].list(unit_id: [80669, 85113])
81
+ @mapon_client.unit_group.list(unit_id: [80669, 85113])
72
82
 
73
- @mapon_client.resources[:unit_group].list_units(id: 11677)
83
+ @mapon_client.unit_group.list_units(id: 11677)
74
84
 
75
85
  ```
76
86
 
77
87
  #### Unit data
78
88
  ```ruby
79
- @mapon_client.resources[:unit_data].ignitions(
89
+ @mapon_client.unit_data.ignitions(
80
90
  from: '2018-02-01T00:00:00Z', till: '2018-02-10T00:00:00Z', unit_id: [80669, 85113]
81
91
  )
82
92
 
83
- @mapon_client.resources[:unit_data].temperature(
93
+ @mapon_client.unit_data.temperature(
84
94
  from: '2018-02-01T00:00:00Z', till: '2018-02-20T00:00:00Z', unit_id: 80669
85
95
  )
86
96
 
87
- @mapon_client.resources[:unit_data].digital_inputs(
97
+ @mapon_client.unit_data.digital_inputs(
88
98
  from: '2018-02-01T00:00:00Z', till: '2018-02-20T00:00:00Z', unit_id: 80669
89
99
  )
90
100
 
91
- @mapon_client.resources[:unit_data].can_period(
101
+ @mapon_client.unit_data.can_period(
92
102
  datetime: '2018-02-01T00:00:00Z', unit_id: 80669,
93
103
  include: [
94
104
  'rpm_average', 'rpm_max', 'fuel_level', 'service_distance', 'total_distance', 'total_fuel', 'total_engine_hours', 'ambient_temperature'
95
105
  ]
96
106
  )
97
107
 
98
- @mapon_client.resources[:unit_data].can_point(
108
+ @mapon_client.unit_data.can_point(
99
109
  from: '2018-02-01T00:00:00Z', till: '2018-02-20T00:00:00Z', unit_id: 80669,
100
110
  include: [
101
111
  'rpm_average', 'rpm_max', 'fuel_level', 'service_distance', 'total_distance', 'total_fuel', 'total_engine_hours', 'ambient_temperature'
102
112
  ]
103
113
  )
104
114
 
105
- @mapon_client.resources[:unit_data].fields(unit_id: 85188)
115
+ @mapon_client.unit_data.fields(unit_id: 85188)
106
116
  ```
107
117
 
108
118
  #### Reefer
109
119
  ```ruby
110
- @mapon_client.resources[:reefer].alert_list(id: 12345, unit_id: [80669, 85113])
120
+ @mapon_client.reefer.alert_list(id: 12345, unit_id: [80669, 85113])
111
121
 
112
- @mapon_client.resources[:reefer].runmodes(unit_id: [80669, 85113])
122
+ @mapon_client.reefer.runmodes(unit_id: [80669, 85113])
113
123
 
114
- @mapon_client.resources[:reefer].list_temperature_data(
124
+ @mapon_client.reefer.list_temperature_data(
115
125
  unit_id: 85113, from: '2018-01-20T00:00:00Z', till: '2018-03-03T00:00:00Z'
116
126
  )
117
127
  ```
118
128
 
119
129
  #### Route
120
130
  ```ruby
121
- @mapon_client.resources[:route].list(
131
+ @mapon_client.route.list(
122
132
  from: '2018-02-20T00:00:00Z', till: '2018-03-06T00:00:00Z', unit_id: [80669, 85113], empty_box_id: true,
123
133
  include: ['polyline', 'speed', 'decoded_route', 'driver_id']
124
134
  )
125
135
 
126
- @mapon_client.resources[:route].custom_fields(route_id: 639655550)
136
+ @mapon_client.route.custom_fields(route_id: 639655550)
127
137
  ```
128
138
 
129
- For decoding polyline use https://github.com/joshuaclayton/polylines
139
+ *For decoding polyline use https://github.com/joshuaclayton/polylines*
130
140
 
131
141
  #### Fuel
132
142
  ```ruby
133
- @mapon_client.resources[:fuel].summary(
143
+ @mapon_client.fuel.summary(
134
144
  from: '2018-02-20T00:00:00Z', till: '2018-03-06T00:00:00Z', unit_id: [86303, 80669]
135
145
  )
136
146
 
137
- @mapon_client.resources[:fuel].changes(
147
+ @mapon_client.fuel.changes(
138
148
  from: '2018-02-20T00:00:00Z', till: '2018-03-06T00:00:00Z', unit_id: [86303, 80669]
139
149
  )
140
150
  ```
141
151
 
142
152
  #### Object
143
153
  ```ruby
144
- @mapon_client.resources[:object].list(
154
+ @mapon_client.object.list(
145
155
  id: 345901, name: 'Depot', group_id: 0, deleted: 1, updated_from: '2017-12-22T09:50:23Z', updated_till: '2017-12-22T09:55:25Z'
146
156
  )
147
157
 
148
- @mapon_client.resources[:object].list_groups(id: 1234, name: 'Group name')
158
+ @mapon_client.object.list_groups(id: 1234, name: 'Group name')
149
159
  ```
150
160
 
151
161
  #### User
152
162
  ```ruby
153
- @mapon_client.resources[:user].list(id: '116361', type: 'user_all')
163
+ @mapon_client.user.list(id: '116361', type: 'user_all')
154
164
  ```
155
165
 
156
166
  #### Driver
157
167
  ```ruby
158
- @mapon_client.resources[:driver].list(id: 116551)
168
+ @mapon_client.driver.list(id: 116551)
159
169
  ```
160
170
 
161
171
  #### Tachograph
162
172
  ```ruby
163
- @mapon_client.resources[:tachograph].list_ddd_driver(from: '2018-02-20T00:00:00Z', till: '2018-03-03T00:00:00Z')
173
+ @mapon_client.tachograph.list_ddd_driver(from: '2018-02-20T00:00:00Z', till: '2018-03-03T00:00:00Z')
164
174
 
165
- @mapon_client.resources[:tachograph].download_ddd_driver(id: 55)
175
+ @mapon_client.tachograph.download_ddd_driver(id: 55)
166
176
 
167
- @mapon_client.resources[:tachograph].list_ddd_vehicle(from: '2018-02-20T00:00:00Z', till: '2018-03-03T00:00:00Z')
177
+ @mapon_client.tachograph.list_ddd_vehicle(from: '2018-02-20T00:00:00Z', till: '2018-03-03T00:00:00Z')
168
178
 
169
- @mapon_client.resources[:tachograph].download_ddd_vehicle(id: 55)
179
+ @mapon_client.tachograph.download_ddd_vehicle(id: 55)
170
180
  ```
171
181
 
172
182
  #### Tracking
173
183
  ```ruby
174
- @mapon_client.resources[:tracking].list(from: '2018-02-20T00:00:00Z', till: '2018-03-03T00:00:00Z')
184
+ @mapon_client.tracking.list(from: '2018-02-20T00:00:00Z', till: '2018-03-03T00:00:00Z')
175
185
  ```
176
186
 
177
187
  #### Data forward
178
188
  ```ruby
179
- @mapon_client.resources[:data_forward].list
189
+ @mapon_client.data_forward.list
180
190
 
181
- @mapon_client.resources[:data_forward].list_packs
191
+ @mapon_client.data_forward.list_packs
182
192
  ```
183
193
 
184
194
  #### Application menu
185
195
  ```ruby
186
- @mapon_client.resources[:application_menu].list
196
+ @mapon_client.application_menu.list
187
197
  ```
188
198
 
189
199
  ### Rest client
@@ -3,20 +3,24 @@ require 'mapon_client/resources'
3
3
 
4
4
  module MaponClient
5
5
  class Client
6
- attr_reader :api_key, :base_url, :resource_base, :format
6
+ DEFAULT_BASE_URL = 'https://mapon.com/api/v1/'.freeze
7
7
 
8
- def initialize(api_key: nil, base_url: nil, format: nil)
9
- @api_key = (
10
- api_key || ENV['MAPON_CLIENT_API_KEY']
11
- )
8
+ JSON = 'json'.freeze
9
+ XML = 'xml'.freeze
10
+ STRUCTURE = 'structure'.freeze
11
+ OBJECT = 'object'.freeze
12
12
 
13
- @base_url = (
14
- base_url || MaponClient::DEFAULT_BASE_URL
15
- )
13
+ AVAILABLE_FORMATS = [JSON, XML, STRUCTURE, OBJECT].freeze
16
14
 
17
- @format = (
18
- format || MaponClient::DEFAULT_FORMAT
19
- )
15
+ attr_reader :api_key, :base_url, :resource_base, :format, :request_format, :parser_class
16
+
17
+ def initialize(api_key: nil, base_url: nil, format: nil)
18
+ @api_key = api_key || ENV['MAPON_CLIENT_API_KEY']
19
+ @base_url = base_url || DEFAULT_BASE_URL
20
+ @format = format || JSON
21
+
22
+ set_request_format
23
+ set_parser_class
20
24
 
21
25
  @resource_base ||= RestClient::Resource.new(
22
26
  @base_url, headers: { params: { key: @api_key } }
@@ -30,8 +34,12 @@ module MaponClient
30
34
  )
31
35
  end
32
36
 
33
- def resources
34
- @resources ||= Resources.new(self)
37
+ Resources.descendants.each do |resource_class|
38
+ class_eval %Q{
39
+ def #{resource_class.resource_name}
40
+ @#{resource_class.resource_name} ||= #{resource_class}.new(self)
41
+ end
42
+ }
35
43
  end
36
44
 
37
45
  def with_key(params)
@@ -39,8 +47,28 @@ module MaponClient
39
47
  end
40
48
 
41
49
  def append_format_to_url(*args)
42
- suburl = "#{args.shift}.#{@format}"
50
+ suburl = "#{args.shift}.#{@request_format}"
43
51
  args.unshift(suburl)
44
52
  end
53
+
54
+ private
55
+
56
+ def set_request_format
57
+ if @format == STRUCTURE || @format == OBJECT
58
+ @request_format = JSON
59
+ else
60
+ @request_format = @format
61
+ end
62
+ end
63
+
64
+ def set_parser_class
65
+ @parser_class = if @format == STRUCTURE
66
+ MaponClient::Parsers::StructureParser
67
+ elsif @format == OBJECT
68
+ MaponClient::Parsers::ObjectParser
69
+ else
70
+ MaponClient::Parsers::EmptyParser
71
+ end
72
+ end
45
73
  end
46
74
  end
@@ -0,0 +1,13 @@
1
+ module MaponClient
2
+ module Helpers
3
+ def resource_name
4
+ self.name.sub(/.*::/, '').tap do |name|
5
+ name.sub!(/Resource/, '')
6
+ name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
7
+ name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
8
+ name.tr!("-", "_")
9
+ name.downcase!
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module MaponClient
2
+ module Parsers
3
+ class EmptyParser
4
+ def initialize(data)
5
+ @data = data
6
+ end
7
+
8
+ def parse
9
+ @data
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ require 'json'
2
+ require 'ostruct'
3
+
4
+ module MaponClient
5
+ module Parsers
6
+ class ObjectParser < EmptyParser
7
+ def parse
8
+ JSON.parse(@data, object_class: OpenStruct)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'json'
2
+
3
+ module MaponClient
4
+ module Parsers
5
+ class StructureParser < EmptyParser
6
+ def parse
7
+ JSON.parse(@data)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module MaponClient
2
+ module Parsers
3
+ autoload :EmptyParser, 'mapon_client/parsers/empty_parser'
4
+ autoload :StructureParser, 'mapon_client/parsers/structure_parser'
5
+ autoload :ObjectParser, 'mapon_client/parsers/object_parser'
6
+ end
7
+ end
@@ -1,11 +1,7 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class ApplicationMenuResource < Resources
4
- def list(params = {})
5
- @client['application_menu/list'].get(
6
- params: with_key(params)
7
- )
8
- end
4
+ generate_action name: 'list'
9
5
  end
10
6
  end
11
7
  end
@@ -1,9 +1,7 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class CompanyResource < Resources
4
- def get
5
- @client['company/get'].get
6
- end
4
+ generate_action name: 'get'
7
5
  end
8
6
  end
9
7
  end
@@ -1,17 +1,8 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class DataForwardResource < Resources
4
- def list(params = {})
5
- @client['data_forward/list'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def list_packs(params = {})
11
- @client['data_forward/list_packs'].get(
12
- params: with_key(params)
13
- )
14
- end
4
+ generate_action name: 'list'
5
+ generate_action name: 'list_packs'
15
6
  end
16
7
  end
17
8
  end
@@ -1,11 +1,7 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class DriverResource < Resources
4
- def list(params = {})
5
- @client['driver/list'].get(
6
- params: with_key(params)
7
- )
8
- end
4
+ generate_action name: 'list'
9
5
  end
10
6
  end
11
7
  end
@@ -1,17 +1,8 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class FuelResource < Resources
4
- def summary(params = {})
5
- @client['fuel/summary'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def changes(params = {})
11
- @client['fuel/changes'].get(
12
- params: with_key(params)
13
- )
14
- end
4
+ generate_action name: 'summary'
5
+ generate_action name: 'changes'
15
6
  end
16
7
  end
17
8
  end
@@ -1,17 +1,8 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class ObjectResource < Resources
4
- def list(params = {})
5
- @client['object/list'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def list_groups(params = {})
11
- @client['object/list_groups'].get(
12
- params: with_key(params)
13
- )
14
- end
4
+ generate_action name: 'list'
5
+ generate_action name: 'list_groups'
15
6
  end
16
7
  end
17
8
  end
@@ -1,23 +1,9 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class ReeferResource < Resources
4
- def alert_list(params = {})
5
- @client['reefer/alert_list'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def runmodes(params = {})
11
- @client['reefer/runmodes'].get(
12
- params: with_key(params)
13
- )
14
- end
15
-
16
- def list_temperature_data(params = {})
17
- @client['reefer/list_temperature_data'].get(
18
- params: with_key(params)
19
- )
20
- end
4
+ generate_action name: 'alert_list'
5
+ generate_action name: 'runmodes'
6
+ generate_action name: 'list_temperature_data'
21
7
  end
22
8
  end
23
9
  end
@@ -1,17 +1,8 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class RouteResource < Resources
4
- def list(params = {})
5
- @client['route/list'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def custom_fields(params = {})
11
- @client['route/custom_fields'].get(
12
- params: with_key(params)
13
- )
14
- end
4
+ generate_action name: 'list'
5
+ generate_action name: 'custom_fields'
15
6
  end
16
7
  end
17
8
  end
@@ -1,29 +1,10 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class TachographResource < Resources
4
- def list_ddd_driver(params = {})
5
- @client['tachograph/list_ddd_driver'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def download_ddd_driver(params = {})
11
- @client['tachograph/download_ddd_driver'].get(
12
- params: with_key(params)
13
- )
14
- end
15
-
16
- def list_ddd_vehicle(params = {})
17
- @client['tachograph/list_ddd_vehicle'].get(
18
- params: with_key(params)
19
- )
20
- end
21
-
22
- def download_ddd_vehicle(params = {})
23
- @client['tachograph/download_ddd_vehicle'].get(
24
- params: with_key(params)
25
- )
26
- end
4
+ generate_action name: 'list_ddd_driver'
5
+ generate_action name: 'download_ddd_driver'
6
+ generate_action name: 'list_ddd_vehicle'
7
+ generate_action name: 'download_ddd_vehicle'
27
8
  end
28
9
  end
29
10
  end
@@ -1,11 +1,7 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class TrackingResource < Resources
4
- def list(params = {})
5
- @client['tracking/list'].get(
6
- params: with_key(params)
7
- )
8
- end
4
+ generate_action name: 'list'
9
5
  end
10
6
  end
11
7
  end
@@ -1,47 +1,13 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class UnitDataResource < Resources
4
- def ignitions(params = {})
5
- @client['unit_data/ignitions'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def temperature(params = {})
11
- @client['unit_data/temperature'].get(
12
- params: with_key(params)
13
- )
14
- end
15
-
16
- def digital_inputs(params = {})
17
- @client['unit_data/digital_inputs'].get(
18
- params: with_key(params)
19
- )
20
- end
21
-
22
- def can_period(params = {})
23
- @client['unit_data/can_period'].get(
24
- params: with_key(params)
25
- )
26
- end
27
-
28
- def can_point(params = {})
29
- @client['unit_data/can_point'].get(
30
- params: with_key(params)
31
- )
32
- end
33
-
34
- def fields(params = {})
35
- @client['unit_data/fields'].get(
36
- params: with_key(params)
37
- )
38
- end
39
-
40
- def debug_info(params = {})
41
- @client['unit_data/debug_info'].get(
42
- params: with_key(params)
43
- )
44
- end
4
+ generate_action name: 'ignitions'
5
+ generate_action name: 'temperature'
6
+ generate_action name: 'digital_inputs'
7
+ generate_action name: 'can_period'
8
+ generate_action name: 'can_point'
9
+ generate_action name: 'fields'
10
+ generate_action name: 'debug_info'
45
11
  end
46
12
  end
47
13
  end
@@ -1,17 +1,8 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class UnitGroupResource < Resources
4
- def list(params = {})
5
- @client['unit_groups/list'].get(
6
- params: with_key(params)
7
- )
8
- end
9
-
10
- def list_units(params = {})
11
- @client['unit_groups/list_units'].get(
12
- params: with_key(params)
13
- )
14
- end
4
+ generate_action name: 'list'
5
+ generate_action name: 'list_units'
15
6
  end
16
7
  end
17
8
  end
@@ -1,11 +1,7 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class UnitResource < Resources
4
- def list(params = {})
5
- @client['unit/list'].get(
6
- params: with_key(params)
7
- )
8
- end
4
+ generate_action name: 'list'
9
5
  end
10
6
  end
11
7
  end
@@ -1,11 +1,7 @@
1
1
  module MaponClient
2
2
  class Resources
3
3
  class UserResource < Resources
4
- def list(params = {})
5
- @client['user/list'].get(
6
- params: with_key(params)
7
- )
8
- end
4
+ generate_action name: 'list'
9
5
  end
10
6
  end
11
7
  end
@@ -1,53 +1,53 @@
1
- require 'mapon_client/resources/company_resource'
2
- require 'mapon_client/resources/unit_resource'
3
- require 'mapon_client/resources/unit_group_resource'
4
- require 'mapon_client/resources/unit_data_resource'
5
- require 'mapon_client/resources/route_resource'
6
- require 'mapon_client/resources/fuel_resource'
7
- require 'mapon_client/resources/object_resource'
8
- require 'mapon_client/resources/user_resource'
9
- require 'mapon_client/resources/driver_resource'
10
- require 'mapon_client/resources/reefer_resource'
11
- require 'mapon_client/resources/tachograph_resource'
12
- require 'mapon_client/resources/tracking_resource'
13
- require 'mapon_client/resources/data_forward_resource'
14
- require 'mapon_client/resources/application_menu_resource'
1
+ require 'mapon_client/helpers'
15
2
 
16
3
  module MaponClient
17
4
  class Resources
18
- AVAILABLE_RESOURCES = {
19
- company: CompanyResource,
20
- unit_group: UnitGroupResource,
21
- unit: UnitResource,
22
- unit_data: UnitDataResource,
23
- route: RouteResource,
24
- fuel: FuelResource,
25
- object: ObjectResource,
26
- user: UserResource,
27
- driver: DriverResource,
28
- reefer: ReeferResource,
29
- tachograph: TachographResource,
30
- tracking: TrackingResource,
31
- data_forward: DataForwardResource,
32
- application_menu: ApplicationMenuResource,
33
- }.freeze
5
+ autoload :CompanyResource, 'mapon_client/resources/company_resource'
6
+ autoload :UnitResource, 'mapon_client/resources/unit_resource'
7
+ autoload :UnitGroupResource, 'mapon_client/resources/unit_group_resource'
8
+ autoload :UnitDataResource, 'mapon_client/resources/unit_data_resource'
9
+ autoload :RouteResource, 'mapon_client/resources/route_resource'
10
+ autoload :FuelResource, 'mapon_client/resources/fuel_resource'
11
+ autoload :ObjectResource, 'mapon_client/resources/object_resource'
12
+ autoload :UserResource, 'mapon_client/resources/user_resource'
13
+ autoload :DriverResource, 'mapon_client/resources/driver_resource'
14
+ autoload :ReeferResource, 'mapon_client/resources/reefer_resource'
15
+ autoload :TachographResource, 'mapon_client/resources/tachograph_resource'
16
+ autoload :TrackingResource, 'mapon_client/resources/tracking_resource'
17
+ autoload :DataForwardResource, 'mapon_client/resources/data_forward_resource'
18
+ autoload :ApplicationMenuResource, 'mapon_client/resources/application_menu_resource'
34
19
 
35
- def initialize(client)
36
- @client = client
37
- @resources_repository = {}
20
+ extend MaponClient::Helpers
21
+
22
+ def self.descendants
23
+ @descendants ||= begin
24
+ classes = constants.map { |const_name| const_get(const_name) }
25
+ classes.select { |klass| klass.name.end_with?('Resource') }
26
+ end
27
+ end
28
+
29
+ def self.generate_action(name:, method: 'get')
30
+ class_eval %{
31
+ def #{name}(params = {})
32
+ raw_response = @client['#{resource_name}/#{name}'].#{method}(
33
+ params: with_key(params)
34
+ ).body
35
+
36
+ parse_repsonse(raw_response)
37
+ end
38
+ }
38
39
  end
39
40
 
40
- def [](resource)
41
- return @resources_repository[resource] if @resources_repository[resource]
42
- raise ResourceNotFound.new(resource) unless AVAILABLE_RESOURCES[resource]
43
- @resources_repository[resource] = AVAILABLE_RESOURCES[resource].new(@client)
41
+ def initialize(client)
42
+ @client = client
44
43
  end
45
44
 
46
45
  def with_key(params)
47
46
  @client.with_key(params)
48
47
  end
49
- end
50
48
 
51
- class ResourceNotFound < ::StandardError
49
+ def parse_repsonse(response)
50
+ @client.parser_class.new(response).parse
51
+ end
52
52
  end
53
53
  end
@@ -1,3 +1,3 @@
1
1
  module MaponClient
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/mapon_client.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require "mapon_client/version"
2
2
 
3
3
  module MaponClient
4
- DEFAULT_BASE_URL = 'https://mapon.com/api/v1/'.freeze
5
- DEFAULT_FORMAT = 'json'.freeze
6
-
7
4
  autoload :Client, 'mapon_client/client'
5
+ autoload :Parsers, 'mapon_client/parsers'
8
6
  end
data/mapon_client.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_dependency "rest-client", ">1"
23
+ spec.add_dependency "rest-client", "~> 2"
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.16"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mapon_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ingus Skaistkalns
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-06 00:00:00.000000000 Z
11
+ date: 2018-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1'
19
+ version: '2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">"
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1'
26
+ version: '2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -86,6 +86,11 @@ files:
86
86
  - bin/setup
87
87
  - lib/mapon_client.rb
88
88
  - lib/mapon_client/client.rb
89
+ - lib/mapon_client/helpers.rb
90
+ - lib/mapon_client/parsers.rb
91
+ - lib/mapon_client/parsers/empty_parser.rb
92
+ - lib/mapon_client/parsers/object_parser.rb
93
+ - lib/mapon_client/parsers/structure_parser.rb
89
94
  - lib/mapon_client/resources.rb
90
95
  - lib/mapon_client/resources/application_menu_resource.rb
91
96
  - lib/mapon_client/resources/company_resource.rb
@@ -123,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
128
  version: '0'
124
129
  requirements: []
125
130
  rubyforge_project:
126
- rubygems_version: 2.7.3
131
+ rubygems_version: 2.6.13
127
132
  signing_key:
128
133
  specification_version: 4
129
134
  summary: Ruby client library for https://www.mapon.com API