td-client 0.8.6 → 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ChangeLog CHANGED
@@ -1,4 +1,9 @@
1
1
 
2
+ == 2012-02-02 version 0.8.7
3
+
4
+ * Added SSL support
5
+
6
+
2
7
  == 2012-01-19 version 0.8.6
3
8
 
4
9
  * Check JSON format of HTTP responses
@@ -6,19 +6,19 @@ require 'td/client/model'
6
6
 
7
7
 
8
8
  class Client
9
- def self.authenticate(user, password)
10
- api = API.new(nil)
9
+ def self.authenticate(user, password, opts={})
10
+ api = API.new(nil, opts)
11
11
  apikey = api.authenticate(user, password)
12
12
  new(apikey)
13
13
  end
14
14
 
15
- def self.server_status
16
- api = API.new(nil)
15
+ def self.server_status(opts={})
16
+ api = API.new(nil, opts)
17
17
  api.server_status
18
18
  end
19
19
 
20
- def initialize(apikey)
21
- @api = API.new(apikey)
20
+ def initialize(apikey, opts={})
21
+ @api = API.new(apikey, opts)
22
22
  end
23
23
 
24
24
  attr_reader :api
@@ -99,8 +99,8 @@ class Client
99
99
  raise NotFoundError, "Table '#{db_name}.#{table_name}' does not exist"
100
100
  end
101
101
 
102
- def tail(db_name, table_name, count, to=nil, from=nil)
103
- @api.tail(db_name, table_name, count, to, from)
102
+ def tail(db_name, table_name, count, to=nil, from=nil, &block)
103
+ @api.tail(db_name, table_name, count, to, from, &block)
104
104
  end
105
105
 
106
106
  # => Job
@@ -16,10 +16,40 @@ end
16
16
 
17
17
 
18
18
  class API
19
- def initialize(apikey)
19
+ def initialize(apikey, opts={})
20
20
  require 'json'
21
21
  require 'time'
22
+ require 'uri'
22
23
  @apikey = apikey
24
+
25
+ endpoint = opts[:endpoint] || ENV['TD_API_SERVER'] || 'api.treasure-data.com'
26
+ uri = URI.parse(endpoint)
27
+
28
+ case uri.scheme
29
+ when 'http', 'https'
30
+ @host = uri.host
31
+ @port = uri.port
32
+ @ssl = uri.scheme == 'https'
33
+ @base_path = uri.path.to_s
34
+
35
+ else
36
+ if uri.port
37
+ # invalid URI
38
+ raise "Invalid endpoint: #{endpoint}"
39
+ end
40
+
41
+ # generic URI
42
+ @host, @port = endpoint.split(':', 2)
43
+ @port = @port.to_i
44
+ if opts[:ssl]
45
+ @port = 443 if @port == 0
46
+ @ssl = true
47
+ else
48
+ @port = 80 if @port == 0
49
+ @ssl = false
50
+ end
51
+ @base_path = ''
52
+ end
23
53
  end
24
54
 
25
55
  # TODO error check & raise appropriate errors
@@ -61,6 +91,7 @@ class API
61
91
  end
62
92
  end
63
93
 
94
+ # TODO support array types
64
95
  def self.normalize_type_name(name)
65
96
  case name
66
97
  when /int/i, /integer/i
@@ -183,7 +214,7 @@ class API
183
214
  return type
184
215
  end
185
216
 
186
- def tail(db, table, count, to, from)
217
+ def tail(db, table, count, to, from, &block)
187
218
  params = {'format' => 'msgpack'}
188
219
  params['count'] = count.to_s if count
189
220
  params['to'] = to.to_s if to
@@ -193,11 +224,16 @@ class API
193
224
  raise_error("Tail table failed", res)
194
225
  end
195
226
  require 'msgpack'
196
- result = []
197
- MessagePack::Unpacker.new.feed_each(body) {|row|
198
- result << row
199
- }
200
- return result
227
+ if block
228
+ MessagePack::Unpacker.new.feed_each(body, &block)
229
+ nil
230
+ else
231
+ result = []
232
+ MessagePack::Unpacker.new.feed_each(body) {|row|
233
+ result << row
234
+ }
235
+ return result
236
+ end
201
237
  end
202
238
 
203
239
 
@@ -611,23 +647,11 @@ class API
611
647
  end
612
648
 
613
649
  private
614
- host = 'api.treasure-data.com'
615
- port = 80
616
- if e = ENV['TD_API_SERVER']
617
- host, port_ = e.split(':',2)
618
- port_ = port_.to_i
619
- port = port_ if port_ != 0
620
- end
621
-
622
- HOST = host
623
- PORT = port
624
- USE_SSL = false
625
- BASE_URL = ''
626
650
 
627
651
  def get(url, params=nil)
628
652
  http, header = new_http
629
653
 
630
- path = BASE_URL + url
654
+ path = @base_path + url
631
655
  if params && !params.empty?
632
656
  path << "?"+params.map {|k,v|
633
657
  "#{k}=#{e v}"
@@ -643,7 +667,7 @@ class API
643
667
  def post(url, params=nil)
644
668
  http, header = new_http
645
669
 
646
- path = BASE_URL + url
670
+ path = @base_path + url
647
671
 
648
672
  if params && !params.empty?
649
673
  request = Net::HTTP::Post.new(path, header)
@@ -662,7 +686,7 @@ class API
662
686
 
663
687
  http.read_timeout = 600
664
688
 
665
- path = BASE_URL + url
689
+ path = @base_path + url
666
690
 
667
691
  header['Content-Type'] = 'application/octet-stream'
668
692
  header['Content-Length'] = size.to_s
@@ -686,8 +710,8 @@ class API
686
710
  require 'net/http'
687
711
  require 'time'
688
712
 
689
- http = Net::HTTP.new(HOST, PORT)
690
- if USE_SSL
713
+ http = Net::HTTP.new(@host, @port)
714
+ if @ssl
691
715
  http.use_ssl = true
692
716
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
693
717
  store = OpenSSL::X509::Store.new
@@ -1,5 +1,5 @@
1
1
  module TreasureData
2
2
 
3
- VERSION = '0.8.6'
3
+ VERSION = '0.8.7'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.6
4
+ version: 0.8.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-20 00:00:00.000000000Z
12
+ date: 2012-02-02 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: msgpack
16
- requirement: &70253485013900 !ruby/object:Gem::Requirement
16
+ requirement: &70350055043760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.4.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70253485013900
24
+ version_requirements: *70350055043760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &70253485012660 !ruby/object:Gem::Requirement
27
+ requirement: &70350055042760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.4.3
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70253485012660
35
+ version_requirements: *70350055042760
36
36
  description:
37
37
  email:
38
38
  executables: []