Class: Hurriyet::Service::Base
- Inherits:
-
Object
- Object
- Hurriyet::Service::Base
show all
- Defined in:
- lib/hurriyet/service/base.rb
Constant Summary
collapse
- ALLOWED_PARAMETERS =
%w(filter select top).map!(&:to_sym).freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client) ⇒ Base
Returns a new instance of Base.
10
11
12
13
|
# File 'lib/hurriyet/service/base.rb', line 10
def initialize(client)
@client = client
@conn = Faraday.new(url: 'https://api.hurriyet.com.tr', headers: { apikey: @client.apikey })
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
8
9
10
|
# File 'lib/hurriyet/service/base.rb', line 8
def client
@client
end
|
Instance Method Details
#allowed?(key) ⇒ Boolean
40
41
42
|
# File 'lib/hurriyet/service/base.rb', line 40
def allowed?(key)
ALLOWED_PARAMETERS.include? key
end
|
#execute(endpoint, options = {}) ⇒ Object
15
16
17
18
19
|
# File 'lib/hurriyet/service/base.rb', line 15
def execute(endpoint, options = {})
@options = options
@endpoint = endpoint
make_call
end
|
#make_call ⇒ Object
21
22
23
24
|
# File 'lib/hurriyet/service/base.rb', line 21
def make_call
resp = @conn.get(url)
JSON.parse(resp.body)
end
|
#param_string ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/hurriyet/service/base.rb', line 30
def param_string
string = ''
@options.each_with_index do |(key, value), index|
raise unless allowed?(key)
prefix = index == 0 ? '?' : '&'
string << "#{prefix}$#{key}=#{value}"
end
string
end
|
#url ⇒ Object
26
27
28
|
# File 'lib/hurriyet/service/base.rb', line 26
def url
"/#{version}/#{@endpoint}#{param_string}"
end
|
#version ⇒ Object
44
45
46
|
# File 'lib/hurriyet/service/base.rb', line 44
def version
'v1'
end
|