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.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/grac/client.rb', line 12

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}" }.merge(options[:headers] || {}),
    :postprocessing => options[:postprocessing] || {},
    :middleware     => options[:middleware]    || []
  }
  @options.freeze
  [:params, :headers, :postprocessing, :middleware].each do |k|
    @options[k].freeze
  end
  @uri.freeze
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



10
11
12
# File 'lib/grac/client.rb', line 10

def uri
  @uri
end

Instance Method Details

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



40
41
42
43
44
45
# File 'lib/grac/client.rb', line 40

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

#set(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/grac/client.rb', line 31

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

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