Class: Hurriyet::Service::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hurriyet/service/base.rb

Direct Known Subclasses

Article, Column, NewsPhotoGallery, Page, Path, Writer

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

#clientObject

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

Returns:

  • (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_callObject



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_stringObject



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

#urlObject



26
27
28
# File 'lib/hurriyet/service/base.rb', line 26

def url
  "/#{version}/#{@endpoint}#{param_string}"
end

#versionObject



44
45
46
# File 'lib/hurriyet/service/base.rb', line 44

def version
  'v1'
end