Class: Crawlyflower::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
# File 'lib/crawlyflower/request.rb', line 10

def initialize(**args)
  @endpoint = args[:endpoint]
  @verbose = args[:verbose]
  @options = args[:options] || {}
  @accept = args[:accept] || 'application/json,*/*'
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/crawlyflower/request.rb', line 8

def endpoint
  @endpoint
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/crawlyflower/request.rb', line 8

def options
  @options
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/crawlyflower/request.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#performObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/crawlyflower/request.rb', line 17

def perform
  conn = Faraday.new(url: Crawlyflower.base_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |f|
    f.response :logger if @verbose
    f.use Faraday::CrawlyflowerErrors::Middleware
    f.response :follow_redirects
    f.adapter Faraday.default_adapter
  end

  conn.headers['Accept'] = @accept
  conn.headers[:user_agent] = make_user_agent
  conn.headers["X-USER-AGENT"] = make_user_agent

  res = conn.get endpoint, @options

  # WoRMS returns 204 for "nothing found"
  return nil if res.status == 204

  begin
    MultiJson.load(res.body)
  rescue MultiJson::ParseError
    res.body
  end
end