shutl_resource 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+
2
+ .rbenv-version
3
+
4
+ .bundle
5
+
6
+ .pkg
7
+
8
+ .vendor
9
+
10
+ vendor
11
+
12
+ pkg
13
+ Gemfile.lock
data/.rbenv-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p327-perf
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --drb
3
+ --debug
4
+ --tag ~@slow
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p362@shutl_resource"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.17.2 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ matrix:
3
+ allow_failures:
4
+ - jruby-19mode
5
+
6
+ rvm:
7
+ - 1.9.2
8
+ - "1.9.3"
9
+ - jruby-19mode # JRuby in 1.9 mode
10
+ jdk:
11
+ - openjdk7
12
+ - oraclejdk7
13
+
14
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shutl_resource.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Rouchy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Shutl::Resource
2
+
3
+ Yep this is ActiveResource-esque.
4
+
5
+ Abstracted from our backend, but no reason to not make this public. Possibly
6
+ useful elsewhere.
7
+ Only slightly strange thing you may find is that we have our own HTTP status
8
+ code (299) which corresponds to the case of no quotes generated. The jury is
9
+ still out on this.
10
+
11
+ This NoQuotesGenerated is shutl specific corresponding to HTTP status 299.
12
+ We had a good think about what the correct HTTP code is for the case that
13
+ the request is fine, but we couldn't generate any quotes. It doesn't feel
14
+ like a 4xx or a 5xx, but not quite like a 2xx either. Comments/thoughts
15
+ more than welcome.
16
+
17
+ #Usage
18
+
19
+ ```ruby
20
+ #app/models/shutl/quote.rb
21
+ class Shutl::QuoteCollection
22
+ include Shutl::Resource::Rest
23
+ base_uri "http://shutl-api-url"
24
+ end
25
+ ```
26
+
27
+
28
+ The following exceptions may be raised
29
+ ```
30
+ 200..399 no problem
31
+ 299 Shutl::NoQuotesGenerated
32
+ 400 Shutl::BadRequest
33
+ 401 Shutl::UnauthorizedAccess
34
+ 403 Shutl::ForbiddenAccess
35
+ 404 Shutl::ResourceNotFound
36
+ 409 Shutl::ResourceConflict
37
+ 410 Shutl::ResourceGone
38
+ 422 Shutl::ResourceInvalid
39
+ 500 Shutl::ServiceUnavailable
40
+ ```
41
+
42
+ ## Installation
43
+
44
+ Add this line to your rails app's Gemfile:
45
+
46
+ gem 'shutl_resource'
47
+
48
+ # OAuth2
49
+ It uses OAuth2 Bearer tokens for API calls using the shutl_auth gem
50
+
51
+ e.g. the following header is attached to requests
52
+
53
+ `
54
+ Authorization: Bearer some/big/long/base64/thing/goes/here==
55
+ `
56
+
57
+
58
+
59
+ ## Contributing
60
+
61
+ The usual: fork, branch, commit, pull request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = "--color --format doc"
7
+ end
8
+
9
+ task :test => :spec
10
+ task :default => :spec
data/bin/autospec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'autospec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'autospec')
data/bin/erubis ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'erubis' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('erubis', 'erubis')
data/bin/htmldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'htmldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'htmldiff')
data/bin/httparty ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'httparty' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('httparty', 'httparty')
data/bin/httpclient ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'httpclient' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('httpclient', 'httpclient')
data/bin/ldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'ldiff')
data/bin/rackup ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rackup' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rack', 'rackup')
data/bin/rails ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rails' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('railties', 'rails')
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
data/bin/rake2thor ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake2thor' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('thor', 'rake2thor')
data/bin/rdebug ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rdebug' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('debugger', 'rdebug')
data/bin/ri ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ri' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rdoc', 'ri')
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
data/bin/sprockets ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'sprockets' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('sprockets', 'sprockets')
data/bin/thor ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'thor' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('thor', 'thor')
data/bin/tilt ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'tilt' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('tilt', 'tilt')
data/bin/tt ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'tt' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('treetop', 'tt')
@@ -0,0 +1,36 @@
1
+ require 'logger'
2
+
3
+ module Shutl::Resource
4
+
5
+ class << self
6
+ attr_writer :raise_exceptions_on_validation
7
+
8
+ def raise_exceptions_on_validation
9
+ if @raise_exceptions_on_validation.nil?
10
+ @raise_exceptions_on_validation = true
11
+ end
12
+
13
+ @raise_exceptions_on_validation
14
+ end
15
+ end
16
+
17
+ def configure(*args, &block)
18
+ Configuration.configure(*args, &block)
19
+ end
20
+
21
+ module Configuration
22
+ class << self
23
+ def logger
24
+ @logger ||= Logger.new($stdout)
25
+ end
26
+
27
+ def configure
28
+ yield self
29
+ end
30
+
31
+ def logger=(logger)
32
+ @logger = logger
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ class Shutl::Resource::Error < ::IOError
2
+ attr_reader :response
3
+
4
+ def initialize message, http_response
5
+ @response = http_response
6
+
7
+ super message #it really is rather spot on, why thanks for saying, kind sir.
8
+ end
9
+ end
10
+
11
+ # This NoQuotesGenerated is shutl specific corresponding to HTTP status 299.
12
+ # We had a good think about what the correct HTTP code is for the case that
13
+ # the request is fine, but we couldn't generate any quotes. It doesn't feel
14
+ # like a 4xx or a 5xx, but not quite like a 2xx either. Comments/thoughts
15
+ # more than welcome.
16
+ Shutl::NoQuotesGenerated = Class.new Shutl::Resource::Error
17
+
18
+ Shutl::BadRequest = Class.new Shutl::Resource::Error
19
+ Shutl::UnauthorizedAccess = Class.new Shutl::Resource::Error
20
+ Shutl::ForbiddenAccess = Class.new Shutl::Resource::Error
21
+ Shutl::ResourceNotFound = Class.new Shutl::Resource::Error
22
+ Shutl::ResourceConflict = Class.new Shutl::Resource::Error
23
+ Shutl::ResourceGone = Class.new Shutl::Resource::Error
24
+ Shutl::ResourceInvalid = Class.new Shutl::Resource::Error
25
+ Shutl::ServerError = Class.new Shutl::Resource::Error
26
+ Shutl::ServiceUnavailable = Class.new Shutl::Resource::Error
27
+
28
+
@@ -0,0 +1,101 @@
1
+ require 'httparty'
2
+ require 'active_support/core_ext/string'
3
+ require 'active_support/core_ext/hash'
4
+ require 'active_support/inflector'
5
+ require 'json'
6
+ require 'uri'
7
+ require 'active_model'
8
+
9
+ module Shutl::Resource
10
+ module Rest
11
+ extend HTTParty
12
+ include ActiveModel::Serialization
13
+
14
+ attr_reader :response
15
+
16
+ def self.included(base)
17
+ base.send :include, HTTParty
18
+ base.send :extend, Shutl::Resource::RestClassMethods
19
+
20
+ base.send :headers, {
21
+ 'Accept' => 'application/json',
22
+ 'Content-Type' => 'application/json'
23
+ }
24
+
25
+ base.send :resource_name, base.name.split('::').last.underscore
26
+ base.send :resource_id, :id
27
+ end
28
+
29
+ def initialize(args = {}, response=nil)
30
+ update_attributes args
31
+ @response = response
32
+ end
33
+
34
+ def to_json(options = nil)
35
+ {
36
+ :"#{prefix}" => attributes
37
+ }.to_json(options)
38
+ end
39
+
40
+ def update_attributes(attrs)
41
+ attrs.each { |a, v| instance_variable_set(:"@#{a}", v) }
42
+ end
43
+
44
+ def update!(attrs)
45
+ new_attributes = attributes.merge attrs
46
+ update_attributes(self.class.add_resource_id_to new_attributes)
47
+ save
48
+ end
49
+
50
+ def save options={}
51
+ self.class.save self, options
52
+ end
53
+
54
+ def destroy options
55
+ self.class.destroy self, options
56
+ end
57
+
58
+ def method_missing(method, *args, &block)
59
+ if self.instance_variables.include?(:"@#{method}")
60
+ return self.instance_variable_get(:"@#{method}")
61
+ end
62
+ super
63
+ end
64
+
65
+ def parsed
66
+ response.parsed_response
67
+ end
68
+
69
+ def status
70
+ response.code
71
+ end
72
+
73
+ attr_accessor :errors
74
+
75
+ def valid?
76
+ errors.blank?
77
+ end
78
+
79
+ def resource_id
80
+ instance_variable_get :"@#{self.class.resource_id_name}"
81
+ end
82
+
83
+ def attributes
84
+ (instance_variables - [:@response]).inject({}.with_indifferent_access) do |h, var|
85
+ h.merge( { var.to_s.gsub('@','').to_sym => instance_variable_get(var)})
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def check_fail *args
92
+ self.class.send :check_fail, *args
93
+ end
94
+
95
+ protected
96
+
97
+ def prefix
98
+ self.class.instance_variable_get(:@resource_name)
99
+ end
100
+ end
101
+ end