Class: Egnyte::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, strategy = :implicit, backoff = 0.5) ⇒ Session

Returns a new instance of Session.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/egnyte/session.rb', line 6

def initialize(opts, strategy=:implicit, backoff=0.5)

  @strategy = strategy # the authentication strategy to use.
  raise Egnyte::UnsupportedAuthStrategy unless @strategy == :implicit
  
  @backoff = backoff # only two requests are allowed a second by Egnyte.
  @api = 'pubapi' # currently we only support the public API.

  # the domain of the egnyte account to interact with.
  raise Egnyte::DomainRequired unless @domain = opts[:domain]

  @client = OAuth2::Client.new(opts[:key], nil, {
    :site => "https://#{@domain}.egnyte.com",
    :authorize_url => "/puboauth/token"
  })

  @access_token = OAuth2::AccessToken.new(@client, opts[:access_token]) if opts[:access_token]
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



4
5
6
# File 'lib/egnyte/session.rb', line 4

def api
  @api
end

#domainObject

Returns the value of attribute domain.



4
5
6
# File 'lib/egnyte/session.rb', line 4

def domain
  @domain
end

Instance Method Details

#authorize_url(redirect_uri) ⇒ Object



25
26
27
# File 'lib/egnyte/session.rb', line 25

def authorize_url(redirect_uri)
  @client.implicit.authorize_url(:redirect_uri => redirect_uri)
end

#create_access_token(token) ⇒ Object



29
30
31
# File 'lib/egnyte/session.rb', line 29

def create_access_token(token)
  @access_token = OAuth2::AccessToken.new(@client, token) if @strategy == :implicit
end

#delete(url, return_parsed_response = true) ⇒ Object



39
40
41
42
43
# File 'lib/egnyte/session.rb', line 39

def delete(url, return_parsed_response=true)
  uri = URI.parse(url)
  request = Net::HTTP::Delete.new( uri.request_uri )
  resp = request( uri, request, return_parsed_response )
end

#get(url, return_parsed_response = true) ⇒ Object



33
34
35
36
37
# File 'lib/egnyte/session.rb', line 33

def get(url, return_parsed_response=true)
  uri = URI.parse(url)
  request = Net::HTTP::Get.new( uri.request_uri )
  resp = request( uri, request, return_parsed_response )
end

#multipart_post(url, filename, data, return_parsed_response = true) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/egnyte/session.rb', line 53

def multipart_post(url, filename, data, return_parsed_response=true)
  uri = URI.parse(url)

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = data.read
  request.content_type = 'application/binary'

  resp = request(uri, request, return_parsed_response)
end

#post(url, body, return_parsed_response = true) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/egnyte/session.rb', line 45

def post(url, body, return_parsed_response=true)
  uri = URI.parse(url)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = body
  request.content_type = "application/json"
  resp = request(uri, request, return_parsed_response)
end

#streaming_download(url, opts) ⇒ Object

perform a streaming download of a file rather than in-memory.



65
66
67
68
69
70
71
72
73
# File 'lib/egnyte/session.rb', line 65

def streaming_download(url, opts)
  params = {
    :content_length_proc => opts[:content_length_proc],
    :progress_proc => opts[:progress_proc],
    'Authorization' => "Bearer #{@access_token.token}"
  }
  
  open(url, params)
end