tigre-client 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,14 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tigre-client (0.1.1)
4
+ tigre-client (0.1.2)
5
5
  curb (~> 0.7.12)
6
+ faraday (~> 0.6.0)
6
7
 
7
8
  GEM
8
9
  remote: http://rubygems.org/
9
10
  specs:
10
- curb (0.7.14)
11
+ addressable (2.2.4)
12
+ curb (0.7.15)
11
13
  diff-lcs (1.1.2)
14
+ faraday (0.6.0)
15
+ addressable (~> 2.2.4)
16
+ multipart-post (~> 1.1.0)
17
+ rack (< 2, >= 1.1.0)
18
+ multipart-post (1.1.0)
19
+ rack (1.2.2)
12
20
  rspec (2.5.0)
13
21
  rspec-core (~> 2.5.0)
14
22
  rspec-expectations (~> 2.5.0)
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+
3
+ module Faraday
4
+ # @private
5
+ class Request::MultipartWithFile < Faraday::Middleware
6
+ def call(env)
7
+ if env[:body].is_a?(Hash)
8
+
9
+ env[:body].each do |key, value|
10
+ if value.is_a?(File)
11
+ env[:body][key] = Faraday::UploadIO.new(value, mime_type(value), value.path)
12
+ end
13
+ end
14
+ end
15
+
16
+ @app.call(env)
17
+ end
18
+
19
+ private
20
+
21
+ def mime_type(file)
22
+ case file.path
23
+ when /\.jpe?g/i then 'image/jpeg'
24
+ when /\.gif$/i then 'image/gif'
25
+ when /\.png$/i then 'image/png'
26
+ else 'application/octet-stream'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -5,17 +5,8 @@ module Tigre
5
5
  # required params
6
6
  # sha256 - String
7
7
  def self.for_sha(sha256)
8
-
9
- if sha256 == ''
10
- raise ArguementError, "Missing sha256 parameter"
11
- end
12
-
13
- c = Curl::Easy.new("#{Tigre.endpoint}/urls/#{sha256}/occurrences")
14
- c.multipart_form_post = false
15
- c.headers["API-TOKEN"] = Tigre.token
16
-
17
- c.perform
18
- return [c.response_code, c.body_str]
8
+ raise ArguementError, "Missing sha256 parameter" if sha256 == ''
9
+ Tigre.get_connection("/urls/#{sha256}/occurrences")
19
10
  end
20
11
 
21
12
  # return an individual occurrence for a given sha256 URL
@@ -23,16 +14,8 @@ module Tigre
23
14
  # sha256 - String
24
15
  # occurrence_id - String
25
16
  def self.get(sha256, occurrence_id)
26
- if sha256 == '' || occurrence_id == ''
27
- raise ArguementError, "Missing sha256 or occurrence_id parameter"
28
- end
29
-
30
- c = Curl::Easy.new("#{Tigre.endpoint}/urls/#{sha256}/occurrences/#{occurrence_id}")
31
- c.multipart_form_post = false
32
- c.headers["API-TOKEN"] = Tigre.token
33
-
34
- c.perform
35
- return [c.response_code, c.body_str]
17
+ raise ArguementError, "Missing sha256 or occurrence_id parameter" if (sha256 == '' || occurrence_id == '')
18
+ Tigre.get_connection("/urls/#{sha256}/occurrences/#{occurrence_id}")
36
19
  end
37
20
 
38
21
  end
@@ -12,43 +12,27 @@ module Tigre
12
12
  raise ArguementError, "File_location or md5 parameter cannot be empty"
13
13
  end
14
14
 
15
- c = Curl::Easy.new("#{Tigre.endpoint}/samples")
16
- c.multipart_form_post = true
17
- c.headers["API-TOKEN"] = Tigre.token
18
-
19
- post_data = options.map {|k, v| Curl::PostField.content(k, v.to_s)}
20
- post_data << Curl::PostField.file("file", file_location)
21
- post_data << Curl::PostField.content("md5", md5)
22
-
23
- c.http_post(*post_data)
24
- return [c.response_code, c.body_str]
15
+ post_data = {:file => File.new(file_location), :md5 => md5}
16
+ Tigre.post_file_connection('/samples', post_data.merge(options))
25
17
  end
26
18
 
27
19
  # required params
28
20
  # md5 - String
29
21
  def self.md5(md5)
30
- c = Curl::Easy.new("#{Tigre.endpoint}/samples/md5/#{md5}")
31
- c.headers["API-TOKEN"] = Tigre.token
32
- c.perform
33
- return [c.response_code, c.body_str]
22
+ Tigre.get_connection("/samples/md5/#{md5}")
34
23
  end
35
24
 
36
25
  # required params
37
26
  # sha1 - String
38
27
  def self.sha1(sha1)
39
- c = Curl::Easy.new("#{Tigre.endpoint}/samples/sha1/#{sha1}")
40
- c.headers["API-TOKEN"] = Tigre.token
41
- c.perform
42
- return [c.response_code, c.body_str]
28
+ Tigre.get_connection("/samples/sha1/#{sha1}")
43
29
  end
44
30
 
45
31
  # required params
46
32
  # sha256 - String
47
33
  def self.sha256(sha256)
48
- c = Curl::Easy.new("#{Tigre.endpoint}/samples/sha256/#{sha256}")
49
- c.headers["API-TOKEN"] = Tigre.token
50
- c.perform
51
- return [c.response_code, c.body_str]
34
+ Tigre.get_connection("/samples/sha256/#{sha256}")
52
35
  end
36
+
53
37
  end
54
38
  end
@@ -9,30 +9,34 @@ module Tigre
9
9
  # options - Hash
10
10
  # :shareable - Boolean, :default => false
11
11
  def self.post(url, options={})
12
-
13
12
  if url == ''
14
13
  raise ArguementError, "Missing url parameter"
15
14
  end
16
-
17
15
  unless options[:source_name]
18
16
  raise ArguementError, "Missing source_name parameter"
19
17
  end
20
18
 
21
19
  # prepare post data
22
- fields_hash = {
20
+ post_data = {
23
21
  "url[original_url]" => url,
24
22
  "url[source_name]" => options[:source_name],
25
23
  "url[shareable]" => options[:shareable]
26
24
  }
27
- post_data = fields_hash.map { |k, v| Curl::PostField.content(k, v.to_s) }
28
-
29
- c = Curl::Easy.new("#{Tigre.endpoint}/urls")
30
- c.multipart_form_post = false
31
- c.headers["API-TOKEN"] = Tigre.token
25
+ Tigre.post_connection('/urls', post_data)
26
+ end
32
27
 
33
- c.http_post(post_data)
28
+ # required
29
+ # sha256 - String
30
+ # options - Hash
31
+ # :source_name - String
32
+ def self.update(sha256, params_hash)
33
+ if sha256 == ''
34
+ raise ArguementError, "Missing url parameter"
35
+ end
34
36
 
35
- return [c.response_code, c.body_str]
37
+ update_data = params_hash.map { |k, v| {"url[#{k.to_s}]" => v.to_s} }
38
+
39
+ Tigre.put_connection("/urls/#{sha256}", update_data )
36
40
  end
37
41
 
38
42
  # required params
@@ -41,36 +45,28 @@ module Tigre
41
45
  if sha256 == ''
42
46
  raise ArguementError, "Missing sha256 parameter"
43
47
  end
44
-
45
- c = Curl::Easy.new("#{Tigre.endpoint}/urls/#{sha256}")
46
- c.headers["API-TOKEN"] = Tigre.token
47
- c.perform
48
- return [c.response_code, c.body_str]
48
+ Tigre.get_connection("/urls/#{sha256}")
49
49
  end
50
-
50
+
51
51
  # required params
52
52
  # sha256 - String
53
53
  def self.get_md5(md5)
54
54
  if md5 == ''
55
55
  raise ArguementError, "Missing md5 parameter"
56
56
  end
57
-
58
- c = Curl::Easy.new("#{Tigre.endpoint}/urls/md5/#{md5}")
59
- c.headers["API-TOKEN"] = Tigre.token
60
- c.perform
61
- return [c.response_code, c.body_str]
57
+ Tigre.get_connection("/urls/md5/#{md5}")
62
58
  end
63
-
59
+
60
+ # optional params
61
+ # options - Hash
62
+ # :page - Integer, :default => 0
63
+ # :per - Integer, :default => 50
64
64
  def self.index(options={})
65
65
  options[:page] ||= 0
66
66
  options[:per] ||= 50
67
-
68
- c = Curl::Easy.new("#{Tigre.endpoint}/urls?page=#{options[:page]}&per=#{options[:per]}")
69
- c.headers["API-TOKEN"] = Tigre.token
70
- c.perform
71
- return [c.response_code, c.body_str]
67
+ Tigre.get_connection("/urls?page=#{options[:page]}&per=#{options[:per]}")
72
68
  end
73
-
69
+
74
70
  # required params
75
71
  # options - Hash
76
72
  # :after => Ruby DateTime object OR Integer (epoch time)
@@ -89,11 +85,7 @@ module Tigre
89
85
 
90
86
  options[:page] ||= 0
91
87
  options[:per] ||= 50
92
-
93
- c = Curl::Easy.new("#{Tigre.endpoint}/urls/daterange?after=#{options[:after]}&before=#{options[:before]}&page=#{options[:page]}&per=#{options[:per]}")
94
- c.headers["API-TOKEN"] = Tigre.token
95
- c.perform
96
- return [c.response_code, c.body_str]
88
+ Tigre.get_connection("/urls/daterange?after=#{options[:after]}&before=#{options[:before]}&page=#{options[:page]}&per=#{options[:per]}")
97
89
  end
98
90
 
99
91
  # required params
@@ -116,13 +108,8 @@ module Tigre
116
108
 
117
109
  options[:page] ||= 0
118
110
  options[:per] ||= 50
119
-
120
- c = Curl::Easy.new("#{Tigre.endpoint}/urls/domains?domain=#{domain}&after=#{options[:after]}&before=#{options[:before]}&page=#{options[:page]}&per=#{options[:per]}")
121
- c.headers["API-TOKEN"] = Tigre.token
122
- c.perform
123
- return [c.response_code, c.body_str]
111
+ Tigre.get_connection("/urls/domains?domain=#{domain}&after=#{options[:after]}&before=#{options[:before]}&page=#{options[:page]}&per=#{options[:per]}")
124
112
  end
125
-
126
113
  end
127
114
 
128
115
  end
@@ -1,5 +1,5 @@
1
1
  module Tigre
2
2
  module Client
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
data/lib/tigre-client.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  require 'curb'
2
+ require 'faraday'
3
+
4
+ require 'tigre-client/multipart_with_file'
5
+
2
6
  require 'tigre-client/occurrence'
3
7
  require 'tigre-client/sample'
4
8
  require 'tigre-client/url'
@@ -6,12 +10,74 @@ require 'tigre-client/url'
6
10
  module Tigre
7
11
 
8
12
  class << self
9
- attr_accessor :token, :endpoint
13
+ attr_accessor :token, :server, :endpoint, :conn
10
14
 
11
15
  def configure
12
16
  yield self
13
17
  true
14
18
  end
19
+
20
+ def build_get_connection
21
+ @conn = Faraday.new(:url => "#{Tigre.server}") do |builder|
22
+ builder.adapter :net_http
23
+ end
24
+ end
25
+
26
+ # TODO: do we need two build_post_put ? can we just put
27
+ # the multipart in the post_put?
28
+ def build_post_put_connection
29
+ @conn = Faraday.new(:url => "#{Tigre.server}") do |builder|
30
+ # builder.request :multipart
31
+ builder.request :url_encoded
32
+ builder.adapter :net_http
33
+ end
34
+ end
35
+
36
+ def build_file_post_put_connection
37
+ @conn = Faraday.new(:url => "#{Tigre.server}") do |builder|
38
+ builder.use Faraday::Request::MultipartWithFile
39
+ builder.use Faraday::Request::Multipart
40
+ builder.use Faraday::Request::UrlEncoded
41
+ builder.adapter :net_http
42
+ end
43
+ end
44
+
45
+ def get_connection(uri_string)
46
+ build_get_connection
47
+ response = Tigre.conn.get(Tigre.endpoint + uri_string, 'API-TOKEN' => Tigre.token)
48
+ [response.status, response.body]
49
+ end
50
+
51
+ def post_connection(uri_string, data)
52
+ build_post_put_connection
53
+ response = Tigre.conn.post do |req|
54
+ req.url Tigre.endpoint + uri_string
55
+ req.headers['API-TOKEN'] = Tigre.token
56
+ req.body = data
57
+ end
58
+ [response.status, response.body]
59
+ end
60
+
61
+ def post_file_connection(uri_string, data)
62
+ build_file_post_put_connection
63
+ response = Tigre.conn.post do |req|
64
+ req.url Tigre.endpoint + uri_string
65
+ req.headers['API-TOKEN'] = Tigre.token
66
+ req.body = data
67
+ end
68
+ [response.status, response.body]
69
+ end
70
+
71
+ def put_connection(uri_string, data)
72
+ build_post_put_connection
73
+ response = Tigre.conn.put do |req|
74
+ req.url Tigre.endpoint + uri_string
75
+ req.headers['API-TOKEN'] = Tigre.token
76
+ req.body = data
77
+ end
78
+ [response.status, response.body]
79
+ end
80
+
15
81
  end
16
82
 
17
83
  end
data/tigre-client.gemspec CHANGED
@@ -24,4 +24,6 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency "rspec"
25
25
 
26
26
  s.add_dependency('curb', '~> 0.7.12')
27
+ s.add_dependency('faraday', '~> 0.6.0')
28
+
27
29
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: tigre-client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Marcio Castilho
@@ -36,6 +36,17 @@ dependencies:
36
36
  version: 0.7.12
37
37
  type: :runtime
38
38
  version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: faraday
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.0
48
+ type: :runtime
49
+ version_requirements: *id003
39
50
  description: Tigre Client API Library
40
51
  email:
41
52
  - marcioc@sunbelt-software.com
@@ -53,6 +64,7 @@ files:
53
64
  - Gemfile.lock
54
65
  - Rakefile
55
66
  - lib/tigre-client.rb
67
+ - lib/tigre-client/multipart_with_file.rb
56
68
  - lib/tigre-client/occurrence.rb
57
69
  - lib/tigre-client/sample.rb
58
70
  - lib/tigre-client/url.rb