Class: DesignHuddle::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subdomain: nil, client_id: nil, client_secret: nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
# File 'lib/design_huddle/client.rb', line 5

def initialize(subdomain:nil, client_id: nil, client_secret: nil)
  @subdomain = subdomain || ENV["DESIGN_HUDDLE_SUBDOMAIN"]
  @client_id = client_id || ENV["DESIGN_HUDDLE_CLIENT_ID"]
  @client_secret = client_secret || ENV["DESIGN_HUDDLE_CLIENT_SECRET"]
  request_access_token
  self
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



3
4
5
# File 'lib/design_huddle/client.rb', line 3

def access_token
  @access_token
end

#client_idObject

Returns the value of attribute client_id.



3
4
5
# File 'lib/design_huddle/client.rb', line 3

def client_id
  @client_id
end

#subdomainObject

Returns the value of attribute subdomain.



3
4
5
# File 'lib/design_huddle/client.rb', line 3

def subdomain
  @subdomain
end

Instance Method Details

#call(method: :get, path: nil, url: nil, payload: nil, headers: {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/design_huddle/client.rb', line 35

def call(method: :get, path: nil, url: nil, payload: nil, headers: {})
  response = RestClient::Request.execute(
    method: method,
    url: url || "https://#{subdomain}.designhuddle.com#{path}",
    payload: payload,
    headers: {
      Authorization: "Bearer #{access_token}",
      "Content-Type" => "application/json",
      "Accept" => "application/json"
    }.merge(headers)
  ) do |response, _request, _result|
    case response.code
    when 200..399
      response.body.empty? ? response : JSON.parse(response.body)
    else
      raise "Error: #{response.code} #{response.body}"
    end
  end
end

#request_access_tokenObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/design_huddle/client.rb', line 13

def request_access_token
  RestClient::Request.execute(
    method: :post,
    url: "https://#{subdomain}.designhuddle.com/oauth/token",
    payload: {
      grant_type: "client_credentials",
      client_id: client_id,
      client_secret: @client_secret
    },
    headers: {
      "Content-Type" => "application/x-www-form-urlencoded"
    }
  ) do |response, _request, _result|
    case response.code
    when 200
      self.access_token = JSON.parse(response.body)["access_token"]
    else
      raise "Error: #{response.code} #{response.body}"
    end
  end
end