cloudsight 0.0.1 → 0.0.2

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: eb35b6bfc1b3ad85f8b22fbada36405a301de3a2
4
- data.tar.gz: 07ad0f0d8a70fd1e07538d75de7f200b2e523ca6
3
+ metadata.gz: bb8badd51b892500316434b647052103df2287be
4
+ data.tar.gz: 241c9348620a066bfb4d67003e82e814c45f4b91
5
5
  SHA512:
6
- metadata.gz: 8ae5d760ea12f5bddaa50a7fc75f79e34d31fb5fcd87ae3146c5ebce675b31e4e32ac58cb8d528137dc0b77f988a7f7d45eca4b4ac0c92a57fba599fde0645f3
7
- data.tar.gz: d00be6755a458a5ac22a47c3b6dd33770e644a1dfdbe4d75760f9da239980a3ce959818416c7de9d5313f36d29cb74afd8a27c2cd051f45b6db5bf9a86bb1306
6
+ metadata.gz: c7c2c691cf71ca220e3a15d1652b757d7ed7b8911ba821a80c9e83bd9d52e6f6f0141b898fa25b4792cab4045dd42c2dd4e6892efebaef69b89afc4d9ccd0fed
7
+ data.tar.gz: b7743e4646dc36a3684e88bf9fce2306126010ef4bde5d72c4ccc71e72d4ff08e7504e93ceebcbd5edd9127f6697a34a0284fd2aa443a33644b288d40ae0e37d
data/Gemfile CHANGED
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'rake'
3
4
  gem 'json'
4
5
  gem 'rest-client'
5
6
  gem 'simple_oauth'
data/Gemfile.lock CHANGED
@@ -3,6 +3,7 @@ GEM
3
3
  specs:
4
4
  json (1.8.1)
5
5
  mime-types (2.2)
6
+ rake (10.3.2)
6
7
  rest-client (1.6.7)
7
8
  mime-types (>= 1.16)
8
9
  simple_oauth (0.2.0)
@@ -12,5 +13,6 @@ PLATFORMS
12
13
 
13
14
  DEPENDENCIES
14
15
  json
16
+ rake
15
17
  rest-client
16
18
  simple_oauth
data/README.md CHANGED
@@ -3,6 +3,12 @@ cloudsight-ruby
3
3
 
4
4
  A simple CloudSight API Client
5
5
 
6
+ | Project | Gem Release |
7
+ |------------------------ | ----------------- |
8
+ | gem name | gem-release |
9
+ | version | [![Gem Version](https://badge.fury.io/rb/gem-release.png)](http://badge.fury.io/rb/gem-release) |
10
+ | continuous integration | [![Build Status](https://secure.travis-ci.org/cloudsight/cloudsight-ruby.png?branch=master)](https://travis-ci.org/cloudsight/cloudsight-ruby) |
11
+
6
12
  Installation
7
13
  ============
8
14
 
@@ -10,11 +16,18 @@ Installation
10
16
  $ gem install cloudsight
11
17
  ```
12
18
 
19
+ Install the `simple_oauth` gem to use with oauth options.
20
+
21
+ ```
22
+ $ gem install simple_oauth
23
+ ```
24
+
13
25
  Configuration
14
26
  =============
15
27
 
16
28
  ```ruby
17
29
  require 'rubygems'
30
+ require 'simple_oauth'
18
31
  require 'cloudsight'
19
32
 
20
33
  Cloudsight.oauth_options = {
@@ -23,6 +36,15 @@ Cloudsight.oauth_options = {
23
36
  }
24
37
  ```
25
38
 
39
+ or, using a single API key:
40
+
41
+ ```ruby
42
+ require 'rubygems'
43
+ require 'cloudsight'
44
+
45
+ Cloudsight.api_key = 'REPLACE WITH YOUR KEY'
46
+ ```
47
+
26
48
  Usage
27
49
  =====
28
50
 
data/cloudsight.gemspec CHANGED
@@ -6,7 +6,7 @@ require 'cloudsight/version'
6
6
  Gem::Specification.new do |s|
7
7
  s.name = 'cloudsight'
8
8
  s.version = Cloudsight::VERSION
9
- s.date = '2014-11-14'
9
+ s.date = '2015-11-05'
10
10
  s.summary = "CloudSight API Client"
11
11
  s.description = "A simple CloudSight API Client for Image Recognition"
12
12
  s.authors = ['Brad Folkens']
data/lib/cloudsight.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require 'rubygems'
2
2
  require 'rest-client'
3
- require 'simple_oauth'
3
+ begin
4
+ require 'simple_oauth'
5
+ rescue LoadError => err
6
+ # Tolerate not having this unless it's actually configured
7
+ end
4
8
  require 'json'
5
9
 
6
10
  module Cloudsight
@@ -8,21 +12,25 @@ module Cloudsight
8
12
 
9
13
  class << self
10
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 }
11
18
  @@oauth_options = val
19
+ end
12
20
 
21
+ def api_key=(val)
22
+ @@api_key = val
13
23
  RestClient.add_before_execution_proc do |req, params|
14
- if params[:payload]
15
- filtered_payload = params[:payload].dup
16
- filtered_payload.delete('image_request[image]')
17
- end
18
-
19
- oauth = SimpleOAuth::Header.new(params[:method], params[:url], filtered_payload, oauth_options)
20
- req.add_field 'Authorization', oauth.to_s
24
+ req.add_field 'Authorization', "CloudSight #{val}"
21
25
  end
22
26
  end
23
27
 
28
+ def api_key
29
+ @@api_key if defined?(@@api_key)
30
+ end
31
+
24
32
  def oauth_options
25
- @@oauth_options
33
+ @@oauth_options if defined?(@@oauth_options)
26
34
  end
27
35
 
28
36
  def base_url=(val)
@@ -34,8 +42,30 @@ module Cloudsight
34
42
  end
35
43
  end
36
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
+ retval = yield
59
+
60
+ RestClient.reset_before_execution_procs
61
+
62
+ return retval
63
+ end
64
+ end
65
+
37
66
  class Request
38
67
  def self.send(options = {})
68
+ raise RuntimeError.new("Need to define either oauth_options or api_key") unless Cloudsight.api_key || Cloudsight.oauth_options
39
69
  url = "#{Cloudsight::base_url}/image_requests"
40
70
 
41
71
  params = {}
@@ -51,7 +81,7 @@ module Cloudsight
51
81
  params['image_request[remote_image_url]'] = options[:url] if options.has_key?(:url)
52
82
  params['image_request[image]'] = options[:file] if options.has_key?(:file)
53
83
 
54
- response = RestClient.post(url, params)
84
+ response = Util.wrap_request { RestClient.post(url, params) }
55
85
  data = JSON.parse(response.body)
56
86
  raise ResponseException.new(data['error']) if data['error']
57
87
  raise UnexpectedResponseException.new(response.body) unless data['token']
@@ -64,7 +94,7 @@ module Cloudsight
64
94
  def self.get(token, options = {})
65
95
  url = "#{Cloudsight::base_url}/image_responses/#{token}"
66
96
 
67
- response = RestClient.get(url)
97
+ response = Util.wrap_request { RestClient.get(url) }
68
98
  data = JSON.parse(response.body)
69
99
  raise ResponseException.new(data['error']) if data['error']
70
100
  raise UnexpectedResponseException.new(response.body) unless data['status']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudsight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Folkens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-14 00:00:00.000000000 Z
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,8 +70,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  requirements: []
72
72
  rubyforge_project:
73
- rubygems_version: 2.2.2
73
+ rubygems_version: 2.4.7
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: CloudSight API Client
77
77
  test_files: []
78
+ has_rdoc: