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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/egnyte/session.rb', line 9

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

  @strategy = strategy # the authentication strategy to use.
  raise Egnyte::UnsupportedAuthStrategy unless [:implicit, :password].include? @strategy

  @backoff = backoff # only two requests are allowed a second by Egnyte.
  @api = 'pubapi' # currently we only support the public API.

  @username = opts[:username]

  # 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_DOMAIN}",
    :authorize_url => "/puboauth/token",
    :token_url => "/puboauth/token"
  })

  if @strategy == :implicit
    @access_token = OAuth2::AccessToken.new(@client, opts[:access_token]) if opts[:access_token]
  elsif @strategy == :password
    if opts[:access_token]
      @access_token = OAuth2::AccessToken.new(@client, opts[:access_token])
    else
      raise Egnyte::OAuthUsernameRequired unless @username
      raise Egnyte::OAuthPasswordRequired unless opts[:password]
      if true #OS.windows?
        body = {
          :client_id => opts[:key],
          :username => @username,
          :password => opts[:password],
          :grant_type => 'password'
        }.map {|k,v| "#{k}=#{v}"}.join("&")
        url = "https://#{@domain}.#{EGNYTE_DOMAIN}/puboauth/token"
        response = (url, body, return_parsed_response=true)
        @access_token = OAuth2::AccessToken.new(@client, response["access_token"])
      else
        @access_token = @client.password.get_token(@username, opts[:password])
      end
    end
    @username = info["username"] unless @username
  end
  
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/egnyte/session.rb', line 7

def access_token
  @access_token
end

#apiObject

Returns the value of attribute api.



6
7
8
# File 'lib/egnyte/session.rb', line 6

def api
  @api
end

#domainObject

Returns the value of attribute domain.



6
7
8
# File 'lib/egnyte/session.rb', line 6

def domain
  @domain
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/egnyte/session.rb', line 6

def username
  @username
end

Instance Method Details

#authorize_url(redirect_uri) ⇒ Object



63
64
65
# File 'lib/egnyte/session.rb', line 63

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

#create_access_token(token) ⇒ Object



67
68
69
# File 'lib/egnyte/session.rb', line 67

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

#delete(url, return_parsed_response = true) ⇒ Object



77
78
79
80
81
# File 'lib/egnyte/session.rb', line 77

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

#get(url, return_parsed_response = true) ⇒ Object



71
72
73
74
75
# File 'lib/egnyte/session.rb', line 71

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

#infoObject



55
56
57
# File 'lib/egnyte/session.rb', line 55

def info
  information
end

#informationObject



59
60
61
# File 'lib/egnyte/session.rb', line 59

def information
  get("https://#{@domain}.#{EGNYTE_DOMAIN}/#{@api}/v1/userinfo", return_parsed_response=true)
end

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



91
92
93
94
95
96
97
# File 'lib/egnyte/session.rb', line 91

def (url, body, return_parsed_response=true)
  uri = URI.parse(Egnyte::Helper.encode_url(url))
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = body
  request.content_type = "application/x-www-form-urlencoded"
  resp = request(uri, request, return_parsed_response)
end

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



115
116
117
118
119
120
121
122
123
# File 'lib/egnyte/session.rb', line 115

def multipart_post(url, filename, data, return_parsed_response=true)
  uri = URI.parse(Egnyte::Helper.encode_url(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

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



99
100
101
102
103
104
105
# File 'lib/egnyte/session.rb', line 99

def patch(url, body, return_parsed_response=true)
  uri = URI.parse(Egnyte::Helper.encode_url(url))
  request = Net::HTTP::Patch.new(uri.request_uri)
  request.body = body
  request.content_type = "application/json"
  resp = request(uri, request, return_parsed_response)
end

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



83
84
85
86
87
88
89
# File 'lib/egnyte/session.rb', line 83

def post(url, body, return_parsed_response=true)
  uri = URI.parse(Egnyte::Helper.encode_url(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

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



107
108
109
110
111
112
113
# File 'lib/egnyte/session.rb', line 107

def put(url, body, return_parsed_response=true)
  uri = URI.parse(Egnyte::Helper.encode_url(url))
  request = Net::HTTP::Put.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.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/egnyte/session.rb', line 127

def streaming_download(url, opts)
  uri = URI.parse(Egnyte::Helper.encode_url(url))

  params = {
    :content_length_proc => opts[:content_length_proc],
    :progress_proc => opts[:progress_proc],
    'Authorization' => "Bearer #{@access_token.token}"
  }

  open(uri, params)
end