Class: Wikimelon::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/wikimelon/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Request

Returns a new instance of Request.



16
17
18
19
20
21
22
23
24
# File 'lib/wikimelon/request.rb', line 16

def initialize(**args)
  @url = args[:url]
  @verbose = args[:verbose]
  @query = args[:query]
  @params = args[:params]
  @limit = args[:limit]
  @offset = args[:offset]
  @options = args[:options] # TODO: not added at wikimelon.rb
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



10
11
12
# File 'lib/wikimelon/request.rb', line 10

def endpoint
  @endpoint
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/wikimelon/request.rb', line 14

def options
  @options
end

#qObject

Returns the value of attribute q.



11
12
13
# File 'lib/wikimelon/request.rb', line 11

def q
  @q
end

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/wikimelon/request.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#performObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wikimelon/request.rb', line 26

def perform

  opts = @params || {'query': @query, 'format': 'json'}.delete_if { |_k, v| v.nil? }

  Faraday::Utils.default_space_encoding = "+"

  retry_max = Wikimelon.retry_max.to_i

  conn = Faraday.new(url: @url) do |f|
          f.response :logger if verbose
          if retry_max > 0
            f.request :retry,
                      max: retry_max,
                      interval: Wikimelon.retry_interval.to_f,
                      backoff_factor: 2,
                      retry_statuses: [429, 503]
          end
          f.use Faraday::WikimelonErrors::Middleware
          f.adapter Faraday.default_adapter
         end

  conn.headers['Accept'] = 'application/json,*/*'
  conn.headers[:user_agent] = make_user_agent
  conn.headers["X-USER-AGENT"] = make_user_agent

  Wikimelon::Throttle.wait!
  res = conn.get(endpoint, opts)

  JSON.parse(res.body)
end