phocoder-rb 0.1.0

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,30 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "activesupport"
7
+ gem "i18n"
8
+ gem "builder"
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "rspec", "~> 2.1.0"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.5.1"
15
+ gem "rcov", ">= 0"
16
+ gem "mocha"
17
+ gem "webmock", "~>1.6.0"
18
+ end
19
+
20
+
21
+ group :test do
22
+ gem "rspec", "~> 2.1.0"
23
+ gem "bundler", "~> 1.0.0"
24
+ gem "jeweler", "~> 1.5.1"
25
+ gem "rcov", ">= 0"
26
+ gem 'autotest', '4.4.4'
27
+ gem 'redgreen', '1.2.2'
28
+ gem "webmock", "~>1.6.0"
29
+ gem "mocha"
30
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.3)
5
+ addressable (2.2.2)
6
+ autotest (4.4.4)
7
+ builder (2.1.2)
8
+ crack (0.1.8)
9
+ diff-lcs (1.1.2)
10
+ git (1.2.5)
11
+ i18n (0.5.0)
12
+ jeweler (1.5.1)
13
+ bundler (~> 1.0.0)
14
+ git (>= 1.2.5)
15
+ rake
16
+ mocha (0.9.10)
17
+ rake
18
+ rake (0.8.7)
19
+ rcov (0.9.9)
20
+ redgreen (1.2.2)
21
+ rspec (2.1.0)
22
+ rspec-core (~> 2.1.0)
23
+ rspec-expectations (~> 2.1.0)
24
+ rspec-mocks (~> 2.1.0)
25
+ rspec-core (2.1.0)
26
+ rspec-expectations (2.1.0)
27
+ diff-lcs (~> 1.1.2)
28
+ rspec-mocks (2.1.0)
29
+ webmock (1.6.1)
30
+ addressable (>= 2.2.2)
31
+ crack (>= 0.1.7)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ activesupport
38
+ autotest (= 4.4.4)
39
+ builder
40
+ bundler (~> 1.0.0)
41
+ i18n
42
+ jeweler (~> 1.5.1)
43
+ mocha
44
+ rcov
45
+ redgreen (= 1.2.2)
46
+ rspec (~> 2.1.0)
47
+ webmock (~> 1.6.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jeremy Green
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
+ = phocoder-rb
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to phocoder-rb
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) 2010 Jeremy Green. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "phocoder-rb"
16
+ gem.homepage = "http://github.com/jagthedrummer/phocoder-rb"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{The ruby client for the phocoder.com API.}
19
+ gem.description = %Q{The ruby client for the phocoder.com API.}
20
+ gem.email = "jagthedrummer@gmail.com"
21
+ gem.authors = ["Jeremy Green"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "phocoder-rb #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,81 @@
1
+ module Phocoder
2
+ class Base
3
+
4
+ def self.api_key
5
+ Phocoder.api_key
6
+ end
7
+
8
+ def self.base_url
9
+ Phocoder.base_url
10
+ end
11
+
12
+ def self.encode(content, format=nil)
13
+ if content.is_a?(String)
14
+ content
15
+ elsif format.to_s == 'xml'
16
+ if content.is_a?(Hash) && content.keys.size == 1
17
+ content[content.keys.first].to_xml(:root => content.keys.first)
18
+ else
19
+ content.to_xml
20
+ end
21
+ else
22
+ content.to_json
23
+ end
24
+ end
25
+
26
+ def encode(content, format=nil)
27
+ self.class.encode(content, format)
28
+ end
29
+
30
+ def self.decode(content, format=nil)
31
+ if content.is_a?(String)
32
+ if format.to_s == 'xml'
33
+ Hash.from_xml(content)
34
+ else
35
+ ActiveSupport::JSON.decode(content)
36
+ end
37
+ else
38
+ content
39
+ end
40
+ end
41
+
42
+ def decode(content, format=nil)
43
+ self.class.decode(content, format)
44
+ end
45
+
46
+ protected
47
+
48
+ def self.apply_api_key(params, format=nil)
49
+ if api_key
50
+ decoded_params = decode(params, format).with_indifferent_access
51
+
52
+ if decoded_params[:api_request]
53
+ decoded_params[:api_request] = decoded_params[:api_request].with_indifferent_access
54
+ end
55
+
56
+ if format.to_s == 'xml'
57
+ if !(decoded_params[:api_request] && decoded_params[:api_request][:api_key])
58
+ decoded_params[:api_request] ||= {}.with_indifferent_access
59
+ decoded_params[:api_request][:api_key] = api_key
60
+ end
61
+ else
62
+ decoded_params['api_key'] = api_key unless decoded_params['api_key']
63
+ end
64
+
65
+ decoded_params
66
+ else
67
+ params
68
+ end
69
+ end
70
+
71
+ def self.merge_params(options, params)
72
+ if options[:params]
73
+ options[:params] = options[:params].merge(params)
74
+ options
75
+ else
76
+ options.merge(:params => params)
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,43 @@
1
+ module Phocoder
2
+ class Error < StandardError
3
+
4
+ def initialize(error_or_message)
5
+ if error_or_message.is_a?(Exception)
6
+ @error = error_or_message
7
+ else
8
+ @message = error_or_message
9
+ end
10
+ end
11
+
12
+ def message
13
+ @message || "#{@error.class} (wrapped in a #{self.class}) - #{@error.message}"
14
+ end
15
+
16
+ def backtrace
17
+ if @error
18
+ @error.backtrace
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def inspect
25
+ if @error
26
+ "#{@error.inspect} (wrapped in a #{self.class})"
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def to_s
33
+ if @error
34
+ "#{@error.class} (wrapped in a #{self.class}) - #{@error}"
35
+ else
36
+ super
37
+ end
38
+ end
39
+ end
40
+
41
+ class HTTPError < Error; end
42
+
43
+ end
@@ -0,0 +1,39 @@
1
+ unless Hash.method_defined?(:recursive_with_indifferent_access)
2
+ class Hash
3
+ def recursive_with_indifferent_access
4
+ hash = with_indifferent_access
5
+
6
+ hash.each do |key, value|
7
+ if value.is_a?(Hash) || value.is_a?(Array)
8
+ hash[key] = value.recursive_with_indifferent_access
9
+ end
10
+ end
11
+
12
+ hash
13
+ end
14
+ end
15
+ end
16
+
17
+ unless Array.method_defined?(:recursive_with_indifferent_access)
18
+ class Array
19
+ def recursive_with_indifferent_access
20
+ array = dup
21
+
22
+ array.each_with_index do |value, index|
23
+ if value.is_a?(Hash) || value.is_a?(Array)
24
+ array[index] = value.recursive_with_indifferent_access
25
+ end
26
+ end
27
+
28
+ array
29
+ end
30
+ end
31
+ end
32
+ #
33
+ #unless String.method_defined?(:shell_escape)
34
+ # class String
35
+ # def shell_escape
36
+ # empty? ? "''" : gsub(/([^A-Za-z0-9_\-.,:\/@\n])/n, '\\\\\\1').gsub(/\n/, "'\n'")
37
+ # end
38
+ # end
39
+ #end
@@ -0,0 +1,140 @@
1
+ # Ruby's Net/HTTP Sucks
2
+ # Borrowed root cert checking from http://redcorundum.blogspot.com/2008/03/ssl-certificates-and-nethttps.html
3
+
4
+ module Phocoder
5
+ class HTTP < Base
6
+ class NetHTTP
7
+
8
+ attr_accessor :method, :url, :uri, :body, :params, :headers, :timeout, :skip_ssl_verify, :options
9
+
10
+ cattr_accessor :root_cert_paths
11
+
12
+ self.root_cert_paths = ['/etc/ssl/certs',
13
+ '/var/ssl',
14
+ '/usr/share/ssl',
15
+ '/usr/ssl',
16
+ '/etc/ssl',
17
+ '/usr/local/openssl',
18
+ '/usr/lib/ssl',
19
+ '/System/Library/OpenSSL',
20
+ '/etc/openssl',
21
+ '/usr/local/ssl',
22
+ '/etc/pki/tls']
23
+
24
+ def initialize(method, url, options)
25
+ @method = method
26
+ @url = url
27
+ @body = options.delete(:body)
28
+ @params = options.delete(:params)
29
+ @headers = options.delete(:headers)
30
+ @timeout = options.delete(:timeout)
31
+ @skip_ssl_verify = options.delete(:skip_ssl_verify)
32
+ @options = options
33
+ end
34
+
35
+ def self.post(url, options={})
36
+ new(:post, url, options).perform
37
+ end
38
+
39
+ def self.put(url, options={})
40
+ new(:put, url, options).perform
41
+ end
42
+
43
+ def self.get(url, options={})
44
+ new(:get, url, options).perform
45
+ end
46
+
47
+ def self.delete(url, options={})
48
+ new(:delete, url, options).perform
49
+ end
50
+
51
+ def perform
52
+ deliver(http, request)
53
+ end
54
+
55
+
56
+ protected
57
+
58
+ def deliver(http, request)
59
+ if timeout
60
+ Timeout.timeout(timeout / 1000.0) do
61
+ http.request(request)
62
+ end
63
+ else
64
+ http.request(request)
65
+ end
66
+ end
67
+
68
+ def http
69
+ u = uri
70
+
71
+ http = Net::HTTP.new(u.host, u.port)
72
+
73
+ if u.scheme == 'https'
74
+ http.use_ssl = true
75
+
76
+ if !skip_ssl_verify && root_cert_path = locate_root_cert_path
77
+ http.ca_path = root_cert_path
78
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
79
+ http.verify_depth = 5
80
+ else
81
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
82
+ end
83
+ end
84
+
85
+ http
86
+ end
87
+
88
+ def request
89
+ r = request_class.new(path)
90
+ r.body = body if body
91
+ if headers
92
+ headers.each do |header, value|
93
+ r.add_field(header.to_s, value.to_s)
94
+ end
95
+ end
96
+ r
97
+ end
98
+
99
+ def uri
100
+ u = URI.parse(url)
101
+
102
+ if params
103
+ params_as_query = params.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
104
+ if u.query.to_s.empty?
105
+ u.query = params_as_query
106
+ else
107
+ u.query = [u.query.to_s, params_as_query].join('&')
108
+ end
109
+ end
110
+
111
+ u
112
+ end
113
+
114
+ def path
115
+ u = uri
116
+
117
+ if u.path.empty?
118
+ u.path = '/'
119
+ end
120
+
121
+ if u.query.to_s.empty?
122
+ u.path
123
+ else
124
+ u.path + '?' + u.query.to_s
125
+ end
126
+ end
127
+
128
+ def request_class
129
+ Net::HTTP.const_get(method.to_s.capitalize)
130
+ end
131
+
132
+ def locate_root_cert_path
133
+ self.class.root_cert_paths.detect do |root_cert_path|
134
+ File.directory?(root_cert_path)
135
+ end
136
+ end
137
+
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,31 @@
1
+ module Phocoder
2
+ class HTTP < Base
3
+ class Typhoeus
4
+
5
+ def self.post(url, options={})
6
+ perform(:post, url, options)
7
+ end
8
+
9
+ def self.put(url, options={})
10
+ perform(:put, url, options)
11
+ end
12
+
13
+ def self.get(url, options={})
14
+ perform(:get, url, options)
15
+ end
16
+
17
+ def self.delete(url, options={})
18
+ perform(:delete, url, options)
19
+ end
20
+
21
+ def self.perform(method, url, options={})
22
+ if options.delete(:skip_ssl_verify)
23
+ options[:disable_ssl_peer_verification] = true
24
+ end
25
+
26
+ ::Typhoeus::Request.send(method, url, options)
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,90 @@
1
+ module Phocoder
2
+ class HTTP < Base
3
+
4
+ attr_accessor :body, :url, :options, :method, :format
5
+
6
+ cattr_accessor :default_options
7
+ cattr_accessor :http_backend
8
+
9
+ self.http_backend = NetHTTP
10
+
11
+ self.default_options = {:timeout => 10000,
12
+ :headers => {'Accept' => 'application/json',
13
+ 'Content-Type' => 'application/json'}}.recursive_with_indifferent_access
14
+
15
+ def initialize(method, url, options={})
16
+ self.method = method
17
+ self.url = url
18
+ self.format = options.delete(:format)
19
+ self.options = options
20
+ self.body = options.delete(:body)
21
+ end
22
+
23
+ def self.post(url, body, options={})
24
+ new(:post, url, options.merge(:body => body)).perform_method
25
+ end
26
+
27
+ def self.put(url, body, options={})
28
+ new(:put, url, options.merge(:body => body)).perform_method
29
+ end
30
+
31
+ def self.get(url, options={})
32
+ new(:get, url, options).perform_method
33
+ end
34
+
35
+ def self.delete(url, options={})
36
+ new(:delete, url, options).perform_method
37
+ end
38
+
39
+ def perform_method
40
+ process(http_backend.send(method, url, options))
41
+ rescue StandardError => e
42
+ raise HTTPError.new(e)
43
+ end
44
+
45
+ def options=(value)
46
+ @options = default_options.recursive_with_indifferent_access.merge(value || {})
47
+
48
+ options[:headers] ||= {}
49
+ options[:headers]['Accept'] = "application/#{format}"
50
+ options[:headers]['Content-Type'] = "application/#{format}"
51
+
52
+ options
53
+ end
54
+
55
+ def options
56
+ @options || self.options = default_options
57
+ end
58
+
59
+ def format
60
+ @format ||= :json
61
+ end
62
+
63
+ def http_backend
64
+ self.class.http_backend
65
+ end
66
+
67
+ def default_options
68
+ self.class.default_options.recursive_with_indifferent_access
69
+ end
70
+
71
+
72
+ protected
73
+
74
+ def process(http_response)
75
+ response = Response.new
76
+ response.code = http_response.code
77
+
78
+ begin
79
+ response.body = decode(http_response.body.to_s, format)
80
+ rescue StandardError # Hack! Returns different exceptions depending on the decoding engine
81
+ response.body = http_response.body
82
+ end
83
+
84
+ response.raw_body = http_response.body
85
+ response.raw_response = http_response
86
+ response
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,42 @@
1
+ module Phocoder
2
+ class Job < Base
3
+
4
+ def self.create(params={}, options={})
5
+ params = apply_api_key(params, options[:format])
6
+ HTTP.post("#{options[:base_url] || base_url}/jobs",
7
+ encode(params, options[:format]),
8
+ options)
9
+ end
10
+
11
+ def self.list(options={})
12
+ params = {:api_key => options.delete(:api_key) || api_key,
13
+ :page => options.delete(:page) || 1,
14
+ :per_page => options.delete(:per_page) || 50,
15
+ :state => options.delete(:state) }
16
+
17
+ HTTP.get("#{options[:base_url] || base_url}/jobs", merge_params(options, params))
18
+ end
19
+
20
+ def self.details(job_id, options={})
21
+ params = {:api_key => options.delete(:api_key) || api_key}
22
+ HTTP.get("#{options[:base_url] || base_url}/jobs/#{job_id}", merge_params(options, params))
23
+ end
24
+
25
+ def self.resubmit(job_id, options={})
26
+ params = {:api_key => options.delete(:api_key) || api_key}
27
+ HTTP.get("#{options[:base_url] || base_url}/jobs/#{job_id}/resubmit", merge_params(options, params))
28
+ end
29
+
30
+ def self.cancel(job_id, options={})
31
+ params = {:api_key => options.delete(:api_key) || api_key}
32
+ HTTP.get("#{options[:base_url] || base_url}/jobs/#{job_id}/cancel", merge_params(options, params))
33
+ end
34
+
35
+ def self.delete(job_id, options={})
36
+ params = {:api_key => options.delete(:api_key) || api_key}
37
+ HTTP.delete("#{options[:base_url] || base_url}/jobs/#{job_id}", merge_params(options, params))
38
+ end
39
+
40
+
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ module Phocoder
2
+
3
+ mattr_writer :api_key
4
+ mattr_writer :base_url
5
+
6
+ self.api_key = nil
7
+ self.base_url = 'https://photoapi.chaos.webapeel.com/'
8
+
9
+ def self.api_key
10
+ @@api_key || ENV['PHOCODER_API_KEY']
11
+ end
12
+
13
+ def self.base_url(env=nil)
14
+ @@base_url
15
+ end
16
+
17
+ end