Class: Clearbit::Resource

Inherits:
Mash
  • Object
show all
Defined in:
lib/clearbit/resource.rb

Direct Known Subclasses

Base

Constant Summary collapse

OPTION_KEYS =
%i{
  params key headers stream
  proxy user password auth_type
  timeout ssl_options request
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mash

#custom_reader, #custom_writer, #deep_merge, #deep_update, #delete, #dup, #fetch, #id, #initialize, #initializing_reader, #key?, #method_missing, new, #regular_dup, #replace, #respond_to?, #shallow_merge, #shallow_update, #type, #underbang_reader

Constructor Details

This class inherits a constructor from Clearbit::Mash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Clearbit::Mash

Class Method Details

.delete(action = '', values = {}) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/clearbit/resource.rb', line 98

def self.delete(action = '', values = {})
  params, options = parse_values(values)

  request(
    uri(action),
    options.merge(method: :delete, params: params))
end

.endpoint(value = nil) ⇒ Object Also known as: endpoint=



5
6
7
8
9
# File 'lib/clearbit/resource.rb', line 5

def self.endpoint(value = nil)
  @endpoint = value if value
  return @endpoint if @endpoint
  superclass.respond_to?(:endpoint) ? superclass.endpoint : nil
end

.get(action = '', values = {}) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/clearbit/resource.rb', line 74

def self.get(action = '', values = {})
  params, options = parse_values(values)

  request(
    uri(action),
    options.merge(method: :get, params: params))
end

.options(value = nil) ⇒ Object Also known as: options=, add_options



17
18
19
20
21
22
23
24
25
26
# File 'lib/clearbit/resource.rb', line 17

def self.options(value = nil)
  @options ||= {}
  @options.merge!(value) if value

  if superclass <= Resource && superclass.respond_to?(:options)
    Nestful::Helpers.deep_merge(superclass.options, @options)
  else
    @options
  end
end

.parse_values(values) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/clearbit/resource.rb', line 56

def self.parse_values(values)
  params  = values.reject {|k,_| OPTION_KEYS.include?(k) }
  options = values.select {|k,_| OPTION_KEYS.include?(k) }

  if request_options = options.delete(:request)
    options.merge!(request_options)
  end

  if key = options.delete(:key)
    options.merge!(
      auth_type: :bearer,
      password:  key
    )
  end

  [params, options]
end

.path(value = nil) ⇒ Object Also known as: path=



11
12
13
14
15
# File 'lib/clearbit/resource.rb', line 11

def self.path(value = nil)
  @path = value if value
  return @path if @path
  superclass.respond_to?(:path) ? superclass.path : nil
end

.post(action = '', values = {}) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/clearbit/resource.rb', line 90

def self.post(action = '', values = {})
  params, options = parse_values(values)

  request(
    uri(action),
    options.merge(method: :post, params: params, format: :json))
end

.put(action = '', values = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/clearbit/resource.rb', line 82

def self.put(action = '', values = {})
  params, options = parse_values(values)

  request(
    uri(action),
    options.merge(method: :put, params: params, format: :json))
end

.request(uri, options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/clearbit/resource.rb', line 106

def self.request(uri, options = {})
  options = Nestful::Helpers.deep_merge(self.options, options)

  if options[:stream]
    uri.host = uri.host.gsub('.clearbit.com', '-stream.clearbit.com')
  end

  response = Nestful::Request.new(
    uri, options
  ).execute

  if notice = response.headers['X-API-Warn']
    Kernel.warn notice
  end

  response
end

.uri(*parts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/clearbit/resource.rb', line 39

def self.uri(*parts)
  # If an absolute URI already
  if (uri = parts.first) && uri.is_a?(URI)
    return uri if uri.host
  end

  value = Nestful::Helpers.to_path(url, *parts)

  URI.parse(value)
end

.url(options = {}) ⇒ Object



35
36
37
# File 'lib/clearbit/resource.rb', line 35

def self.url(options = {})
  URI.join(endpoint.to_s, path.to_s).to_s
end

Instance Method Details

#uri(*parts) ⇒ Object



124
125
126
# File 'lib/clearbit/resource.rb', line 124

def uri(*parts)
  self.class.uri(*[id, *parts].compact)
end