Class: RubyPtvApi::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/ruby_ptv_api/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
Returns the value of attribute coord_format.
3
4
5
|
# File 'lib/ruby_ptv_api/base.rb', line 3
def coord_format
@coord_format
end
|
#profile ⇒ Object
Returns the value of attribute profile.
3
4
5
|
# File 'lib/ruby_ptv_api/base.rb', line 3
def profile
@profile
end
|
Instance Method Details
#connection ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/ruby_ptv_api/base.rb', line 58
def connection
Faraday.new(url: endpoint) do |faraday|
faraday.request :url_encoded faraday.adapter Faraday.default_adapter end
end
|
#default_params ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/ruby_ptv_api/base.rb', line 66
def default_params
{
callerContext: {
properties: [
{
key: 'Profile',
value: 'car'
},
{
key: 'CoordFormat',
value: coord_format || :PTV_GEODECIMAL
}
]
}
}
end
|
#demodulize(path) ⇒ Object
Avoid active support import
32
33
34
35
36
37
38
39
|
# File 'lib/ruby_ptv_api/base.rb', line 32
def demodulize(path)
path = path.to_s
if i = path.rindex('::')
path[(i+2)..-1]
else
path
end
end
|
#demodulized_name ⇒ Object
27
28
29
|
# File 'lib/ruby_ptv_api/base.rb', line 27
def demodulized_name
demodulize(self.class.name)
end
|
#endpoint ⇒ Object
5
6
7
|
# File 'lib/ruby_ptv_api/base.rb', line 5
def endpoint
RubyPtvApi.config.send("#{parent_module_name.downcase}_endpoint")
end
|
#parent_module_name ⇒ Object
21
22
23
24
25
|
# File 'lib/ruby_ptv_api/base.rb', line 21
def parent_module_name
name = self.class.name.split('::')[-2]
return nil unless name
underscore(name)
end
|
#post(params) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ruby_ptv_api/base.rb', line 45
def post(params)
params.merge!(default_params)
response = connection.post do |req|
req.url "#{ptv_path}/#{ptv_function}"
req.['Content-Type'] = 'application/json; charset=utf-8'
req.body = Oj.dump(params)
end
body = Oj.load(response.body)
(200..300).include?(response.status) ? body : raise(BadResponse.new(response.body))
end
|
#ptv_function ⇒ Object
17
18
19
|
# File 'lib/ruby_ptv_api/base.rb', line 17
def ptv_function
raise "Not implemented PTV function in #{demodulized_name}"
end
|
#ptv_path ⇒ Object
13
14
15
|
# File 'lib/ruby_ptv_api/base.rb', line 13
def ptv_path
raise "Not implemented PTV path in #{demodulized_name}"
end
|
#underscore(path) ⇒ Object
41
42
43
|
# File 'lib/ruby_ptv_api/base.rb', line 41
def underscore(path)
path.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2').downcase
end
|
#uri ⇒ Object
9
10
11
|
# File 'lib/ruby_ptv_api/base.rb', line 9
def uri
"#{endpoint}/#{ptv_path}/#{ptv_function}"
end
|