Class: Phenoscape::Request

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpt, args, verbose, options, ret, acchd = "application/json") ⇒ Request

Returns a new instance of Request.



23
24
25
26
27
28
29
30
# File 'lib/phenoscaperb/request.rb', line 23

def initialize(endpt, args, verbose, options, ret, acchd = "application/json")
  self.endpt = endpt
  self.args = args
  self.verbose = verbose
  self.options = options
  self.ret = ret
  self.acchd = acchd
end

Instance Attribute Details

#acchdObject

Returns the value of attribute acchd.



21
22
23
# File 'lib/phenoscaperb/request.rb', line 21

def acchd
  @acchd
end

#argsObject

Returns the value of attribute args.



17
18
19
# File 'lib/phenoscaperb/request.rb', line 17

def args
  @args
end

#endptObject

Returns the value of attribute endpt.



16
17
18
# File 'lib/phenoscaperb/request.rb', line 16

def endpt
  @endpt
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/phenoscaperb/request.rb', line 19

def options
  @options
end

#retObject

Returns the value of attribute ret.



20
21
22
# File 'lib/phenoscaperb/request.rb', line 20

def ret
  @ret
end

#verboseObject

Returns the value of attribute verbose.



18
19
20
# File 'lib/phenoscaperb/request.rb', line 18

def verbose
  @verbose
end

Instance Method Details

#performObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/phenoscaperb/request.rb', line 32

def perform
  if verbose
    conn = Faraday.new(:url => Phenoscape.base_url, :request => self.options || []) do |f|
      f.response :logger
      f.use FaradayMiddleware::RaiseHttpException
      f.options.params_encoder = Faraday::FlatParamsEncoder
      f.adapter  Faraday.default_adapter
    end
  else
    conn = Faraday.new(:url => Phenoscape.base_url, :request => self.options || []) do |f|
      f.use FaradayMiddleware::RaiseHttpException
      f.options.params_encoder = Faraday::FlatParamsEncoder
      f.adapter  Faraday.default_adapter
    end
  end

  conn.headers[:user_agent] = make_ua
  conn.headers["X-USER-AGENT"] = make_ua
  if !self.acchd.nil?
    conn.headers["Accept"] = self.acchd
  end

  res = conn.get self.endpt, self.args
  
  if !res.headers['content-type'].match(/json/).nil?
    out = MultiJson.load(res.body)
  else  
    case self.ret
    when "hash"
      out = Nokogiri::XML(res.body).to_hash
    when "text"
      out = res.body
    when "noko"
      out = Nokogiri::XML(res.body)
    end
  end

  return out

end