Class: Bkblz::V1::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/bkblz/v1/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Session

Returns a new instance of Session.



23
24
25
# File 'lib/bkblz/v1/session.rb', line 23

def initialize(config)
  @config = config
end

Instance Attribute Details

#auth_responseObject

Returns the value of attribute auth_response.



21
22
23
# File 'lib/bkblz/v1/session.rb', line 21

def auth_response
  @auth_response
end

#configObject

Returns the value of attribute config.



21
22
23
# File 'lib/bkblz/v1/session.rb', line 21

def config
  @config
end

Class Method Details

.authorize(config) {|session| ... } ⇒ Object

Yields:

  • (session)


11
12
13
14
15
16
17
18
# File 'lib/bkblz/v1/session.rb', line 11

def authorize(config, &block)
  session = Session.new config
  session.auth_response =
    session.send Bkblz::V1::AuthorizeAccountRequest.new

  yield(session) if block_given?
  session
end

Instance Method Details

#account_idObject



31
32
33
34
# File 'lib/bkblz/v1/session.rb', line 31

def 
  check_authorized
  auth_response.
end

#authorized?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/bkblz/v1/session.rb', line 36

def authorized?
  !!auth_response && !!auth_response.authorization_token
end

#create_download_url(url_suffix) ⇒ Object



40
41
42
43
# File 'lib/bkblz/v1/session.rb', line 40

def create_download_url(url_suffix)
  check_authorized
  URI.join auth_response.download_url, url_suffix
end

#create_get(url, addl_headers = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/bkblz/v1/session.rb', line 50

def create_get(url, addl_headers={})
  Bkblz.log.debug { "creating GET for request => #{url}" }
  check_authorized

  request = Net::HTTP::Get.new uri_from_url(url)
  add_request_headers request, addl_headers
  request
end

#create_post(url, body = nil, addl_headers = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bkblz/v1/session.rb', line 59

def create_post(url, body=nil, addl_headers={})
  Bkblz.log.debug { "creating POST for request => #{url}" }
  check_authorized

  request = Net::HTTP::Post.new uri_from_url(url)

  if body.is_a? Hash
    body = Bkblz::MapKeyFormatter.camelcase_keys(body).to_json
  end
  request.body = body if body

  add_request_headers request, addl_headers

  request
end

#create_url(url_suffix) ⇒ Object



45
46
47
48
# File 'lib/bkblz/v1/session.rb', line 45

def create_url(url_suffix)
  check_authorized
  URI.join auth_response.api_url, url_suffix
end

#send(request) ⇒ Object



27
28
29
# File 'lib/bkblz/v1/session.rb', line 27

def send(request)
  request.send self
end