Class: DesignHuddle::Client
- Inherits:
-
Object
- Object
- DesignHuddle::Client
- Defined in:
- lib/design_huddle/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#subdomain ⇒ Object
Returns the value of attribute subdomain.
Instance Method Summary collapse
- #call(method: :get, path: nil, url: nil, payload: nil, headers: {}) ⇒ Object
-
#initialize(subdomain: nil, client_id: nil, client_secret: nil) ⇒ Client
constructor
A new instance of Client.
- #request_access_token ⇒ Object
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_token ⇒ Object
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_id ⇒ Object
Returns the value of attribute client_id.
3 4 5 |
# File 'lib/design_huddle/client.rb', line 3 def client_id @client_id end |
#subdomain ⇒ Object
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_token ⇒ Object
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 |