cloudsight 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cloudsight.rb +131 -130
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88263f76dbd9b938a6196193e5f9d2695ad28c29
4
- data.tar.gz: 195421c20284a46e5e8967026958f8a779a4e377
3
+ metadata.gz: 2fd9cb63d16322fabb673abe3da859ef57382cb4
4
+ data.tar.gz: c83cf57f6ebc7a618b858b602abc234a56f166a9
5
5
  SHA512:
6
- metadata.gz: a0526368cef6a2cd9c8cfdb4b4c3f9ae99f1e7628198b051bce03dcbe1db6dbedc0570c606861bc7886983760310f362152fe77a07e77595c09c31e160b238f5
7
- data.tar.gz: 5f841aa7c29d5da09d398119a1af5402d780b3362b348dd6433ea5e2a47849812ec6c1ae1ee83f5e29e849ef9c23274abf8121510fbc985fb0baa9b88de462ad
6
+ metadata.gz: eb0623535da6c8e2657172f2a57f96d54c572dfec4e877d7256b906f8a94fdb0d17aa271231826b7f6c2184ea829b715a2eacae47f0027cc662d7df887fb295c
7
+ data.tar.gz: 12c945044957ef0c4e8fa2dc7833e6c0050be84537549b12deaa0548cc9f979aee2510f028c9a8b4fcfbd7db5d0305f70cd0c179376e717b31dc1213e7731ae3
@@ -1,139 +1,140 @@
1
1
  require 'rubygems'
2
2
  require 'rest-client'
3
3
  begin
4
- require 'simple_oauth'
4
+ require 'simple_oauth'
5
5
  rescue LoadError => err
6
- # Tolerate not having this unless it's actually configured
6
+ # Tolerate not having this unless it's actually configured
7
7
  end
8
8
  require 'json'
9
9
 
10
10
  module Cloudsight
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
- RestClient.add_before_execution_proc do |req, params|
24
- req.add_field 'Authorization', "CloudSight #{val}"
25
- end
26
- end
27
-
28
- def api_key
29
- @@api_key if defined?(@@api_key)
30
- end
31
-
32
- def oauth_options
33
- @@oauth_options if defined?(@@oauth_options)
34
- end
35
-
36
- def base_url=(val)
37
- @@base_url = val
38
- end
39
-
40
- def base_url
41
- @@base_url ||= BASE_URL
42
- end
43
- end
44
-
45
- class Util
46
- def self.wrap_request &block
47
- RestClient.reset_before_execution_procs
48
- RestClient.add_before_execution_proc do |req, params|
49
- if params[:payload]
50
- filtered_payload = params[:payload].dup
51
- filtered_payload.delete('image_request[image]')
52
- end
53
-
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
-
58
- begin
59
- retval = yield
60
- rescue RestClient::Exception => e
61
- retval = e.response
62
- end
63
-
64
- RestClient.reset_before_execution_procs
65
-
66
- return retval
67
- end
68
- end
69
-
70
- class Request
71
- def self.send(options = {})
72
- raise RuntimeError.new("Need to define either oauth_options or api_key") unless Cloudsight.api_key || Cloudsight.oauth_options
73
- url = "#{Cloudsight::base_url}/image_requests"
74
-
75
- params = {}
76
- [:locale, :language, :latitude, :longitude, :altitude, :device_id, :ttl].each do |attr|
77
- params["image_request[#{attr}]"] = options[attr] if options.has_key?(attr)
78
- end
79
-
80
- if options[:focus]
81
- params['focus[x]'] = options[:focus][:x]
82
- params['focus[y]'] = options[:focus][:y]
83
- end
84
-
85
- params['image_request[remote_image_url]'] = options[:url] if options.has_key?(:url)
86
- params['image_request[image]'] = options[:file] if options.has_key?(:file)
87
-
88
- response = Util.wrap_request { RestClient.post(url, params) }
89
- data = JSON.parse(response.body)
90
- raise ResponseException.new(data['error']) if data['error']
91
- raise UnexpectedResponseException.new(response.body) unless data['token']
92
-
93
- data
94
- end
95
-
96
- def self.repost(token, options = {})
97
- url = "#{Cloudsight::base_url}/image_requests/#{token}/repost"
98
-
99
- response = Util.wrap_request { RestClient.post(url, options) }
100
- return true if response.code == 200 and response.body.to_s.strip.empty?
101
-
102
- data = JSON.parse(response.body)
103
- raise ResponseException.new(data['error']) if data['error']
104
- raise UnexpectedResponseException.new(response.body) unless data['token']
105
-
106
- data
107
- end
108
- end
109
-
110
- class Response
111
- def self.get(token, options = {})
112
- url = "#{Cloudsight::base_url}/image_responses/#{token}"
113
-
114
- response = Util.wrap_request { RestClient.get(url) }
115
- data = JSON.parse(response.body)
116
- raise ResponseException.new(data['error']) if data['error']
117
- raise UnexpectedResponseException.new(response.body) unless data['status']
118
-
119
- data
120
- end
121
-
122
- def self.retrieve(token, options = {})
123
- options = { poll_wait: 1 }.merge(options)
124
-
125
- data = nil
126
- loop do
127
- sleep options[:poll_wait]
128
- data = Cloudsight::Response.get(token, options)
129
- yield data if block_given?
130
- break if data['status'] != 'not completed' and data['status'] != 'in progress'
131
- end
132
-
133
- data
134
- end
135
- end
136
-
137
- class ResponseException < Exception; end
138
- class UnexpectedResponseException < Exception; end
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.3
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.4.8
101
+ rubygems_version: 2.6.12
102
102
  signing_key:
103
103
  specification_version: 4
104
104
  summary: CloudSight API Client