Class: Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/juxta/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username = nil, password = nil) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/juxta/connection.rb', line 10

def initialize( url, username=nil, password=nil )

   @authtoken = "Basic #{Base64.encode64("#{username}:#{password}")}"
   @url = url
   @workspace = "public"
   
   @timeout = 600        # 10 minute get timeout...
   @open_timeout = 600   # 10 minute post timeout

   opts = {
       :timeout => @timeout,
       :open_timeout=> @open_timeout
   }

   @rest_client = RestClient::Resource.new( @url, opts )
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/juxta/connection.rb', line 7

def url
  @url
end

#workspaceObject

Returns the value of attribute workspace.



8
9
10
# File 'lib/juxta/connection.rb', line 8

def workspace
  @workspace
end

Instance Method Details

#delete(request, in_workspace = true) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/juxta/connection.rb', line 90

def delete( request, in_workspace=true )
   start_time = Time.now.to_f
   url = in_workspace ? make_url( request ) : request   
   resp =  @rest_client[ url ].delete :authorization => @authtoken
   dump_time( "delete", start_time )
   return resp
end

#dump_time(what, start_time) ⇒ Object



106
107
108
# File 'lib/juxta/connection.rb', line 106

def dump_time( what, start_time )
  #puts "#{what}: %0.2f mS" % ( ( Time.now.to_f - start_time ) * 1000 )
end

#get(request, in_workspace = true) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/juxta/connection.rb', line 35

def get( request, in_workspace=true )
   start_time = Time.now.to_f
   url = in_workspace ? make_url( request ) : request 
   resp = @rest_client[ url ].get :content_type => "application/json", :accept=>'application/json', :authorization => @authtoken
   dump_time( "get", start_time )
   JSON.parse(resp)
end

#get_html(request) ⇒ Object



43
44
45
46
47
48
# File 'lib/juxta/connection.rb', line 43

def get_html( request )
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( request ) ].get :content_type => "text/html", :authorization => @authtoken
   dump_time( "get", start_time )
   return resp
end

#get_ws_versionObject



27
28
29
30
31
32
33
# File 'lib/juxta/connection.rb', line 27

def get_ws_version()
   start_time = Time.now.to_f
   resp = @rest_client.get :content_type => "application/json", :authorization => @authtoken
   dump_time( "get", start_time )
   json = JSON.parse(resp)
   return json['version']
end

#get_xml(request) ⇒ Object



50
51
52
53
54
55
# File 'lib/juxta/connection.rb', line 50

def get_xml( request )
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( request ) ].get :content_type => "text/xml", :authorization => @authtoken
   dump_time( "get", start_time )
   return resp
end

#make_full_url(request) ⇒ Object



102
103
104
# File 'lib/juxta/connection.rb', line 102

def make_full_url( request ) 
 "#{@url}/#{make_url(request)}"
end

#make_url(request) ⇒ Object



98
99
100
# File 'lib/juxta/connection.rb', line 98

def make_url( request )
  "#{@workspace}/#{request}"
end

#post(request, payload, in_workspace = true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/juxta/connection.rb', line 57

def post( request, payload, in_workspace=true )
   start_time = Time.now.to_f
   url = in_workspace ? make_url( request ) : request   
   if payload.nil? == true
      resp = @rest_client[ url ].post "", :authorization => @authtoken
   else
      resp = @rest_client[ url ].post payload.to_json, :authorization => @authtoken, :content_type => "application/json"
   end
   dump_time( "post", start_time )
   return resp
end

#put(request, payload) ⇒ Object



83
84
85
86
87
88
# File 'lib/juxta/connection.rb', line 83

def put( request, payload )
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( request ) ].put payload.to_json, :authorization => @authtoken, :content_type => "application/json"
   dump_time( "put", start_time )
   return resp
end

#upload_file(file_name, content_type, file) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/juxta/connection.rb', line 69

def upload_file( file_name, content_type, file ) 
   opts = {
      :sourceName=> file_name,
      :contentType => content_type, 
      :sourceFile=> file, 
      :multipart => true
   }
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( "source" ) ].post opts, :authorization => @authtoken
   dump_time( "post", start_time )
   json = JSON.parse(resp)
   return json[ 0 ]
end