Skip to content

Commit

Permalink
Merge pull request #8 from cloudsight/use-v1-api
Browse files Browse the repository at this point in the history
Use v1 api
  • Loading branch information
mccallumjack authored Dec 11, 2017
2 parents cdee965 + 5c9edde commit ed6940a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 46 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ Usage
Send the image request using a file:

```ruby
requestData = Cloudsight::Request.send(locale: 'en', file: File.open('image.jpg'))
request_data = Cloudsight::Request.send(locale: 'en', file: File.open('image.jpg'))
```

Or, you can send the image request using a URL:

```ruby
requestData = Cloudsight::Request.send(locale: 'en', url: 'http://www.google.com/images/srpr/logo11w.png')
request_data = Cloudsight::Request.send(locale: 'en', url: 'http://www.google.com/images/srpr/logo11w.png')
```

Then, use the token to retrieve the response:

```ruby
responseData = Cloudsight::Response.get(requestData['token'])
response_data = Cloudsight::Response.get(request_data['token'])
```

You can also use the `retrieve` method which will poll for the response for you:

```ruby
Cloudsight::Response.retrieve(requestData['token']) do |responseData|
p responseData
Cloudsight::Response.retrieve(request_data['token']) do |response_data|
p response_data
end
```
11 changes: 10 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
require "bundler/gem_tasks"

task :default do
begin
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |t|
t.fail_on_error = false
end

task :default => :spec
rescue LoadError
# no rspec available
end
16 changes: 8 additions & 8 deletions lib/cloudsight/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Request
class << self
def send(options = {})
raise RuntimeError.new("Need to define either oauth_options or api_key") unless Cloudsight.api_key || Cloudsight.oauth_options
url = "#{Cloudsight::base_url}/image_requests"
url = "#{Cloudsight::base_url}/v1/images"

params = construct_params(options)
response = Api.post(url, params)
Expand All @@ -15,7 +15,7 @@ def send(options = {})
end

def repost(token, options = {})
url = "#{Cloudsight::base_url}/image_requests/#{token}/repost"
url = "#{Cloudsight::base_url}/v1/images/#{token}/repost"

response = Api.post(url, options)
return true if response.code == 200 and response.body.to_s.strip.empty?
Expand All @@ -29,17 +29,17 @@ def repost(token, options = {})

def construct_params(options)
params = {}
[:locale, :language, :latitude, :longitude, :altitude, :device_id, :ttl].each do |attr|
params["image_request[#{attr}]"] = options[attr] if options.has_key?(attr)
[:locale, :language, :latitude, :longitude, :altitude, :device_id, :ttl, :focus_x, :focus_y].each do |attr|
params[attr.to_s] = options[attr] if options.has_key?(attr)
end

if options[:focus]
params['focus[x]'] = options[:focus][:x]
params['focus[y]'] = options[:focus][:y]
params['focus_x'] = options[:focus][:x]
params['focus_y'] = options[:focus][:y]
end

params['image_request[remote_image_url]'] = options[:url] if options.has_key?(:url)
params['image_request[image]'] = options[:file] if options.has_key?(:file)
params['remote_image_url'] = options[:url] if options.has_key?(:url)
params['image'] = options[:file] if options.has_key?(:file)
params
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cloudsight/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Cloudsight
class Response
class << self
def get(token, options = {})
url = "#{Cloudsight::base_url}/image_responses/#{token}"
url = "#{Cloudsight::base_url}/v1/images/#{token}"

response = Api.get(url)
data = JSON.parse(response.body)
Expand Down
2 changes: 1 addition & 1 deletion lib/cloudsight/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cloudsight
VERSION = "0.0.9.1"
VERSION = "0.1.0"
end
48 changes: 24 additions & 24 deletions spec/cloudsight/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
options = described_class.construct_params(params)
expect(options).to eq(
{
'image_request[locale]' => 'en',
'image_request[language]' => 'en',
'image_request[latitude]' => '5',
'image_request[longitude]' => '5',
'image_request[altitude]' => '5',
'image_request[device_id]' => '5',
'image_request[ttl]' => '5',
'image_request[remote_image_url]' => 'test_url',
'image_request[image]' => 'test_file',
'focus[x]' => '5',
'focus[y]' => '5'
'locale' => 'en',
'language' => 'en',
'latitude' => '5',
'longitude' => '5',
'altitude' => '5',
'device_id' => '5',
'ttl' => '5',
'remote_image_url' => 'test_url',
'image' => 'test_file',
'focus_x' => '5',
'focus_y' => '5'
}
)
end
Expand All @@ -50,8 +50,8 @@

it 'returns the proper result' do
stub_post(
path: '/image_requests',
body: { "image_request"=>{ "locale"=>"en", "remote_image_url"=>"test_url" } },
path: '/v1/images',
body: { "locale" => "en", "remote_image_url" => "test_url" },
response: fixture_file('image_request.json')
)

Expand All @@ -64,8 +64,8 @@

it 'responds correctly to a response exception error' do
stub_post(
path: '/image_requests',
body: { "image_request"=>{ "locale"=>"en", "remote_image_url"=>"test_url" } },
path: '/v1/images',
body: { "locale" => "en", "remote_image_url" => "test_url" },
response: fixture_file('error_response.json')
)

Expand All @@ -74,8 +74,8 @@

it 'responds correctly to an unexpected response' do
stub_post(
path: '/image_requests',
body: { "image_request"=>{ "locale"=>"en", "remote_image_url"=>"test_url" } },
path: '/v1/images',
body: { "locale" => "en", "remote_image_url" => "test_url" },
response: fixture_file('unexpected_response.json')
)

Expand All @@ -84,12 +84,12 @@
end

describe '#repost' do
let(:params) { { image_request: { locale: 'en', remote_image_url: 'test_url' } } }
let(:params) { { locale: 'en', remote_image_url: 'test_url' } }

it 'returns the proper result' do
stub_post(
path: '/image_requests/sample_token/repost',
body: { "image_request"=>{ "locale"=>"en", "remote_image_url"=>"test_url" } },
path: '/v1/images/sample_token/repost',
body: { "locale" => "en", "remote_image_url" => "test_url" },
response: fixture_file('image_request.json')
)

Expand All @@ -102,8 +102,8 @@

it 'responds correctly to a response exception error' do
stub_post(
path: '/image_requests/sample_token/repost',
body: { "image_request"=>{ "locale"=>"en", "remote_image_url"=>"test_url" } },
path: '/v1/images/sample_token/repost',
body: { "locale" => "en", "remote_image_url" => "test_url" },
response: fixture_file('error_response.json')
)

Expand All @@ -112,8 +112,8 @@

it 'responds correctly to an unexpected response' do
stub_post(
path: '/image_requests/sample_token/repost',
body: { "image_request"=>{ "locale"=>"en", "remote_image_url"=>"test_url" } },
path: '/v1/images/sample_token/repost',
body: { "locale" => "en", "remote_image_url" => "test_url" },
response: fixture_file('unexpected_response.json')
)

Expand Down
12 changes: 6 additions & 6 deletions spec/cloudsight/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
describe '#get' do
it 'returns the proper result' do
stub_get(
path: '/image_responses/sample_token',
path: '/v1/images/sample_token',
response: fixture_file('completed_response.json')
)

Expand All @@ -21,7 +21,7 @@

it 'responds correctly to a response exception error' do
stub_get(
path: '/image_responses/sample_token',
path: '/v1/images/sample_token',
response: fixture_file('error_response.json')
)

Expand All @@ -30,7 +30,7 @@

it 'responds correctly to an unexpected response' do
stub_get(
path: '/image_responses/sample_token',
path: '/v1/images/sample_token',
response: fixture_file('unexpected_response.json')
)

Expand All @@ -40,7 +40,7 @@

describe '#retrieve' do
it 'returns the proper result' do
stub_polling(3, 'image_request.json', 'completed_response.json', '/image_responses/sample_token')
stub_polling(3, 'image_request.json', 'completed_response.json', '/v1/images/sample_token')

response = described_class.retrieve('sample_token', poll_wait: 0.01)

Expand All @@ -50,13 +50,13 @@
end

it 'responds correctly to a response exception error' do
stub_polling(3, 'image_request.json', 'error_response.json', '/image_responses/sample_token')
stub_polling(3, 'image_request.json', 'error_response.json', '/v1/images/sample_token')

expect { described_class.retrieve('sample_token', poll_wait: 0.01) }.to raise_error Cloudsight::ResponseException
end

it 'responds correctly to an unexpected response' do
stub_polling(3, 'image_request.json', 'unexpected_response.json', '/image_responses/sample_token')
stub_polling(3, 'image_request.json', 'unexpected_response.json', '/v1/images/sample_token')

expect { described_class.retrieve('sample_token', poll_wait: 0.01) }.to raise_error Cloudsight::UnexpectedResponseException
end
Expand Down

0 comments on commit ed6940a

Please sign in to comment.