fotolia_rest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.7.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
14
+
15
+ gem 'json'
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ json (1.6.3)
11
+ rake (0.9.2.2)
12
+ rcov (0.9.11)
13
+ rspec (2.7.0)
14
+ rspec-core (~> 2.7.0)
15
+ rspec-expectations (~> 2.7.0)
16
+ rspec-mocks (~> 2.7.0)
17
+ rspec-core (2.7.1)
18
+ rspec-expectations (2.7.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.7.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.6.4)
28
+ json
29
+ rcov
30
+ rspec (~> 2.7.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Tony La
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = fotolia_rest
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to fotolia_rest
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Tony La. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "fotolia_rest"
18
+ gem.homepage = "http://github.com/tonyla/fotolia_rest"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Fotolia REST client}
21
+ gem.description = %Q{Wrapper library for the Fotolia API}
22
+ gem.email = "tonyla@pingg.com"
23
+ gem.authors = ["Tony La"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "fotolia_rest #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,83 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'json'
5
+ module FotoliaRest
6
+ class Client
7
+
8
+ BASE_URL = 'api.fotolia.com'
9
+ attr_reader :api_key, :username, :password, :session_id
10
+ def initialize(api_key, username, password)
11
+ @api_key = api_key
12
+ @username = username
13
+ @password = password
14
+ @session_id = nil
15
+ @logger = defined?(Rails) ? Rails.logger : nil
16
+ end
17
+
18
+ def log(level, message)
19
+ if @logger
20
+ @logger.send(level.to_sym, message)
21
+ end
22
+ end
23
+
24
+ def login
25
+ result = execute('user', 'loginUser', {:login => username, :pass => password}, :post)
26
+ @session_id = result.response['session_token'] if result.success?
27
+ return result
28
+ end
29
+
30
+ def logged_in?
31
+ @session_id && @session_id != ''
32
+ end
33
+
34
+ def execute(method, function, args={}, http_method=:get)
35
+ begin
36
+ uri = URI.parse(compose_uri(method, function))
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ if http_method == :get
39
+ uri.query = URI.encode_www_form(fotolia_parameters(args))
40
+ request = Net::HTTP::Get.new(uri.request_uri)
41
+ request.basic_auth api_key, @session_id
42
+ request['session_id'] = @session_id if logged_in?
43
+ response = http.request(request)
44
+ else
45
+ request = Net::HTTP::Post.new(uri.request_uri)
46
+ request.basic_auth api_key, @session_id
47
+ request.set_form_data(args)
48
+ request['session_id'] = @session_id if logged_in?
49
+ response = http.request(request)
50
+ end
51
+ Result.parse(response)
52
+ rescue Timeout::Error => e
53
+ log(:error, "Timeout error with #{method} #{args.inspect}")
54
+ raise FotoliaError.new("Fotolia Communication Error: timeout")
55
+ rescue => e
56
+ log(:error, "Fotolia client error with #{method} #{args.inspect} #{e.message}")
57
+ e.backtrace.each{|line| log(:debug, line)}
58
+ raise FotoliaError.new("Fotolia Communication Error: #{e.message}")
59
+ end
60
+ end
61
+
62
+ protected
63
+
64
+ def fotolia_parameters(args)
65
+ normalized = {}
66
+ args.each do |key, value|
67
+ if value.is_a?(Hash)
68
+ value.each do |inner_key, inner_value|
69
+ normalized["#{key}[#{inner_key}]"] = inner_value
70
+ end
71
+ else
72
+ normalized[key] = value
73
+ end
74
+ end
75
+ normalized
76
+ end
77
+
78
+ def compose_uri(method, function)
79
+ "http://#{BASE_URL}/Rest/1/#{method}/#{function}"
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,11 @@
1
+ module FotoliaRest
2
+ class FotoliaApiError
3
+ attr_reader :code, :message
4
+ def initialize(code, message)
5
+ @code = code
6
+ @message = message
7
+ end
8
+ end
9
+
10
+ class FotoliaError < StandardError; end
11
+ end
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+
3
+ module FotoliaRest
4
+ class Result
5
+ attr_reader :response, :error
6
+
7
+ def initialize(code, body)
8
+ @response = JSON.parse(body)
9
+ case code.to_s
10
+ when "200"
11
+ @success = true
12
+ else
13
+ @success = false
14
+ @error = FotoliaApiError.new(@response['code'], @response['error'])
15
+ end
16
+ end
17
+
18
+ def success?
19
+ @success
20
+ end
21
+
22
+ class << self
23
+ def parse(response)
24
+ self.new(response.code, response.body)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ require 'fotolia_rest/client'
2
+ require 'fotolia_rest/exceptions'
3
+ require 'fotolia_rest/result'
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "FotoliaRest" do
4
+ describe "Client" do
5
+ describe "valid credentials" do
6
+ before(:each) do
7
+ @config = YAML.load(File.open(File.expand_path(File.dirname(__FILE__) + '/../fotolia_rest.yml')))
8
+ @client = FotoliaRest::Client.new(@config['api_key'], @config['username'], @config['password'])
9
+ end
10
+ it "should be able to make GET requests" do
11
+ result = @client.execute('search', 'getSearchResults', :search_parameters => {:words => 'cat'} )
12
+ result.success?.should be_true
13
+ result.response.should_not be_nil
14
+ end
15
+
16
+ it "should be able to login" do
17
+ result = @client.login()
18
+ result.success?.should be_true
19
+ result.response.should_not be_nil
20
+ @client.session_id.should_not be_nil
21
+ end
22
+
23
+ it "should be able to make authenticated calls" do
24
+ @client.login()
25
+ result = @client.execute('media', 'getMediaComp', :id => 28261761)
26
+ result.success?.should be_true
27
+ result.response.should_not be_nil
28
+ puts result.response
29
+ end
30
+ it "should be able to make authenticated calls" do
31
+ @client.login()
32
+ result = @client.execute('user', 'getUserData')
33
+ result.success?.should be_true
34
+ result.response.should_not be_nil
35
+ puts result.response
36
+ end
37
+ end
38
+
39
+ describe "invalid credentials" do
40
+ before(:each) do
41
+ @client = FotoliaRest::Client.new('', '', '')
42
+ end
43
+
44
+ it "should not result in a success" do
45
+ result = @client.execute('search', 'getSearchResults', :search_parameters => {:words => 'cat'} )
46
+ result.success?.should be_false
47
+ result.error.code.should_not be_nil
48
+ result.error.code.should eql('010')
49
+ result.error.message.should_not be_nil
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'fotolia_rest'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fotolia_rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tony La
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &70285131528760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70285131528760
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70285131527920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70285131527920
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &70285131545940 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70285131545940
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &70285131543620 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.4
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70285131543620
58
+ - !ruby/object:Gem::Dependency
59
+ name: rcov
60
+ requirement: &70285131540980 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70285131540980
69
+ description: Wrapper library for the Fotolia API
70
+ email: tonyla@pingg.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - .rspec
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.rdoc
83
+ - Rakefile
84
+ - VERSION
85
+ - lib/fotolia_rest.rb
86
+ - lib/fotolia_rest/client.rb
87
+ - lib/fotolia_rest/exceptions.rb
88
+ - lib/fotolia_rest/result.rb
89
+ - spec/fotolia_rest_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: http://github.com/tonyla/fotolia_rest
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: 1765157675272554145
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.11
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Fotolia REST client
119
+ test_files: []