Class: Stone::Siclos::Client::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(token = Stone.config.siclos.api_key) ⇒ Base

Returns a new instance of Base.



40
41
42
# File 'lib/stone/siclos/client.rb', line 40

def initialize(token = Stone.config.siclos.api_key)
  @token = token
end

Instance Method Details

#parse_endpoint(path) ⇒ Object



44
45
46
47
48
49
# File 'lib/stone/siclos/client.rb', line 44

def parse_endpoint(path)
  path = path[1..-1] if path.starts_with?('/')
  endpoint = Stone.config.siclos.api_endpoint
  endpoint = "#{endpoint}/" unless endpoint.end_with?('/')
  URI.join(endpoint, path)
end

#remove_blank_values(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stone/siclos/client.rb', line 51

def remove_blank_values(data)
  object = data.respond_to?(:attributes) ? data.attributes : data
  object.each_with_object({}) do |(k, v), new_hash|
    unless v.blank? && v != false
      if v.respond_to?(:attributes)
        new_hash[k] = remove_blank_values(v.attributes)
        next
      end

      new_hash[k] = v.is_a?(Hash) ? remove_blank_values(v) : v
    end
  end
end

#request(method, path, data = nil, params = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/stone/siclos/client.rb', line 65

def request(method, path, data = nil, params = nil)
  response = Typhoeus::Request.new(
    parse_endpoint(path),
    method: method,
    body: data ? remove_blank_values(data).to_json : nil,
    params: params,
    headers: { 'Content-Type': 'application/json', Authorization: "Bearer #{@token}" }
  ).run
  Response.new(response.response_code, response.response_body)
end