easy-api 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,8 @@ module Easy::Api::ControllerMethods
5
5
  def render_format
6
6
  respond_to do |format|
7
7
  format.html { render :status => @result.status_code }
8
- format.json { render :json => @result, :status => @result.status_code }
8
+ format.xml { render :xml => @result.to_xml(:root => 'response', :skip_types => true), :status => @result.status_code }
9
+ format.json { render :json => @result, :status => (params[:callback].present? ? 200 : @result.status_code), :callback => params[:callback] }
9
10
  end
10
11
  end
11
12
 
@@ -3,27 +3,31 @@ module Easy::Api
3
3
  class Error
4
4
  attr_reader :code, :message
5
5
 
6
- CODES = {
7
- invalid: 400,
8
- unauthorized: 401,
9
- not_found: 404,
10
- unexpected: 500
11
- }
6
+ def self.codes
7
+ {
8
+ invalid: 400,
9
+ unauthorized: 401,
10
+ not_found: 404,
11
+ unexpected: 500
12
+ }
13
+ end
12
14
 
13
- MESSAGES = {
14
- invalid: "Invalid request",
15
- unauthorized: "This request requires a valid Private API Key",
16
- not_found: "Resource not found",
17
- unexpected: 'Sorry! An exception has occured. Please try again later',
18
- }
15
+ def self.messages
16
+ {
17
+ invalid: "Invalid request",
18
+ unauthorized: "This request requires a valid Private API Key",
19
+ not_found: "Resource not found",
20
+ unexpected: 'Sorry! An exception has occured. Please try again later',
21
+ }
22
+ end
19
23
 
20
24
  # Initializes a new error based on the type, with an optional custom message
21
25
  #
22
26
  # @param [Symbol] type can be :invalid, :unauthorized, :not_found, or :unexpected
23
27
  # @param [optional, String] msg a custom error message (see MESSAGES for default message values)
24
28
  def initialize(type, msg=nil)
25
- @code = CODES[type]
26
- @message = msg || MESSAGES[type]
29
+ @code = self.class.codes[type]
30
+ @message = msg || self.class.messages[type]
27
31
  end
28
32
 
29
33
  # Returns the error as a hash
@@ -39,5 +43,12 @@ module Easy::Api
39
43
  def as_json(options={})
40
44
  to_hash
41
45
  end
46
+
47
+ # Used by Rails to parse the error as xml
48
+ #
49
+ # @return [Hash] a hash containing the error code and message
50
+ def to_xml(options={})
51
+ to_hash.to_xml(options)
52
+ end
42
53
  end
43
54
  end
@@ -34,6 +34,20 @@ module Easy::Api
34
34
  # Will always contain 'success', the error if there is one, and any dynamic attributes.
35
35
  # @return [Hash]
36
36
  def as_json(options={})
37
+ convert_to_hash
38
+ end
39
+
40
+ # Used by Rails to parse the result as xml
41
+ #
42
+ # Will always contain 'success', the error if there is one, and any dynamic attributes.
43
+ # @return [Hash]
44
+ def to_xml(options={})
45
+ convert_to_hash.to_xml(options)
46
+ end
47
+
48
+ private
49
+
50
+ def convert_to_hash
37
51
  hash = marshal_dump.merge(success: success)
38
52
  hash[:error] = error unless error.nil?
39
53
  hash
@@ -1,5 +1,5 @@
1
1
  module Easy
2
2
  module Api
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -11,7 +11,7 @@ describe Easy::Api::Error do
11
11
 
12
12
  context "when no message is passed in" do
13
13
  let(:message) { nil }
14
- its(:message) { should == Easy::Api::Error::MESSAGES[:invalid] }
14
+ its(:message) { should == Easy::Api::Error.messages[:invalid] }
15
15
  end
16
16
  end
17
17
 
@@ -19,7 +19,7 @@ describe Easy::Api::Error do
19
19
  subject { Easy::Api::Error.new(:unexpected, msg).as_json }
20
20
  let(:msg) { 'uh, oh' }
21
21
 
22
- its([:code]) { should == Easy::Api::Error::CODES[:unexpected] }
22
+ its([:code]) { should == Easy::Api::Error.codes[:unexpected] }
23
23
  its([:message]) { should == msg }
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  segments:
112
112
  - 0
113
- hash: 3062167263312735286
113
+ hash: -2946297048628258418
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  none: false
116
116
  requirements:
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  segments:
121
121
  - 0
122
- hash: 3062167263312735286
122
+ hash: -2946297048628258418
123
123
  requirements: []
124
124
  rubyforge_project:
125
125
  rubygems_version: 1.8.24