Permalink
Browse files

Merge pull request #2 from shutl/shutl_namespacing

Shutl namespacing
  • Loading branch information...
2 parents 992cd55 + a57af6e commit 0cb50bb37964a4ebe5054ae781eff2f6b7ea66c2 @markburns markburns committed Dec 19, 2012
View
@@ -1,4 +1,4 @@
-# ShutlResource
+# Shutl::Resource
Yep this is ActiveResource-esque.
@@ -14,83 +14,46 @@ the request is fine, but we couldn't generate any quotes. It doesn't feel
like a 4xx or a 5xx, but not quite like a 2xx either. Comments/thoughts
more than welcome.
-#Config
-
-`config/initializers/shutl_resource.rb`
-
-```ruby
-Shutl::Resource.configure do |c|
- c.logger = Rails.logger
-end
-```
-
-```ruby
-class ApplicationController
- include ShutlResource::ApplicationControllerMethods
-end
-```
-
#Usage
```ruby
-#app/resources/spider_cow.rb
-class SpiderCow
- include ShutlResource::RestResource
- base_uri "http://localhost:3001"
-end
-
-#app/controllers/spider_cows_controller.rb
-class SpiderCowsController < ShutlResource::BackendResourcesController
-end
-
-#/app/converters/boolean_converter.rb
-module BooleanConverter
- extend self
-
- def to_front_end b; b end
- def to_back_end b; b == 'true' end
+#app/models/shutl/quote.rb
+class Shutl::QuoteCollection
+ include Shutl::Resource::Rest
+ base_uri "http://shutl-api-url"
end
+```
-#/app/converters/spider_cow_converter.rb
-module SpiderCowConverter
- extend ShutlResource::Converter
-
- convert :enabled,
- with: BooleanConverter,
- only: :to_back_end
-end
+The following exceptions may be raised
+```
+ 200..399 no problem
+ 299 Shutl::NoQuotesGenerated
+ 400 Shutl::BadRequest
+ 401 Shutl::UnauthorizedAccess
+ 403 Shutl::ForbiddenAccess
+ 404 Shutl::ResourceNotFound
+ 409 Shutl::ResourceConflict
+ 410 Shutl::ResourceGone
+ 422 Shutl::ResourceInvalid
+ 500 Shutl::ServiceUnavailable
```
+## Installation
+
+Add this line to your rails app's Gemfile:
+ gem 'shutl_resource'
# OAuth2
-It uses OAuth2 Bearer tokens for API calls.
+It uses OAuth2 Bearer tokens for API calls using the shutl_auth gem
e.g. the following header is attached to requests
`
Authorization: Bearer some/big/long/base64/thing/goes/here==
`
-```
- 200..399 no problem
- 299 ShutlResource::NoQuotesGenerated
- 400 ShutlResource::BadRequest
- 401 ShutlResource::UnauthorizedAccess
- 403 ShutlResource::ForbiddenAccess
- 404 ShutlResource::ResourceNotFound
- 409 ShutlResource::ResourceConflict
- 410 ShutlResource::ResourceGone
- 422 ShutlResource::ServerError
- 500 ShutlResource::ServiceUnavailable
-```
-
-## Installation
-
-Add this line to your rails app's Gemfile:
-
- gem 'shutl_resource'
## Contributing
@@ -1,161 +0,0 @@
-module ShutlResource
- class BackendResourcesController < ApplicationController
- respond_to :json
- before_filter :request_access_token
- before_filter :load_resource, only: [:show]
-
- rescue_from ShutlResource::Error do |e|
- case e.response.content_type
- when "text/html"
- response_hash = { debug_info: e.response.body.to_json}
-
- when "application/json"
- begin
- response_hash = e.response.parsed_response
- rescue
- debug_info = <<-eof
- Failed to parse JSON response from quote service:
-
- #{e.message}
-
- #{e.class}
-
- #{e.backtrace}
-
- #{e.response.body}
- eof
-
- response_hash = { debug_info: debug_info}
- end
- end
-
- render status: e.response.code, json: response_hash
- end
-
- def index
- authenticated_request do
- instance_variable_set(
- pluralized_instance_variable_name,
- resource_klass.all(id_params.merge auth: access_token))
- end
-
- response_collection = pluralized_instance_variable.map do |o|
- attributes_to_front_end o.attributes
- end
-
- render json: response_collection
- end
-
- def new
- self.instance_variable= resource_klass.new
- respond_with_json
- end
-
- def create
- authenticated_request do
- self.instance_variable= resource_klass.create id_params.merge(attributes_from_params), auth: access_token
- end
-
- render status: instance_variable.status, json: instance_variable.parsed
- end
-
- def show
- respond_with_json
- end
-
- def update
- authenticated_request do
- resource_klass.update id_params.merge(attributes_from_params), auth: access_token
- end
-
- render nothing: true, status: 204
- end
-
- def destroy
- authenticated_request do
- resource_klass.destroy(id_params.merge(id: params[:id]), auth: access_token)
- end
-
- render nothing: true, status: 204
- end
-
- private
-
- def id_params
- params.dup.keep_if do |k, v|
- k == 'id' or k =~ /_id\Z/
- end
- end
-
- # Respond_with does not work in this case
- def respond_with_json(status=200)
- render status: status, json: attributes_to_front_end(instance_variable.attributes)
- end
-
- def load_resource
- authenticated_request do
- self.instance_variable = resource_klass.find(id_params, auth: access_token)
- end
-
- render nothing: true, status: 404 if instance_variable.nil?
- end
-
- def resource_klass
- resource_klass_name.constantize
- end
-
- def instance_variable
- instance_variable_get instance_variable_name
- end
-
- def instance_variable= value
- instance_variable_set instance_variable_name, value
- end
-
- def pluralized_instance_variable
- instance_variable_get pluralized_instance_variable_name
- end
-
- def pluralized_instance_variable= value
- instance_variable_set pluralized_instance_variable_name, value
- end
-
- def pluralized_instance_variable_name
- :"@#{pluralized_resource_name}"
- end
-
- def pluralized_resource_name
- singular_resource_name.pluralize
- end
-
- def instance_variable_name
- :"@#{singular_resource_name}"
- end
-
- def singular_resource_name
- resource_klass_name.underscore
- end
-
- def resource_klass_name
- self.class.to_s.gsub(/Controller/,"").singularize
- end
-
- def attributes_from_params
- @attributes ||= converter_class.to_back_end set_attributes_from_params
- end
-
- def set_attributes_from_params
- params[singular_resource_name].dup.tap do |p|
- p[:id] = params[:id] if params[:id]
- end
- end
-
- def converter_class
- ShutlResource::Converter.class_for resource_klass
- end
-
- def attributes_to_front_end attrs
- converter_class.to_front_end attrs
- end
- end
-end
@@ -0,0 +1,26 @@
+module Shutl::Resource
+ class AccessTokenRequest
+ def initialize
+ @client = Rack::OAuth2::Client.new(
+ identifier: Shutl.authentication_service[:client_id],
+ secret: Shutl.authentication_service[:client_secret],
+ token_endpoint: '/token',
+ host: uri.host,
+ port: uri.port,
+ scheme: uri.scheme
+ )
+ end
+
+ def access_token!
+ Shutl.retry_connection "Authentication Service Error" do
+ @client.access_token!
+ end
+ end
+
+ private
+
+ def uri
+ @uri ||= URI Shutl.authentication_service[:url]
+ end
+ end
+end
@@ -1,6 +1,6 @@
require 'logger'
-module ShutlResource
+module Shutl::Resource
def configure(*args, &block)
Configuration.configure(*args, &block)
end
@@ -0,0 +1,28 @@
+class Shutl::Resource::Error < ::IOError
+ attr_reader :response
+
+ def initialize message, http_response
+ @response = http_response
+
+ super message #it really is rather spot on, why thanks for saying, kind sir.
+ end
+end
+
+# This NoQuotesGenerated is shutl specific corresponding to HTTP status 299.
+# We had a good think about what the correct HTTP code is for the case that
+# the request is fine, but we couldn't generate any quotes. It doesn't feel
+# like a 4xx or a 5xx, but not quite like a 2xx either. Comments/thoughts
+# more than welcome.
+Shutl::NoQuotesGenerated = Class.new Shutl::Resource::Error
+
+Shutl::BadRequest = Class.new Shutl::Resource::Error
+Shutl::UnauthorizedAccess = Class.new Shutl::Resource::Error
+Shutl::ForbiddenAccess = Class.new Shutl::Resource::Error
+Shutl::ResourceNotFound = Class.new Shutl::Resource::Error
+Shutl::ResourceConflict = Class.new Shutl::Resource::Error
+Shutl::ResourceGone = Class.new Shutl::Resource::Error
+Shutl::ResourceInvalid = Class.new Shutl::Resource::Error
+Shutl::ServerError = Class.new Shutl::Resource::Error
+Shutl::ServiceUnavailable = Class.new Shutl::Resource::Error
+
+
@@ -6,16 +6,16 @@
require 'uri'
require 'active_model'
-module ShutlResource
- module RestResource
+module Shutl::Resource
+ module Rest
extend HTTParty
include ActiveModel::Serialization
attr_reader :response
def self.included(base)
base.send :include, HTTParty
- base.send :extend, ShutlResource::RestResourceClassMethods
+ base.send :extend, Shutl::Resource::RestClassMethods
base.send :headers, {
'Accept' => 'application/json',
Oops, something went wrong.

0 comments on commit 0cb50bb

Please sign in to comment.