esa 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68f57d011747ff152b15ebe7eccd82591ef53acc
4
- data.tar.gz: bd1c794d59fffb421390f764e17eb5b4e35baedb
3
+ metadata.gz: 329f57a926a65830b8354c22344a8157c1b3de43
4
+ data.tar.gz: 17a14d17d1e788b3741eb8bd3c69aea848abff7c
5
5
  SHA512:
6
- metadata.gz: 2ac4eac59e6e15e59a1d8afa2debf2e2723f5b6760e3d3b734ad79650a0fe37764cc3a7cf2a69e585c8c1c3cb85ac3d9fc5867ce27b8ad87d618cfbe3a1e7b3a
7
- data.tar.gz: 757bb7c3a20dc6bb22f312e5f16f6d2db2996a4c4854ba04b279f065b4a06968171ad3d1e874a5b53f2a0abac6a366d4690c73423cbbeb482d5c449768f88a50
6
+ metadata.gz: a61640a6684988e31e0a88d9024533204243fd3777a559a64ef2540315064e98a6a47112203d59ed8ba3e84b9554462ddbfa526eb144087e0ae563007380e1ed
7
+ data.tar.gz: a8a8587f8dab6de832b47e3c1dada4f8e8a27800224adee3da4b876b6a7511b5b1fdac62ca5d7f5f686d44a29d4bc42bddf014075a36c1e025c036f6dc7cb633
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 1.1.0 (2015-10-01)
2
+ - [Enable to Upload Attachment by fukayatsu · Pull Request #13 · esaio/esa-ruby](https://github.com/esaio/esa-ruby/pull/13)
3
+
1
4
  ## 1.0.0 (2015-09-25)
2
5
  - Production Ready (\\( ⁰⊖⁰)/)
3
6
  - [Add Stats API by fukayatsu · Pull Request #12 · esaio/esa-ruby](https://github.com/esaio/esa-ruby/pull/12)
data/README.md CHANGED
@@ -74,6 +74,12 @@ client.update_comment(comment_id, body_md: 'bazbaz')
74
74
  client.delete_comment(comment_id)
75
75
  #=> DELETE /v1/teams/foobar/comments/123
76
76
 
77
+
78
+ # Upload Attachment(beta)
79
+ client.upload_attachment('/Users/foo/Desktop/foo.png') # Path
80
+ client.upload_attachment(File.open('/Users/foo/Desktop/foo.png')) # File
81
+ client.upload_attachment('http://example.com/foo.png') # URL
82
+
77
83
  ```
78
84
 
79
85
 
data/esa.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency "faraday", "~> 0.9"
22
22
  spec.add_runtime_dependency "faraday_middleware"
23
+ spec.add_runtime_dependency "mime-types", '~> 2.6'
23
24
  spec.add_development_dependency "bundler", "~> 1.7"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
25
26
  spec.add_development_dependency "pry"
@@ -1,5 +1,7 @@
1
1
  module Esa
2
2
  module ApiMethods
3
+ HTTP_REGEX = %r{^https?://}
4
+
3
5
  def teams(params = nil, headers = nil)
4
6
  send_get("/v1/teams", params, headers)
5
7
  end
@@ -52,6 +54,33 @@ module Esa
52
54
  send_delete("/v1/teams/#{current_team!}/comments/#{comment_id}", params, headers)
53
55
  end
54
56
 
57
+ class PathStringIO < StringIO
58
+ attr_accessor :path
59
+
60
+ def initialize(*args)
61
+ super(*args[1..-1])
62
+ @path = args[0]
63
+ end
64
+ end
65
+
66
+ # beta
67
+ def upload_attachment(path_or_file_or_url, params = {}, headers = nil)
68
+ file = file_from(path_or_file_or_url)
69
+ setup_params_for_upload(params, file)
70
+
71
+ response = send_post("/v1/teams/#{current_team!}/attachments/policies", params, headers)
72
+ return response unless response.status == 200
73
+
74
+ attachment = response.body['attachment']
75
+ form_data = response.body['form'].merge(file: Faraday::UploadIO.new(file, params[:type]))
76
+
77
+ s3_response = send_s3_request(:post, attachment['endpoint'], form_data)
78
+ return s3_response unless s3_response.status == 204
79
+
80
+ response.body.delete('form')
81
+ response
82
+ end
83
+
55
84
  private
56
85
 
57
86
  def wrap(params, envelope)
@@ -60,5 +89,34 @@ module Esa
60
89
  return params if params.has_key?(envelope.to_sym) || params.has_key?(envelope.to_s)
61
90
  { envelope => params }
62
91
  end
92
+
93
+ def content_type_from_file(file)
94
+ require 'mime/types'
95
+ if mime_type = MIME::Types.type_for(file.path).first
96
+ mime_type.content_type
97
+ end
98
+ rescue LoadError
99
+ msg = 'Please pass content_type or install mime-types gem to guess content type from file'
100
+ raise MissingContentTypeErrork, msg
101
+ end
102
+
103
+ def file_from(path_or_file_or_url)
104
+ if path_or_file_or_url.respond_to?(:read)
105
+ path_or_file_or_url
106
+ elsif path_or_file_or_url.is_a?(String) && HTTP_REGEX.match(path_or_file_or_url)
107
+ remote_url = path_or_file_or_url
108
+ response = send_simple_request(:get, remote_url)
109
+ PathStringIO.new(File.basename(remote_url), response.body)
110
+ else
111
+ path = path_or_file_or_url
112
+ File.new(path, "r+b")
113
+ end
114
+ end
115
+
116
+ def setup_params_for_upload(params, file)
117
+ params[:type] = params.delete(:content_type) || content_type_from_file(file)
118
+ params[:size] = file.size
119
+ params[:name] = File.basename(file.path)
120
+ end
63
121
  end
64
122
  end
data/lib/esa/client.rb CHANGED
@@ -1,6 +1,6 @@
1
+ require 'esa/errors'
1
2
  require 'esa/api_methods'
2
3
  require "esa/response"
3
- require 'esa/errors'
4
4
 
5
5
  module Esa
6
6
  class Client
@@ -38,15 +38,38 @@ module Esa
38
38
  send_request(:delete, path, params, headers)
39
39
  end
40
40
 
41
- def send_request(method, path, params, headers)
42
- Esa::Response.new(connection.send(method, path, params, headers))
41
+ def send_request(method, path, params = nil, headers = nil)
42
+ Esa::Response.new(esa_connection.send(method, path, params, headers))
43
+ end
44
+
45
+ def send_s3_request(method, path, params = nil, headers = nil)
46
+ Esa::Response.new(s3_connection.send(method, path, params, headers))
47
+ end
48
+
49
+ def send_simple_request(method, path, params = nil, headers = nil)
50
+ Esa::Response.new(simple_connection.send(method, path, params, headers))
43
51
  end
44
52
 
45
- def connection
46
- @connection ||= Faraday.new(faraday_options) do |c|
47
- c.request :json
53
+ def esa_connection
54
+ @esa_connection ||= Faraday.new(faraday_options) do |c|
55
+ c.request :json
48
56
  c.response :json
49
- c.adapter Faraday.default_adapter
57
+ c.adapter Faraday.default_adapter
58
+ end
59
+ end
60
+
61
+ def s3_connection
62
+ @s3_connection ||= Faraday.new do |c|
63
+ c.request :multipart
64
+ c.request :url_encoded
65
+ c.response :xml
66
+ c.adapter Faraday.default_adapter
67
+ end
68
+ end
69
+
70
+ def simple_connection
71
+ @simple_connection ||= Faraday.new do |c|
72
+ c.adapter Faraday.default_adapter
50
73
  end
51
74
  end
52
75
 
@@ -55,7 +78,7 @@ module Esa
55
78
  def faraday_options
56
79
  {
57
80
  url: faraday_url,
58
- headers: faraday_headers,
81
+ headers: faraday_headers
59
82
  }
60
83
  end
61
84
 
data/lib/esa/errors.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Esa
2
2
  class EsaError < StandardError; end
3
3
  class TeamNotSpecifiedError < EsaError; end
4
+ class MissingContentTypeError < EsaError; end
4
5
  end
data/lib/esa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Esa
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esa
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - fukayatsu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-25 00:00:00.000000000 Z
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mime-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement