portatext 1.1.1 → 1.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8ef581d57fc7b2b4a813b6522661766edc4388b
4
- data.tar.gz: 4dddcf5fd947ddd806cb5d2a7fb6090720e12257
3
+ metadata.gz: 8060e94cc7da5a6ec5f4b61a2019438b551f8c65
4
+ data.tar.gz: c4648838ececee96cb984c2e4524636d604c2e11
5
5
  SHA512:
6
- metadata.gz: 576c63568bd6bc6b8c6370af7d0a6302cafccf7b642a40997260d5984031adea3003f7c573c1e58d5e937f499bf2e2a5d1522034f0b6ebdc48ef14ccab06c8ef
7
- data.tar.gz: e66e7fefa293cbb06b75b62ddd39f60bb9f616de78aefff46b0805caf86707d9d45cdd983ec4ff748af81974b7cac434caf14120565c0430721503931890dcdf
6
+ metadata.gz: 97e8c5e54143cf3a406afccd617bf7f7972856c644fda997fd8902cf58bd1670c42afc34e1e5c919b176e7544748fc029f2cf3383956214b9b1342ff08feb296
7
+ data.tar.gz: 71b5d73f5d6b56cf16e1004503c751255c125550a5127f6b692df83a31f4fcf0faa0f1feab6169c1493b6dffb6bfc9cef1a5c0e5a4e249182bb01bd7ec33a03b
@@ -17,8 +17,10 @@ module PortaText
17
17
 
18
18
  def method_missing(method, *_arguments, &_block)
19
19
  method = method.to_s.split('_').map(&:capitalize)
20
- name = "PortaText::Command::Api::#{method.join('')}"
21
- class_name = Object.const_get name
20
+ class_name = Object.const_get('PortaText')
21
+ .const_get('Command')
22
+ .const_get('Api')
23
+ .const_get(method.join(''))
22
24
  command = class_name.new
23
25
  command.client = self
24
26
  command
@@ -26,27 +28,35 @@ module PortaText
26
28
 
27
29
  # rubocop:disable Metrics/MethodLength
28
30
  # rubocop:disable Metrics/AbcSize
29
- def run(endpoint, method, content_type, body, auth = nil)
31
+ # rubocop:disable Metrics/ParameterLists
32
+ def run(
33
+ endpoint, method, content_type, accept_content_type,
34
+ body, output_file = nil, auth = nil
35
+ )
30
36
  true_endpoint = "#{@endpoint}/#{endpoint}"
31
37
  auth ||= auth_method(auth)
32
- headers = form_headers content_type, auth
38
+ headers = form_headers content_type, accept_content_type, auth
33
39
  @logger.debug "Calling #{method} #{true_endpoint} with #{auth}"
34
40
  descriptor = PortaText::Command::Descriptor.new(
35
- true_endpoint, method, headers, body
41
+ true_endpoint, method, headers, body, output_file
36
42
  )
37
43
  ret_code, ret_headers, ret_body = @executor.execute descriptor
38
44
  @logger.debug "Got: #{ret_code} / #{ret_headers} / #{ret_body}"
39
- result = PortaText::Command::Result.new(
40
- ret_code, ret_headers, JSON.parse(ret_body)
41
- )
45
+ ret_body = '{}' if ret_body.nil?
46
+ ret_body = JSON.parse ret_body
47
+ result = PortaText::Command::Result.new ret_code, ret_headers, ret_body
42
48
  if ret_code.eql?(401) && auth.eql?(:session_token)
43
49
  login!
44
- result = run endpoint, method, content_type, body
50
+ result = run(
51
+ endpoint, method, content_type, accept_content_type,
52
+ body, output_file, auth
53
+ )
45
54
  end
46
55
  assert_result descriptor, result
47
56
  end
48
57
  # rubocop:enable Metrics/MethodLength
49
58
  # rubocop:enable Metrics/AbcSize
59
+ # rubocop:enable Metrics/ParameterLists
50
60
 
51
61
  def initialize
52
62
  @logger = Logger.new nil
@@ -60,7 +70,10 @@ module PortaText
60
70
  private
61
71
 
62
72
  def login!
63
- result = run 'login', :post, 'application/json', '', :basic
73
+ result = run(
74
+ 'login', :post, 'application/json', 'application/json',
75
+ '', nil, :basic
76
+ )
64
77
  @session_token = result.data['token']
65
78
  end
66
79
 
@@ -94,10 +107,10 @@ module PortaText
94
107
  errors[code]
95
108
  end
96
109
 
97
- def form_headers(content_type, auth)
110
+ def form_headers(content_type, accept_content_type, auth)
98
111
  headers = {
99
112
  'Content-Type' => content_type,
100
- 'Accept' => 'application/json'
113
+ 'Accept' => accept_content_type
101
114
  }
102
115
  case auth
103
116
  when :session_token
@@ -35,7 +35,10 @@ module PortaText
35
35
  # rubocop:disable Metrics/MethodLength
36
36
  def create_request(uri, method, body)
37
37
  method = method.to_s.capitalize
38
- request = Object.const_get("Net::HTTP::#{method}").new uri
38
+ request = Object.const_get('Net')
39
+ .const_get('HTTP')
40
+ .const_get(method)
41
+ .new uri
39
42
  data = /^file:(.*)$/.match(body)
40
43
  if data.nil?
41
44
  request.body = body
@@ -48,16 +51,31 @@ module PortaText
48
51
  end
49
52
  # rubocop:enable Metrics/MethodLength
50
53
 
51
- def request!(descriptor, http, request)
52
- descriptor.headers.each_pair do |k, v|
53
- request[k] = v
54
+ def writer_proc(file)
55
+ if file.nil?
56
+ nil
57
+ else
58
+ proc do |response|
59
+ open file, 'w+' do |io|
60
+ response.read_body { |chunk| io.write chunk }
61
+ end
62
+ end
54
63
  end
55
- result = http.request request
56
- headers = result.to_hash.each_with_object({}) do |(k, v), acc|
64
+ end
65
+
66
+ def normalize_headers(result)
67
+ result.to_hash.each_with_object({}) do |(k, v), acc|
57
68
  acc[k.downcase] = v.shift
58
69
  acc
59
70
  end
60
- [result.code.to_i, headers, result.body]
71
+ end
72
+
73
+ def request!(descriptor, http, request)
74
+ descriptor.headers.each_pair { |k, v| request[k] = v }
75
+ body = nil
76
+ result = http.request request, &(writer_proc descriptor.output_file)
77
+ body = result.body if descriptor.output_file.nil?
78
+ [result.code.to_i, normalize_headers(result), body]
61
79
  end
62
80
  end
63
81
  end
@@ -16,7 +16,12 @@ module PortaText
16
16
  set :file, file
17
17
  end
18
18
 
19
+ def save_to(file)
20
+ set :accept_file, file
21
+ end
22
+
19
23
  def endpoint(_method)
24
+ return 'blacklist/contacts' unless @args[:accept_file].nil?
20
25
  return 'blacklist/contacts' unless @args[:file].nil?
21
26
  return 'blacklist' if @args[:number].nil?
22
27
  number = @args[:number]
@@ -24,11 +24,16 @@ module PortaText
24
24
  set :file, file
25
25
  end
26
26
 
27
+ def save_to(file)
28
+ set :accept_file, file
29
+ end
30
+
27
31
  def endpoint(_method)
28
32
  return 'contact_lists' if @args[:id].nil?
29
33
  id = @args[:id]
30
34
  @args.delete :id
31
35
  return "contact_lists/#{id}/contacts" unless @args[:file].nil?
36
+ return "contact_lists/#{id}/contacts" unless @args[:accept_file].nil?
32
37
  "contact_lists/#{id}"
33
38
  end
34
39
  end
@@ -40,6 +40,11 @@ module PortaText
40
40
  'application/json'
41
41
  end
42
42
 
43
+ def accept_content_type(_method)
44
+ return 'text/csv' unless @args[:accept_file].nil?
45
+ 'application/json'
46
+ end
47
+
43
48
  def body(_method)
44
49
  return "file:#{@args[:file]}" unless @args[:file].nil?
45
50
  return '' if @args.size.eql? 0
@@ -53,7 +58,14 @@ module PortaText
53
58
  private
54
59
 
55
60
  def run(method)
56
- @client.run endpoint(method), method, content_type(method), body(method)
61
+ a_type = accept_content_type method
62
+ command_endpoint = endpoint(method)
63
+ file = @args[:accept_file]
64
+ @args.delete :accept_file
65
+ @client.run(
66
+ command_endpoint, method, content_type(method),
67
+ a_type, body(method), file
68
+ )
57
69
  end
58
70
  end
59
71
  end
@@ -11,12 +11,14 @@ module PortaText
11
11
  attr_accessor :method
12
12
  attr_accessor :headers
13
13
  attr_accessor :body
14
+ attr_accessor :output_file
14
15
 
15
- def initialize(uri, method, headers, body)
16
+ def initialize(uri, method, headers, body, output_file = nil)
16
17
  @uri = uri
17
18
  @method = method
18
19
  @headers = headers
19
20
  @body = body
21
+ @output_file = output_file
20
22
  end
21
23
  end
22
24
  end
@@ -15,7 +15,7 @@ module PortaText
15
15
  @code = code
16
16
  @headers = headers
17
17
  @data = data
18
- @success = code > 199 && code < 300 && data['success']
18
+ @success = code > 199 && code < 300
19
19
  end
20
20
  end
21
21
  end
data/portatext.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'portatext'
3
- s.version = '1.1.1'
3
+ s.version = '1.1.2'
4
4
  s.summary = 'Official PortaText API ruby client'
5
5
  s.description = 'This is the official PortaText API ruby client'
6
6
  s.authors = ['PortaText']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: portatext
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - PortaText
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-05 00:00:00.000000000 Z
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simplecov