Class: Grac::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/grac/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/grac/client.rb', line 14

def initialize(uri, options = {})
  URI.parse(uri)

  @uri = uri
  @options = {
    connecttimeout: options[:connecttimeout] || 0.1,
    timeout:        options[:timeout]        || 15,
    params:         options[:params]         || {},
    headers:        {
      'User-Agent'   => "Grac v#{Grac::VERSION}",
      'Content-Type' => 'application/json;charset=utf-8',
    }.merge(options[:headers] || {}),
    postprocessing: {},
    middleware:     options[:middleware] || [],
    retry_get_head: options.fetch(:retry_get_head, true),
    proxy:          options[:proxy],
    ssl:            options[:ssl],
  }

  if options[:postprocessing]
    options[:postprocessing]
      .each_with_object(postprocessing = {}) do |(pattern, transformation), obj|
      if pattern.is_a?(Regexp)
        obj[pattern] = transformation
      else
        obj[Regexp.new(pattern)] = transformation
      end
    end

    @options[:postprocessing] = postprocessing
  end

  @options.freeze

  [:params, :headers, :postprocessing, :middleware, :proxy, :ssl].each do |k|
    @options[k]&.freeze
  end

  @uri.freeze
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



12
13
14
# File 'lib/grac/client.rb', line 12

def uri
  @uri
end

Instance Method Details

#call(opts, request_uri, method, params, body) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/grac/client.rb', line 89

def call(opts, request_uri, method, params, body)
  request_hash = {
    method:         method,
    params:         params, # Query params are escaped by Typhoeus
    body:           body,
    connecttimeout: opts[:connecttimeout],
    timeout:        opts[:timeout],
    headers:        opts[:headers],
  }
  apply_proxy_options(request_hash, opts[:proxy])
  apply_ssl_options(request_hash, opts[:ssl])

  request  = ::Typhoeus::Request.new(request_uri, request_hash)
  response = request.run

  # Retry GET and HEAD requests - modifying requests might not be idempotent
  # Only retry those requests if the feature is enabled
  if response.timed_out? && ['get', 'head'].include?(method) && @options[:retry_get_head]
    response = request.run
  end

  # A request can time out while receiving data. In this case response.code might indicate
  # success although data hasn't been fully transferred. Thus rely on Typhoeus for
  # detecting a timeout.
  if response.timed_out?
    raise Exception::ServiceTimeout.new(method, request.url, response.return_message)
  end

  # List of possible codes
  # https://github.com/typhoeus/ethon/blob/ab052b6a317309b6ae8be7b538738b138b11437a/lib/ethon/curls/codes.rb#L10
  case response.return_code
  # A request may have received a response but may have aborted while receiving the data.
  # Typhoeus does not necessarily consider this a timeout but might try to parse a response.
  # If the response content length does not match the content length header,
  # the return code will be :partial_file.
  # In that case we do not want to pass it on as it would result in JSON Parser errors
  # due to invalid JSON from the incomplete response.
  when :partial_file
    raise Exception::PartialResponse.new(method, request.url, response.return_message)
  end

  return Response.new(response)
end

#path(path, variables = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/grac/client.rb', line 68

def path(path, variables = {})
  variables.each do |key, value|
    path = path.gsub("{#{key}}", escape_url_param(value))
  end
  self.class.new("#{@uri}#{path}", @options)
end

#set(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grac/client.rb', line 55

def set(options = {})
  options = options.merge(
              {
                headers:    @options[:headers].merge(options[:headers]    || {}),
                middleware: @options[:middleware] + (options[:middleware] || []),
                proxy:      merge_option_hash(options, :proxy),
                ssl:        merge_option_hash(options, :ssl),
              },
            )

  self.class.new(@uri, @options.merge(options))
end