phraseapp-ruby 1.2.4 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,22 +15,15 @@ module PhraseApp
15
15
  @host
16
16
  end
17
17
 
18
- def validate
18
+ def validate!
19
19
  if self.username.to_s == "" && self.token.to_s == ""
20
- return raise("either username or token must be given")
21
- else
22
- return nil
20
+ raise ValidationError.new("either username or token must be given")
23
21
  end
24
22
  end
25
23
 
26
24
  def authenticate(req)
27
- if err = load_config
28
- return err
29
- end
30
-
31
- if err = validate
32
- return err
33
- end
25
+ load_config
26
+ validate!
34
27
 
35
28
  if self.token && self.token != ""
36
29
  req["Authorization"] = "token #{self.token}"
@@ -41,13 +34,11 @@ module PhraseApp
41
34
  raise "Multi-Factor Token required in config but not provided." unless self.tfa_token
42
35
  req["X-PhraseApp-OTP"] = auth.tfa_token
43
36
  end
44
- else
45
- raise "No authentication present"
46
37
  end
47
38
 
48
39
  return nil
49
-
50
40
  end
41
+
51
42
  private
52
43
  def load_config
53
44
  path = config
@@ -81,9 +72,10 @@ module PhraseApp
81
72
  if configCredentials.debug
82
73
  self.debug = configCredentials.debug
83
74
  end
84
-
85
- return nil
86
75
  end
87
76
  end
88
77
  end
89
78
  end
79
+
80
+ class ValidationError < StandardError
81
+ end
@@ -10,12 +10,10 @@ module PhraseApp
10
10
 
11
11
  def initialize(http_response)
12
12
  hash = JSON.load(http_response.body)
13
- self.message = hash['message']
13
+ @message = hash['message']
14
14
  end
15
15
 
16
- def error
17
- self.message
18
- end
16
+ alias_method :error, :message
19
17
  end
20
18
 
21
19
  class ValidationErrorResponse
@@ -24,8 +22,8 @@ module PhraseApp
24
22
 
25
23
  def initialize(http_response)
26
24
  hash = JSON.load(http_response.body)
27
- self.message = hash['message']
28
- self.errors = hash['errors'].map { |error| ValidationErrorMessage.new(error) }
25
+ @message = hash['message']
26
+ @errors = hash['errors'].map { |error| ValidationErrorMessage.new(error) }
29
27
  end
30
28
  end
31
29
 
@@ -33,13 +31,13 @@ module PhraseApp
33
31
  attr_reader :resource, :field, :message
34
32
 
35
33
  def initialize(error)
36
- self.resource = error["resource"]
37
- self.field = error["field"]
38
- self.message = error["message"]
34
+ @resource = error["resource"]
35
+ @field = error["field"]
36
+ @message = error["message"]
39
37
  end
40
38
 
41
39
  def to_s
42
- return sprintf("\t[%s:%s] %s", self.resource, self.field, self.message)
40
+ sprintf("\t[%s:%s] %s", self.resource, self.field, self.message)
43
41
  end
44
42
  end
45
43
 
@@ -47,13 +45,13 @@ module PhraseApp
47
45
  attr_reader :limit, :remaining, :reset
48
46
 
49
47
  def initialize(resp)
50
- self.limit = resp["X-Rate-Limit-Limit"].to_i
51
- self.remaining = resp["X-Rate-Limit-Remaining"].to_i
52
- self.reset = Time.at(resp["X-Rate-Limit-Reset"].to_i)
48
+ @limit = resp["X-Rate-Limit-Limit"].to_i
49
+ @remaining = resp["X-Rate-Limit-Remaining"].to_i
50
+ @reset = Time.at(resp["X-Rate-Limit-Reset"].to_i)
53
51
  end
54
52
 
55
53
  def to_s
56
- sprintf("Rate limit exceeded: from %d requests %d are remaning (reset in %d seconds)", self.limit, self.remaining, int64(rle.Reset.Sub(time.Now()).Seconds()))
54
+ sprintf("Rate limit exceeded: from %d requests %d are remaning (reset in %d seconds)", self.limit, self.remaining, int64(rle.Reset.Sub(time.Now()).Seconds()))
57
55
  end
58
56
  end
59
57
  end
@@ -64,15 +62,8 @@ module PhraseApp
64
62
  res << "Content-Disposition: form-data; name=\"#{k}\"\r\n"
65
63
  # res << "Content-Type: #{headers["Content-Type"]}\r\n" if headers["Content-Type"]
66
64
  res << "\r\n"
67
- if v.is_a?(Array)
68
- v.each do |vv|
69
- res << vv+","
70
- end
71
- res << "\r\n"
72
- else
73
- res << "#{v}\r\n"
74
- end
75
- res
65
+ res << Array(v).join(',')
66
+ res << "\r\n"
76
67
  end
77
68
  end
78
69
 
@@ -151,7 +142,7 @@ module PhraseApp
151
142
  end
152
143
  resp = http.request(req)
153
144
 
154
- err = handleResponseStatus(resp, status)
145
+ err = handleResponseStatus(resp, status)
155
146
 
156
147
  return resp, err
157
148
  end
@@ -159,17 +150,17 @@ module PhraseApp
159
150
  def self.handleResponseStatus(resp, expectedStatus)
160
151
  case resp.code.to_i
161
152
  when expectedStatus
162
- return
153
+ return
163
154
  when 400
164
155
  return PhraseApp::RequestErrors::ErrorResponse.new(resp)
165
156
  when 404
166
- return raise("not found")
157
+ return raise("not found")
167
158
  when 422
168
159
  return PhraseApp::RequestErrors::ValidationErrorResponse.new(resp)
169
160
  when 429
170
161
  return PhraseApp::RequestErrors::RateLimitingError.new(resp)
171
162
  else
172
- return raise("unexpected status code (#{resp.code}) received; expected #{expectedStatus}")
163
+ return raise("unexpected status code (#{resp.code}) received; expected #{expectedStatus}")
173
164
  end
174
165
  end
175
166
  end
@@ -1,3 +1,3 @@
1
1
  module PhraseApp
2
- VERSION = '1.2.4'
2
+ VERSION = '1.2.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phraseapp-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - PhraseApp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-22 00:00:00.000000000 Z
11
+ date: 2016-04-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: PhraseApp API client libary
14
14
  email:
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  version: '0'
42
42
  requirements: []
43
43
  rubyforge_project:
44
- rubygems_version: 2.4.5.1
44
+ rubygems_version: 2.5.1
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: Interact with the PhraseApp API