Class: Tmdby::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/tmdby/wrapper.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clean_paramsObject

Returns the value of attribute clean_params.



4
5
6
# File 'lib/tmdby/wrapper.rb', line 4

def clean_params
  @clean_params
end

.paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/tmdby/wrapper.rb', line 4

def params
  @params
end

.post_paramsObject

Returns the value of attribute post_params.



4
5
6
# File 'lib/tmdby/wrapper.rb', line 4

def post_params
  @post_params
end

.rootObject

Returns the value of attribute root.



4
5
6
# File 'lib/tmdby/wrapper.rb', line 4

def root
  @root
end

Class Method Details

.add_default_languageObject

Add default language to parameters if it has been setted



26
27
28
29
30
31
32
# File 'lib/tmdby/wrapper.rb', line 26

def self.add_default_language
  default_language = Tmdby::Setup.default_language

  if default_language && !@params.key?('language') && !@params.key?(:language)
    @params['language'] = default_language
  end
end

.fetch(route = "", *args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tmdby/wrapper.rb', line 48

def self.fetch(route = "", *args)

  if args.length == 0
    options = {}
  elsif args.length == 1
    options = args[0]
  elsif args.length == 2
    options = args[1].merge(args[0])
  end

  method = options.fetch(:method, 'get')
  post_params = options.fetch(:post_params, [])
  authorized_params = options.fetch(:authorized_params, [])
  disable_unknown_params = options.fetch(:disable_unknown_params, false)
  [:method, :post_params, :authorized_params, :disable_unknown_params].each do |key|
    options.delete(key)
  end

  @params = options

  self.add_default_language if authorized_params.include?'language'
  self.verify_params authorized_params unless disable_unknown_params or authorized_params.include?"append_to_response"
  self.handle_post_params post_params

  Tmdby::Client.api_call method, self.route(route), @clean_params, @post_params
end

.handle_post_params(post_param_names) ⇒ Object

Dispatch params in 2 hashes : one for api url parameters, another on for api post parameters



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tmdby/wrapper.rb', line 35

def self.handle_post_params(post_param_names)
  @clean_params = {}
  @post_params  = {}

  @params.each do |k, v|
    if post_param_names.include?k or post_param_names.include?k.to_s
      @post_params[k.to_s] = v if v
    else
      @clean_params[k.to_s] = v if v
    end
  end
end

.route(route) ⇒ Object

Compute api route



8
9
10
11
12
13
14
15
16
# File 'lib/tmdby/wrapper.rb', line 8

def self.route(route)
  route = route.to_s if route.is_a? Integer

  if @root and not @root.empty? and not route.empty?
    "#{@root}/#{route}"
  else
    "#{@root}#{route}"
  end
end

.verify_params(authorized) ⇒ Object

Verify that every parameter is allowed by the api



19
20
21
22
23
# File 'lib/tmdby/wrapper.rb', line 19

def self.verify_params(authorized)
  @params.each do |key, _|
    raise RuntimeError, "Unknown argument : #{key}\nAuthorized arguments : #{authorized.join(', ')}" if not authorized.include? key.to_s
  end
end