Class: Importio

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id = nil, api_key = nil, host = "https://query.import.io") ⇒ Importio

The main import.io client, used for managing the message channel and sending queries and receiving data



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/importio.rb', line 73

def initialize(user_id=nil, api_key=nil, host="https://query.import.io")
  # Initialises the client library with its configuration
  @host = host
  @proxy_host = nil
  @proxy_port = nil
  @user_id = user_id
  @api_key = api_key
  @username = nil
  @password = nil
  @login_host = nil
  @session = nil
  @queue = Queue.new
end

Instance Attribute Details

#sessionObject (readonly)

We use this only for a specific test case



88
89
90
# File 'lib/importio.rb', line 88

def session
  @session
end

Instance Method Details

#connectObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/importio.rb', line 126

def connect
  # Connect this client to the import.io server if not already connected

  # Check if there is a session already first
  if @session != nil
    return
  end

  @session = Session::new(self, @host, @user_id, @api_key, @proxy_host, @proxy_port)
  @session.connect()

  # This should be a @queue.clone, but this errors in 2.1 branch of Ruby: #9718
  # q = @queue.clone
  q = Queue.new
  until @queue.empty?
    q.push(@queue.pop(true))
  end
  @queue = Queue.new

  until q.empty?
    query_data = q.pop(true) rescue nil
    if query_data
      query(query_data.query, query_data.callback)
    end
  end
end

#disconnectObject



153
154
155
156
157
158
159
160
161
162
# File 'lib/importio.rb', line 153

def disconnect
  # Call this method to ask the client library to disconnect from the import.io server
  # It is best practice to disconnect when you are finished with querying, so as to clean
  # up resources on both the client and server

  if @session != nil
    @session.disconnect()
    @session = nil
  end
end

#joinObject



171
172
173
174
175
176
# File 'lib/importio.rb', line 171

def join
  # This method joins the threads that are running together in the session, so we can wait for them to be finished
  if @session != nil
    return @session.join()
  end
end

#login(username, password, host = "https://api.import.io") ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/importio.rb', line 96

def (username, password, host="https://api.import.io")
  # If you want to use cookie-based authentication, this method will log you in with a username and password to get a session
  @username = username
  @password = password
  @login_host = host

  # If we don't have a session, then connect one
  if @session == nil
    connect()
  end

  # Once connected, do the login
  @session.(@username, @password, @login_host)
end

#proxy(host, port) ⇒ Object



90
91
92
93
94
# File 'lib/importio.rb', line 90

def proxy(host, port)
  # If you want to configure an HTTP proxy, use this method to do so
  @proxy_host = host
  @proxy_port = port
end

#query(query, callback) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/importio.rb', line 178

def query(query, callback)
  # This method takes an import.io Query object and either queues it, or issues it to the server
  # depending on whether the session is connected
  
  if @session == nil || !@session.connected
    @queue << {"query"=>query,"callback"=>callback}
    return
  end

  @session.query(query, callback)
end

#reconnectObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/importio.rb', line 111

def reconnect
  # Reconnects the client to the platform by establishing a new session
  
  # Disconnect an old session, if there is one
  if @session != nil
    disconnect()
  end

  if @username != nil
    (@username, @password, @login_host)
  else
    connect()
  end
end

#stopObject



164
165
166
167
168
169
# File 'lib/importio.rb', line 164

def stop
  # This method stops all of the threads that are currently running in the session
  if @session != nil
    return @session.stop()
  end
end