jsonorama 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jsonorama.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/jsonorama.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jsonorama/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jsonorama"
7
+ s.version = Jsonorama::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Sam Taylor"]
10
+ s.email = ["sjltaylor@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{for helping out with json apis}
13
+ #s.description = %q{TODO: Write a gem description}
14
+
15
+ s.rubyforge_project = "jsonorama"
16
+
17
+ s.add_dependency 'blobject'
18
+ s.add_dependency 'rack'
19
+
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,53 @@
1
+ module Jsonorama
2
+ module ControllerSupport
3
+
4
+ protected
5
+ # 'status': HTTP Response Status Code, response is an optional Blobject
6
+ def json_api_response(status, response=Jsonorama.json_response)
7
+ response.status_code = Rack::Utils.status_code status
8
+ yield response if block_given?
9
+ respond_to do |format|
10
+ format.json do
11
+ render :json => response, :status => status
12
+ # don't return a head, difficult to handle in clientside ajax
13
+ end
14
+ end
15
+ end
16
+
17
+ # if a block is given is will be passed (model, valid?, omg_api_response, rails_response_hash)
18
+ # the rails response hash allows the status code and json object to be modified
19
+ # usage:
20
+ # respond_to_api_post(model) do |model, valid, r, h|
21
+ # if valid
22
+ # do_some_business_logic
23
+ # # modifiy the status code (just for example)
24
+ # h[:status]=201
25
+ # else
26
+ # # set an error message in the api response
27
+ # r.message = 'model was invalid. you silly sausage'
28
+ # end
29
+ # end
30
+ def respond_to_api_post active_model
31
+ r = Jsonorama.json_response
32
+ h = {:json => r}
33
+
34
+ if active_model.valid?
35
+ h[:status] = :ok # HTTP 200
36
+ yield active_model, true, r, h if block_given?
37
+ else
38
+ r.errors = active_model.errors
39
+ h[:status] = :bad_request # HTTP 400
40
+ yield active_model, false, r, h if block_given?
41
+ end
42
+
43
+ r.status_code = Rack::Utils.status_code h[:status]
44
+
45
+ respond_to do |format|
46
+ format.json do
47
+ render h
48
+ # don't return a head, difficult to handle in clientside ajax
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ class Exception
2
+ def as_json
3
+ {:class_name => self.class.name.to_s, :message => self.message, :backtrace => self.backtrace}
4
+ end
5
+ end
@@ -0,0 +1,112 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module Jsonorama
5
+ class JsonClient
6
+ class ClientError < Exception; end
7
+ class ServerError < Exception; end
8
+
9
+ protected
10
+
11
+ def process_response http_response, options
12
+ c = http_response.code
13
+ case
14
+ when c.starts_with?('4') then
15
+ raise ClientError.new "HTTP:#{c}. #{http_response.message}" if options[:client_error]==:raise
16
+ when c.starts_with?('5') then
17
+ raise ServerError.new "HTTP:#{c}. #{http_response.message}" if options[:server_error]==:raise
18
+ else
19
+ json_response = Blobject.from_json http_response.body if http_response.body && (http_response.content_type=='application/json')
20
+
21
+ json_response.defrost
22
+
23
+ json_response ||= Api.json_response
24
+ json_response.status ||= c.to_i
25
+ json_response.message ||= http_response.message
26
+
27
+ json_response.freeze
28
+ json_response.data.freeze
29
+
30
+ return json_response
31
+ end
32
+ end
33
+
34
+ def handle_exception exception, options
35
+ raise exception if options[:exception]==:raise
36
+ r = Jsonorama.json_response
37
+ r.exception = exception
38
+ r
39
+ end
40
+
41
+ def api_client_version
42
+ v = Version.get
43
+ return false if v=='0.0.0'
44
+ v
45
+ end
46
+
47
+ def query_string
48
+ if api_client_version
49
+ sprintf('?%s', {:api_client_version => api_client_version}.to_query)
50
+ else
51
+ ''
52
+ end
53
+ end
54
+
55
+ def default_options
56
+ {
57
+ # how to handle different sorts of error. :return returns data with raising an exception, :raise raises an exception
58
+ :exception => :raise, # an error building the request or getting a response
59
+ :client_error => :raise, #4XX errors
60
+ :server_error => :raise, #5XX errors
61
+ }
62
+ end
63
+
64
+ public
65
+ attr_accessor :host
66
+
67
+ def initialize host
68
+ self.host = host
69
+ end
70
+
71
+ def uri path
72
+
73
+ extension = ''
74
+ extension = '.json' if path.downcase.match(/\.json$/).blank?
75
+
76
+ uri = URI.join(
77
+ self.host,
78
+ "#{path}#{extension}#{query_string}")
79
+ uri
80
+ end
81
+
82
+ def get path, options={}
83
+ options = default_options.merge options
84
+
85
+ begin
86
+ uri = uri path
87
+ http = Net::HTTP.new(uri.host, uri.port)
88
+ http_request = Net::HTTP::Get.new(uri.request_uri)
89
+ http_response = http.request(http_request)
90
+ process_response http_response, options
91
+ rescue Exception => e
92
+ handle_exception e, options
93
+ end
94
+ end
95
+
96
+ def post path, data, options={}
97
+ options = default_options.merge options
98
+
99
+ begin
100
+ uri = uri path
101
+ http = Net::HTTP.new(uri.host, uri.port)
102
+ http_request = Net::HTTP::Post.new(uri.request_uri)
103
+ http_request.set_form_data :json => ActiveSupport::JSON.encode(data)
104
+ http_response = http.request(http_request)
105
+ process_response http_response, options
106
+ rescue Exception => e
107
+ handle_exception e, options
108
+ end
109
+ end
110
+
111
+ end
112
+ end
@@ -0,0 +1,24 @@
1
+ module Jsonorama
2
+ class JsonResponse
3
+
4
+ attr_accessor :message, :errors, :data, :exception, :status_code
5
+
6
+ def initialize
7
+ self.message = nil
8
+ self.errors = nil
9
+ self.data = Blobject.new
10
+ self.exception = nil
11
+ self.status_code = nil
12
+ yield self if block_given?
13
+ self
14
+ end
15
+
16
+ def client_error?
17
+ self.status_code.to_s.starts_with? '4'
18
+ end
19
+
20
+ def server_error?
21
+ self.status_code.to_s.starts_with? '5'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Jsonorama
2
+ VERSION = "0.1.0"
3
+ end
data/lib/jsonorama.rb ADDED
@@ -0,0 +1,10 @@
1
+ Dir["lib/jsonorama/*.rb"].each {|file| require "./#{file}" }
2
+
3
+ module Jsonorama
4
+ #use on server and client to marshal to/from json
5
+ def self.json_response
6
+ r = Api::JsonResponse.new
7
+ yield r if block_given?
8
+ r
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonorama
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Sam Taylor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-12 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: blobject
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description:
39
+ email:
40
+ - sjltaylor@gmail.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - Rakefile
51
+ - jsonorama.gemspec
52
+ - lib/jsonorama.rb
53
+ - lib/jsonorama/controller_support.rb
54
+ - lib/jsonorama/exception.rb
55
+ - lib/jsonorama/json_client.rb
56
+ - lib/jsonorama/json_response.rb
57
+ - lib/jsonorama/version.rb
58
+ has_rdoc: true
59
+ homepage: ""
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project: jsonorama
82
+ rubygems_version: 1.5.0
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: for helping out with json apis
86
+ test_files: []
87
+