Class: Nest::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/nest/real.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Real

Returns a new instance of Real.



4
5
6
7
8
9
10
11
# File 'lib/nest/real.rb', line 4

def initialize(attributes={})
  @url          = attributes[:url]          || 'https://developer-api.nest.com/'
  @access_token = attributes[:access_token] || ENV["ACCESS_TOKEN"]
  @connection   = Faraday.new(url: @url, headers: { "Content-Type" => "application/json" }) do |b|
    b.adapter :excon
    b.authorization :Bearer, @access_token
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



2
3
4
# File 'lib/nest/real.rb', line 2

def access_token
  @access_token
end

#connectionObject (readonly)

Returns the value of attribute connection.



2
3
4
# File 'lib/nest/real.rb', line 2

def connection
  @connection
end

#urlObject (readonly)

Returns the value of attribute url.



2
3
4
# File 'lib/nest/real.rb', line 2

def url
  @url
end

Instance Method Details

#request(method, path, args = {}) ⇒ Object



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

def request(method, path, args={})
  res = connection.send(method) do |req|
    req.url path
  end

  while res.status == 307
    redirect_client = Faraday.new(res.headers["Location"]) do |b|
      b.adapter :excon
      b.authorization :Bearer, @access_token
    end

    res = redirect_client.send(method, "/")
  end

  Oj.load(res.body)
end