cloudsight 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cloudsight.rb +131 -130
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fd9cb63d16322fabb673abe3da859ef57382cb4
|
4
|
+
data.tar.gz: c83cf57f6ebc7a618b858b602abc234a56f166a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb0623535da6c8e2657172f2a57f96d54c572dfec4e877d7256b906f8a94fdb0d17aa271231826b7f6c2184ea829b715a2eacae47f0027cc662d7df887fb295c
|
7
|
+
data.tar.gz: 12c945044957ef0c4e8fa2dc7833e6c0050be84537549b12deaa0548cc9f979aee2510f028c9a8b4fcfbd7db5d0305f70cd0c179376e717b31dc1213e7731ae3
|
data/lib/cloudsight.rb
CHANGED
@@ -1,139 +1,140 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rest-client'
|
3
3
|
begin
|
4
|
-
|
4
|
+
require 'simple_oauth'
|
5
5
|
rescue LoadError => err
|
6
|
-
|
6
|
+
# Tolerate not having this unless it's actually configured
|
7
7
|
end
|
8
8
|
require 'json'
|
9
9
|
|
10
10
|
module Cloudsight
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
11
|
+
BASE_URL = 'https://api.cloudsight.ai'
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def oauth_options=(val)
|
15
|
+
raise RuntimeError.new("Could not load the simple_oauth gem. Install it with `gem install simple_oauth`.") unless defined?(SimpleOAuth::Header)
|
16
|
+
|
17
|
+
val = val.inject({}) {|memo, (k, v)| memo[k.to_sym] = v; memo }
|
18
|
+
@@oauth_options = val
|
19
|
+
end
|
20
|
+
|
21
|
+
def api_key=(val)
|
22
|
+
@@api_key = val
|
23
|
+
end
|
24
|
+
|
25
|
+
def api_key
|
26
|
+
@@api_key if defined?(@@api_key)
|
27
|
+
end
|
28
|
+
|
29
|
+
def oauth_options
|
30
|
+
@@oauth_options if defined?(@@oauth_options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def base_url=(val)
|
34
|
+
@@base_url = val
|
35
|
+
end
|
36
|
+
|
37
|
+
def base_url
|
38
|
+
@@base_url ||= BASE_URL
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Util
|
43
|
+
def self.wrap_request &block
|
44
|
+
RestClient.reset_before_execution_procs
|
45
|
+
RestClient.add_before_execution_proc do |req, params|
|
46
|
+
if params[:payload]
|
47
|
+
filtered_payload = params[:payload].dup
|
48
|
+
filtered_payload.delete('image_request[image]')
|
49
|
+
end
|
50
|
+
|
51
|
+
if Cloudsight.api_key
|
52
|
+
req.add_field 'Authorization', "CloudSight #{Cloudsight.api_key}"
|
53
|
+
else
|
54
|
+
oauth = SimpleOAuth::Header.new(params[:method], params[:url], filtered_payload, Cloudsight.oauth_options || {})
|
55
|
+
req.add_field 'Authorization', oauth.to_s
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
begin
|
60
|
+
retval = yield
|
61
|
+
rescue RestClient::Exception => e
|
62
|
+
retval = e.response
|
63
|
+
end
|
64
|
+
|
65
|
+
RestClient.reset_before_execution_procs
|
66
|
+
|
67
|
+
return retval
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Request
|
72
|
+
def self.send(options = {})
|
73
|
+
raise RuntimeError.new("Need to define either oauth_options or api_key") unless Cloudsight.api_key || Cloudsight.oauth_options
|
74
|
+
url = "#{Cloudsight::base_url}/image_requests"
|
75
|
+
|
76
|
+
params = {}
|
77
|
+
[:locale, :language, :latitude, :longitude, :altitude, :device_id, :ttl].each do |attr|
|
78
|
+
params["image_request[#{attr}]"] = options[attr] if options.has_key?(attr)
|
79
|
+
end
|
80
|
+
|
81
|
+
if options[:focus]
|
82
|
+
params['focus[x]'] = options[:focus][:x]
|
83
|
+
params['focus[y]'] = options[:focus][:y]
|
84
|
+
end
|
85
|
+
|
86
|
+
params['image_request[remote_image_url]'] = options[:url] if options.has_key?(:url)
|
87
|
+
params['image_request[image]'] = options[:file] if options.has_key?(:file)
|
88
|
+
|
89
|
+
response = Util.wrap_request { RestClient.post(url, params) }
|
90
|
+
data = JSON.parse(response.body)
|
91
|
+
raise ResponseException.new(data['error']) if data['error']
|
92
|
+
raise UnexpectedResponseException.new(response.body) unless data['token']
|
93
|
+
|
94
|
+
data
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.repost(token, options = {})
|
98
|
+
url = "#{Cloudsight::base_url}/image_requests/#{token}/repost"
|
99
|
+
|
100
|
+
response = Util.wrap_request { RestClient.post(url, options) }
|
101
|
+
return true if response.code == 200 and response.body.to_s.strip.empty?
|
102
|
+
|
103
|
+
data = JSON.parse(response.body)
|
104
|
+
raise ResponseException.new(data['error']) if data['error']
|
105
|
+
raise UnexpectedResponseException.new(response.body) unless data['token']
|
106
|
+
|
107
|
+
data
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
class Response
|
112
|
+
def self.get(token, options = {})
|
113
|
+
url = "#{Cloudsight::base_url}/image_responses/#{token}"
|
114
|
+
|
115
|
+
response = Util.wrap_request { RestClient.get(url) }
|
116
|
+
data = JSON.parse(response.body)
|
117
|
+
raise ResponseException.new(data['error']) if data['error']
|
118
|
+
raise UnexpectedResponseException.new(response.body) unless data['status']
|
119
|
+
|
120
|
+
data
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.retrieve(token, options = {})
|
124
|
+
options = { poll_wait: 1 }.merge(options)
|
125
|
+
|
126
|
+
data = nil
|
127
|
+
loop do
|
128
|
+
sleep options[:poll_wait]
|
129
|
+
data = Cloudsight::Response.get(token, options)
|
130
|
+
yield data if block_given?
|
131
|
+
break if data['status'] != 'not completed' and data['status'] != 'in progress'
|
132
|
+
end
|
133
|
+
|
134
|
+
data
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class ResponseException < Exception; end
|
139
|
+
class UnexpectedResponseException < Exception; end
|
139
140
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloudsight
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brad Folkens
|
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
100
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.6.12
|
102
102
|
signing_key:
|
103
103
|
specification_version: 4
|
104
104
|
summary: CloudSight API Client
|