kraken-io 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/kraken-io.rb +74 -29
  3. metadata +50 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 927703fd46ec662ea64578cecb8589dfde21d68f
4
- data.tar.gz: 7d7bf33a13ce67a99a013dd5b2c35a199abfeacc
3
+ metadata.gz: dcf2758e91edc0914df8ac3b0612094a0acd7afa
4
+ data.tar.gz: 2d86b7c78d74482902a62682c6c7964c18c32a1d
5
5
  SHA512:
6
- metadata.gz: 7bbb72eac59df48bde082e7d4474ea0940ef2d785f311a256849ac8f35af81ae917901a68e8f6e186cdc3dfe63ce83e5392047b6fe7bee01b8e536722d7bbccd
7
- data.tar.gz: 07107083986dd2beb3244d90b03a0457a103055258effbd81f84e6917298c040aea04362c6384693b737a4daf8a918eccf668f1a8d4e0a409ff8472b9708147a
6
+ metadata.gz: 1c58c080015aca348f142087ec49b33aa138fc912be1820e64993c22887f8e26c5db84d2485609accb458bf643a8cad62fc9c1ad9af8f70958f108456afde0fe
7
+ data.tar.gz: f55daf3c1f69dece0381e434089160141fe3c60f25c6c71b933ad28bb511fd3b818b5103c50dd035eb9cdcb409526561e1f8d3283808c7895293ffb026430527
@@ -1,51 +1,96 @@
1
1
  require 'json'
2
2
  require 'httparty'
3
- require 'rest-client'
4
- require 'net/http/post/multipart'
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'active_support/core_ext/hash'
5
+ require 'thread'
6
+
7
+ require 'kraken-io/http_multi_part'
8
+ require 'kraken-io/response'
5
9
 
6
10
  module Kraken
7
11
  class API
8
12
  include HTTParty
13
+ extend HTTPMultiPart
14
+
15
+ base_uri 'https://api.kraken.io/v1'
9
16
 
10
17
  attr_accessor :api_key, :api_secret
11
18
 
12
- def initialize(api_key = '', api_secret = '')
13
- @api_key = api_key
14
- @api_secret = api_secret
19
+ def initialize(options = {})
20
+ @api_key = options.fetch(:api_key)
21
+ @api_secret = options.fetch(:api_secret)
15
22
  end
16
23
 
17
- def url(params = {})
18
- params.merge!({
19
- 'auth' => {
20
- 'api_key' => @api_key,
21
- 'api_secret' => @api_secret
22
- }
23
- })
24
+ def async
25
+ @async = true
26
+ self
27
+ end
24
28
 
25
- self.class.post('https://api.kraken.io/v1/url', {:body => JSON.generate(params)})
29
+ def sync
30
+ @async = false
31
+ self
26
32
  end
27
33
 
28
- def upload(params = {})
29
- params.merge!({
30
- 'auth' => {
31
- 'api_key' => @api_key,
32
- 'api_secret' => @api_secret
33
- }
34
- })
34
+ def url(url, params = {})
35
+ params = normalized_params(params).merge!(auth_hash)
36
+ params[:url] = url
37
+ call_kraken do
38
+ res = self.class.post('/url', body: params.to_json)
39
+ res = Kraken::Response.new(res)
40
+ yield res if block_given? or return res
41
+ end
42
+ end
35
43
 
36
- url = URI.parse("https://api.kraken.io/v1/upload")
44
+ def callback_url(url)
45
+ @callback_url = url
46
+ self
47
+ end
37
48
 
38
- File.open(params['file']) do |file|
39
- params.delete('file')
49
+ def upload(file_name, params = {})
50
+ params = normalized_params(params).merge!(auth_hash)
51
+ call_kraken do
52
+ res = self.class.multipart_post('/upload', file: file_name, body: params.to_json)
53
+ res = Kraken::Response.new(res)
54
+ yield res if block_given? or return res
55
+ end
56
+ end
40
57
 
41
- req = Net::HTTP::Post::Multipart.new url.path, "body" => JSON.generate(params), "file" => UploadIO.new(file, 'logotyp.png')
58
+ private
59
+ def call_kraken(&block)
60
+ if @async
61
+ call_async(&block)
62
+ else
63
+ yield
64
+ end
65
+ end
66
+
67
+ def call_async(&block)
68
+ Thread.abort_on_exception = false
69
+ Thread.new do |t|
70
+ block.call
71
+ end
72
+ nil
73
+ end
42
74
 
43
- https = Net::HTTP.new(url.host, url.port)
44
- https.use_ssl = true
75
+ def normalized_params(params)
76
+ params = params.with_indifferent_access
45
77
 
46
- res = https.start() {|conn| conn.request(req)}
47
- response = JSON.parse(res.body)
78
+ if params.keys.include?(:callback_url) || @callback_url
79
+ params[:callback_url] = @callback_url
80
+ else
81
+ params[:wait] = true
48
82
  end
83
+
84
+ params
85
+ end
86
+
87
+ def auth_hash
88
+ {
89
+ auth: {
90
+ api_key: api_key,
91
+ api_secret: api_secret
92
+ }
93
+ }
49
94
  end
50
95
  end
51
- end
96
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kraken-io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Przemek Matylla
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-01 00:00:00.000000000 Z
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: httmultiparty
42
+ name: multipart-post
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '>='
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: multipart-post
56
+ name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '>='
@@ -67,13 +67,55 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rest-client
70
+ name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
- type: :runtime
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.17'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.17'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
77
119
  prerelease: false
78
120
  version_requirements: !ruby/object:Gem::Requirement
79
121
  requirements:
@@ -81,7 +123,7 @@ dependencies:
81
123
  - !ruby/object:Gem::Version
82
124
  version: '0'
83
125
  description: With this gem you can plug into the power and speed of Kraken.io Image
84
- Optimizer. http://kraken.io/
126
+ Optimizer. https://kraken.io/
85
127
  email:
86
128
  - przemek@matylla.pl
87
129
  executables: []
@@ -109,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
151
  version: '0'
110
152
  requirements: []
111
153
  rubyforge_project:
112
- rubygems_version: 2.0.3
154
+ rubygems_version: 2.0.14
113
155
  signing_key:
114
156
  specification_version: 4
115
157
  summary: Ruby gem for interacting with Kraken.io API