netdocuments 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 524fb3b97fa5307755f2f2a81668f3e414e10971
4
- data.tar.gz: 2e4e46cfcbd2c596e2e09d65c42415d85406fa6a
3
+ metadata.gz: b6238d70c0a0c4691c514b63aa7661d2e5b5cf87
4
+ data.tar.gz: ef2cc92ebe68c13d163df48a937272c98bfa557f
5
5
  SHA512:
6
- metadata.gz: 51fc1b7fd8db20e1456d98676a04f1aa2c03263d95a6e8d2b467603c5d409f517323258d230b731867ae52e0de091b251e34ea2b7ffbc3b2382a7156bfb1b1eb
7
- data.tar.gz: e3f15645aa06a84ec62ec20c1a6262a3f884757e01e71c9598dbbe79e65b98e23b071f8f1349202ac8c173d3ed6291e26b6e0b1078c6bb05adea626a21c7f92a
6
+ metadata.gz: 003bd29507a95d63746e729d34e404c249d61ea2a1e72637ae0564574b3ebc7b4f0abc470906428407da883fc3b5bfa5d5da0b15cfdd3ed30093ec45dd52fcb7
7
+ data.tar.gz: 4e85d9c5671deccf8ef0ae839f163fbbb0d1bf46411452e0dd26b8e8b80e9a03fc9829831dd3f2537e290e350aa41def2f53c9751daf9ccff575110e3aeac782
@@ -12,6 +12,7 @@ require_relative 'netdocuments/path_formatter'
12
12
  require_relative 'netdocuments/configuration'
13
13
  require_relative 'netdocuments/client'
14
14
  require_relative 'netdocuments/access_token_master'
15
+ require_relative 'netdocuments/logger'
15
16
 
16
17
  module Netdocuments
17
18
 
@@ -27,5 +28,3 @@ module Netdocuments
27
28
  yield(configuration)
28
29
  end
29
30
  end
30
-
31
- $logger = Logger.new(Netdocuments.configuration.log_path)
@@ -6,14 +6,16 @@ module Netdocuments
6
6
  def post(opts = {})
7
7
  HTTParty.post(END_POINT + opts[:url],
8
8
  :body => opts[:body],
9
- :headers => opts[:headers])
9
+ :headers => opts[:headers],
10
+ :timeout => 180)
10
11
  end
11
12
 
12
13
 
13
14
  def get(opts = {})
14
15
  HTTParty.get(END_POINT + opts[:url],
15
16
  query: opts[:query],
16
- headers: opts[:headers])
17
+ headers: opts[:headers],
18
+ timeout: 180)
17
19
  end
18
20
 
19
21
  def put(opts = {})
@@ -23,15 +25,22 @@ module Netdocuments
23
25
  end
24
26
 
25
27
 
28
+ def delete(opts = {})
29
+ end
30
+
31
+
32
+ def client
33
+ Netdocuments::Client.instance
34
+ end
35
+
26
36
  def validate_config!
27
- #$logger.info "Token is: #{@client.access_token.valid? ? 'valid' : 'invalid'} ..Last generated: #{@client.access_token.last_generated_minutes_ago} minutes ago. "
28
- if @client.access_token.valid?
29
- true
30
- else
31
- $logger.info "Fetching new token..."
32
- @client.get_token!
37
+ if client.access_token.nil?
38
+ puts "--- Fetching first time token---"
39
+ client.get_token
40
+ elsif !client.access_token.valid?
41
+ puts "--- Fetching New token ---"
42
+ client.get_token
33
43
  end
34
44
  end
35
-
36
45
  end
37
46
  end
@@ -3,20 +3,19 @@ module Netdocuments
3
3
 
4
4
  attr_reader :id
5
5
 
6
- def initialize(client,opts = {})
7
- @client = client
6
+ def initialize(opts = {})
8
7
  validate_config!
9
- @headers = {'Authorization' => "Bearer #{@client.access_token.token}"}
10
8
  @id = opts[:id] if opts[:id]
11
9
  end
12
10
 
13
11
 
14
12
  def find_folder_tree_and_update_file_path
13
+ Netdocuments.logger.info "Starting: find_folder_tree_and_update_file_path"
15
14
  subfolders_count = 0
16
15
  Parallel.map(folders,in_threads: 50) do |folder|
17
16
  subfolders = folder.subfolders
18
- $logger.info "Collected: #{subfolders.count}"
19
- subfolders_count += subfolders.count #folder.subfolders.flatten.count
17
+ Netdocuments.logger.info "Collected: #{subfolders.count} subfolders in #{folder.name}"
18
+ subfolders_count += subfolders.count
20
19
  Parallel.map(subfolders,in_threads: 75){|node| node.update }
21
20
  end
22
21
  subfolders_count
@@ -27,16 +26,20 @@ module Netdocuments
27
26
  end
28
27
 
29
28
  def folders
30
- $logger.info "Fetching folders..."
29
+ Netdocuments.logger.info "Fetching folders..."
31
30
  response = get(url: "/v1/Cabinet/#{@id}/folders",
32
31
  query: {'$select' => "standardAttributes"},
33
- headers: @headers)
34
- response["ndList"]["standardList"]["ndProfile.DocumentStat"].collect {|i| Netdocuments::Folder.new(@client,{id: i['id'],name: i['name']})}
32
+ headers: headers)
33
+ response["ndList"]["standardList"]["ndProfile.DocumentStat"].collect {|i| Netdocuments::Folder.new(id: i['id'],name: i['name'])}
35
34
  end
36
35
 
37
36
 
38
37
  def info
39
- get(url: "/v1/Cabinet/#{@id}/info",headers: @headers)
38
+ get(url: "/v1/Cabinet/#{@id}/info",headers: headers)
39
+ end
40
+
41
+ def headers
42
+ {'Authorization' => "Bearer #{client.access_token.token}"}
40
43
  end
41
44
  end
42
45
  end
@@ -1,6 +1,8 @@
1
1
  module Netdocuments
2
2
  class Client
3
3
 
4
+ include Singleton
5
+
4
6
  attr_reader :refresh_token,:client_secret,:access_token
5
7
 
6
8
 
@@ -1,15 +1,19 @@
1
1
  module Netdocuments
2
2
  class Configuration
3
3
 
4
- attr_accessor :cabinet_id,:client_secret,:refresh_token,:log_path,:folder_path_id
4
+ attr_accessor :cabinet_id,:client_secret,:refresh_token,:log_path,:folder_path_id,
5
+ :cabinet_name,:logger
5
6
 
6
7
  def initialize
7
8
  log_dir = create_log_dir
8
9
  @cabinet_id = 'abc'
9
- @client_secret = 'xxxxx'
10
- @refresh_token = '1291271823781273712'
10
+ @client_secret = '1291271823781273712129127182378127371212912718237812737121291271823781273712'
11
+ @refresh_token = '1291271823781273712129127182378127371212912718237812737121291271823781273712'
11
12
  @log_path = "#{log_dir}/netdocuments.log"
12
13
  @folder_path_id = '48'
14
+ @cabinet_id = 'NG-12345'
15
+ @cabinet_name ='Test'
16
+ @logger = Logger.new(STDOUT)
13
17
  end
14
18
 
15
19
  def create_log_dir
@@ -1,10 +1,10 @@
1
1
  module Netdocuments
2
2
  class Document < Base
3
3
 
4
- def initialize(client,opts = {})
5
- @client = client
4
+ attr_reader :id,:name
5
+
6
+ def initialize(opts = {})
6
7
  validate_config!
7
- @headers = {'Authorization' => "Bearer #{@client.access_token.token}"}
8
8
  @id = opts[:id] if opts[:id]
9
9
  @name = opts[:name] if opts[:name]
10
10
  @query = opts[:query] if opts[:query]
@@ -13,14 +13,18 @@ module Netdocuments
13
13
 
14
14
 
15
15
  def info
16
- get(url: "/v1/Document/#{@id}/info",headers: @headers)
16
+ get(url: "/v1/Document/#{@id}/info",headers: headers)
17
17
  end
18
18
 
19
19
  def update_info(opts = {})
20
20
  put(url: "/v1/Document/#{@id}/info",
21
21
  query: opts[:query],
22
- headers: @headers.merge({'Content-Type' => 'application/json'}))
22
+ headers: headers.merge({'Content-Type' => 'application/json'}))
23
23
  end
24
24
 
25
+
26
+ def headers
27
+ {'Authorization' => "Bearer #{client.access_token.token}"}
28
+ end
25
29
  end
26
30
  end
@@ -1,20 +1,16 @@
1
1
  module Netdocuments
2
2
  class Folder < Base
3
3
 
4
- attr_reader :id,:name,:query,:parent,:cabinet_id,:client
4
+ attr_reader :id,:name,:query,:parent
5
5
 
6
- def initialize(client,opts = {})
7
- @client = client
6
+ def initialize(opts = {})
8
7
  validate_config!
9
- @headers = {'Authorization' => "Bearer #{@client.access_token.token}"}
10
- @id = opts[:id] if opts[:id]
8
+ @id = URI::encode(opts[:id]) if opts[:id]
11
9
  @name = opts[:name] if opts[:name]
12
10
  @query = opts[:query] if opts[:query]
13
11
  @parent = opts[:parent] if opts[:parent]
14
-
15
12
  end
16
13
 
17
-
18
14
  def cabinet_id
19
15
  Netdocuments.configuration.cabinet_id
20
16
  end
@@ -25,23 +21,23 @@ module Netdocuments
25
21
  name: @name,
26
22
  parent: @parent
27
23
  },
28
- headers: @headers)
24
+ headers: headers)
29
25
  end
30
26
 
31
27
  def info
32
28
  get(url: "/v1/Folder/#{@id}/info",
33
- headers: @headers)
29
+ headers: headers)
34
30
  end
35
31
 
36
32
  def folder_content
37
33
  begin
38
34
  response = get(url: "/v1/Folder/#{id}",
39
35
  query: {'$select' => 'standardAttributes'},
40
- headers: @headers)
36
+ headers: headers)
41
37
  response["ndList"]["standardList"].nil? ? [] : [response["ndList"]["standardList"]["ndProfile.DocumentStat"]].flatten
42
38
  rescue Exception => e
43
- $logger.error "********* #{id} ********* #{e.message}"
44
- $logger.error e.backtrace.join("\n")
39
+ #puts "********* #{id} ********* #{e.message}"
40
+ #puts e.backtrace.join("\n")
45
41
  []
46
42
  end
47
43
 
@@ -49,23 +45,23 @@ module Netdocuments
49
45
 
50
46
 
51
47
  def folder_extraction(opts = {})
52
- contents = Netdocuments::Folder.new(@client, {id: opts[:id]}).folder_content.compact
48
+ contents = Netdocuments::Folder.new(id: opts[:id]).folder_content.compact
53
49
  col = contents.collect do |folder|
54
- obj = Netdocuments::Node.new(@client,name: folder['name'],
50
+ obj = Netdocuments::Node.new(name: folder['name'],
55
51
  id: folder['id'],
56
52
  extension: folder['extension'],
57
- parent: opts[:parent])
53
+ parent: opts[:parent],
54
+ folder_path: "#{opts[:parent]}/#{folder['name']}")
58
55
  obj
59
56
  end
60
57
  end
61
58
 
62
59
 
63
60
  def subfolders
64
- $logger.info "Starting subfolder collection for: #{name}"
61
+ Netdocuments.logger.info "Starting subfolders collection for: #{name}"
65
62
  nodes = []
66
- ids = [{id: @id, parent: "WorkspaceResetTest/#{name}"}]
63
+ ids = [{id: @id, parent: "#{Netdocuments.configuration.cabinet_name}/#{name}"}]
67
64
  loop do
68
- sleep 0.5
69
65
  r = ids.collect do |id|
70
66
  folder_extraction(id)
71
67
  end.flatten!
@@ -79,15 +75,42 @@ module Netdocuments
79
75
 
80
76
 
81
77
  def ancestry
82
- response = get(url: "/v1/Folder/#{@id}/ancestry",headers: @headers)
78
+ response = get(url: "/v1/Folder/#{@id}/ancestry",headers: headers)
83
79
  end
84
80
 
85
81
  def update_info(opts = {})
86
82
  response = put(url: "/v1/Folder/#{@id}/info",
87
83
  query: opts[:query],
88
- headers: @headers.merge({'Content-Type' => 'application/json'}))
84
+ headers: headers.merge({'Content-Type' => 'application/json'}))
85
+ end
86
+
87
+ #udpating nodes via sidekiq worker
88
+ def find_subfolders_and_update_nodes
89
+ stats = {nodes_count: 0,folder_name: name}
90
+ puts "---- Starting subfolders collection for - #{name} ---- "
91
+ nodes = []
92
+ ids = [{id: @id, parent: "#{Netdocuments.configuration.cabinet_name}/#{name}"}]
93
+ loop do
94
+ r = ids.collect do |id|
95
+ folder_extraction(id)
96
+ end.flatten!
97
+ nodes << r
98
+ stats[:nodes_count] = stats[:nodes_count] + nodes.flatten.count
99
+ nodes.flatten.each do |node|
100
+ puts "---- Pushing: #{node.folder_path} in node queue ----"
101
+ NodeWorker.perform_async(node.id,node.extension,node.folder_path)
102
+ end
103
+ nodes = []
104
+ folders = r.select {|i| i.extension == 'ndfld'}
105
+ ids = folders.collect {|o| {id: o.id,parent: "#{o.parent}/#{o.name}"}}
106
+ break if ids.count == 0
107
+ end
108
+ stats
89
109
  end
90
110
 
91
111
 
112
+ def headers
113
+ {'Authorization' => "Bearer #{client.access_token.token}"}
114
+ end
92
115
  end
93
116
  end
@@ -2,25 +2,25 @@ module Netdocuments
2
2
  class Node < Base
3
3
 
4
4
 
5
- attr_reader :name,:id,:extension,:parent,:client
5
+ attr_reader :name,:id,:extension,:parent,:folder_path
6
6
 
7
- def initialize(client,opts = {})
8
- @client = client
7
+ def initialize(opts = {})
9
8
  validate_config!
10
9
  @name = opts[:name]
11
10
  @id = opts[:id]
12
11
  @extension = opts[:extension]
13
12
  @parent = opts[:parent]
13
+ @folder_path = opts[:folder_path]
14
14
  end
15
15
 
16
16
  def update
17
- $logger.info "Updating #{name} with file path as #{parent}"
17
+ # Netdocuments.logger.info "Updating #{name} with file path as #{folder_path}"
18
18
  if extension == 'ndfld'
19
- q1 = {'customAttributes' => [{"id" => "48","value" => Netdocuments::PathFormatter.new(parent).format}]}
20
- Netdocuments::Folder.new(@client,id: id).update_info({query: q1.to_json})
19
+ q1 = {'customAttributes' => [{"id" => Netdocuments.configuration.folder_path_id,"value" => Netdocuments::PathFormatter.new(folder_path).format}]}
20
+ Netdocuments::Folder.new(id: id).update_info({query: q1.to_json})
21
21
  else
22
- q1 = {'customAttributes' => [{"id" => "48","value" => Netdocuments::PathFormatter.new(parent).format}]}
23
- Netdocuments::Document.new(@client,id: id).update_info({query: q1.to_json})
22
+ q1 = {'customAttributes' => [{"id" => Netdocuments.configuration.folder_path_id,"value" => Netdocuments::PathFormatter.new(folder_path).format}]}
23
+ Netdocuments::Document.new(id: id).update_info({query: q1.to_json})
24
24
  end
25
25
  end
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netdocuments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - ankit8898
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-24 00:00:00.000000000 Z
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -133,3 +133,4 @@ signing_key:
133
133
  specification_version: 4
134
134
  summary: Ruby wrapper for Netdocuments REST API
135
135
  test_files: []
136
+ has_rdoc: