smooth_operator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smooth_operator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 goncalvesjoao
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,29 @@
1
+ # SmoothOperator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'smooth_operator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install smooth_operator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ module SmoothOperator
2
+ # Thank you Mario! But our princess is in another castle!
3
+ end
4
+
5
+ require "smooth_operator/version"
6
+ require "smooth_operator/base"
@@ -0,0 +1,62 @@
1
+ require "smooth_operator/operator"
2
+ require "smooth_operator/orm"
3
+
4
+ module SmoothOperator
5
+ class Base < OpenStruct
6
+
7
+ include SmoothOperator::Operator
8
+ include SmoothOperator::ORM
9
+
10
+ def hash_of_full_content
11
+ @table
12
+ end
13
+
14
+ def as_json(options = nil)
15
+ @table.as_json(options)
16
+ end
17
+
18
+ def self.nested_objects_classes(hash)
19
+ hash.each do |nested_object_symbol, nested_object_class|
20
+ define_method(nested_object_symbol.to_s) do
21
+ get_nested_object_variable(nested_object_symbol, nested_object_class)
22
+ end
23
+ end
24
+ end
25
+
26
+ private #---------------------------- private
27
+
28
+ def get_nested_object_variable(nested_object_symbol, nested_object_class)
29
+ nested_object_variable = instance_variable_get("@#{nested_object_symbol}")
30
+
31
+ return nested_object_variable if nested_object_variable.present?
32
+
33
+ nested_object_variable = initialize_nested_object_variable(hash_of_full_content[nested_object_symbol], nested_object_class, nested_object_symbol)
34
+
35
+ instance_variable_set("@#{nested_object_symbol}", nested_object_variable)
36
+
37
+ nested_object_variable
38
+ end
39
+
40
+ def initialize_nested_object_variable(attributes, nested_object_class, nested_object_symbol)
41
+ if attributes.kind_of? Array
42
+ attributes.map { |_attributes| get_nested_object(_attributes, nested_object_class, nested_object_symbol) }
43
+ else
44
+ get_nested_object(attributes, nested_object_class, nested_object_symbol)
45
+ end
46
+ end
47
+
48
+ def get_nested_object(nested_objects_attributes, nested_object_class, nested_object_symbol)
49
+ if nested_objects_attributes.blank?
50
+ plural?(nested_object_symbol) ? [] : nil
51
+ else
52
+ nested_object_class.new(nested_objects_attributes)
53
+ end
54
+ end
55
+
56
+ def plural?(string)
57
+ string = string.to_s
58
+ string == string.pluralize
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ module SmoothOperator
2
+ module Exceptions
3
+
4
+ extend self
5
+
6
+ def raise_proper_exception(response)
7
+ return nil if CODES_TO_IGNORE.include?(response.code)
8
+ exception_to_raise = (CODE_VS_EXCEPTIONS[response.code] || SmoothOperator::Exceptions::Unknown).new(response)
9
+ raise exception_to_raise, exception_to_raise.message
10
+ end
11
+
12
+ class Base < StandardError
13
+ attr_reader :response
14
+
15
+ def initialize(response)
16
+ @response = response
17
+ end
18
+ end
19
+
20
+ class Unknown < SmoothOperator::Exceptions::Base
21
+ def message
22
+ 'Unknown Error'
23
+ end
24
+ end
25
+
26
+ class NotFound < SmoothOperator::Exceptions::Base
27
+ def message
28
+ 'NotFound'
29
+ end
30
+ end
31
+
32
+ class EntityNotProcessed < SmoothOperator::Exceptions::Base
33
+ def message
34
+ 'EntityNotProcessed'
35
+ end
36
+ end
37
+
38
+ class ServerError < SmoothOperator::Exceptions::Base
39
+ def message
40
+ 'ServerError'
41
+ end
42
+ end
43
+
44
+ class AuthorizationRequired < SmoothOperator::Exceptions::Base
45
+ def message
46
+ 'AuthorizationRequired'
47
+ end
48
+ end
49
+
50
+ CODES_TO_IGNORE = [200]
51
+
52
+ CODE_VS_EXCEPTIONS = {
53
+ 401 => AuthorizationRequired,
54
+ 422 => EntityNotProcessed,
55
+ 404 => NotFound,
56
+ 500 => ServerError
57
+ }
58
+
59
+ end
60
+ end
@@ -0,0 +1,16 @@
1
+ module SmoothOperator
2
+ module OpenStruct
3
+
4
+ extend self # extends the module Class with its instance methods and
5
+ # makes it possible do 'SmoothOperator::OpenStruct.new'
6
+
7
+ def new(attributes = {})
8
+ ::OpenStruct.new(attributes).extend(SmoothOperator::OpenStruct)
9
+ end
10
+
11
+ def to_hash
12
+ @table
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,100 @@
1
+ require "smooth_operator/protocol_handler"
2
+
3
+ module SmoothOperator
4
+ module Operator
5
+
6
+ HTTP_SUCCESS_CODES = [200, 201, 202, 203, 204]
7
+
8
+ def self.included(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ attr_writer :endpoint
15
+ def endpoint
16
+ @endpoint ||= ENV["API_ENDPOINT"]
17
+ end
18
+
19
+ attr_writer :endpoint_user
20
+ def endpoint_user
21
+ @endpoint_user ||= ENV["API_USER"]
22
+ end
23
+
24
+ attr_writer :endpoint_pass
25
+ def endpoint_pass
26
+ @endpoint_pass ||= ENV["API_PASS"]
27
+ end
28
+
29
+ def get(relative_path, options = {})
30
+ make_the_call(:get, relative_path, SmoothOperator::ProtocolHandler.get_options(options))
31
+ end
32
+
33
+ def post(relative_path, options = {})
34
+ make_the_call(:post, relative_path, SmoothOperator::ProtocolHandler.post_options(options))
35
+ end
36
+
37
+ def put(relative_path, options = {})
38
+ make_the_call(:put, relative_path, SmoothOperator::ProtocolHandler.put_options(options))
39
+ end
40
+
41
+ def delete(relative_path, options = {})
42
+ make_the_call(:delete, relative_path, SmoothOperator::ProtocolHandler.delete_options(options))
43
+ end
44
+
45
+
46
+ def safe_get(relative_path, options = {})
47
+ safe_response(get(relative_path, options)) rescue nil
48
+ end
49
+
50
+ def safe_post(relative_path, options = {})
51
+ safe_response(post(relative_path, options)) rescue nil
52
+ end
53
+
54
+ def safe_put(relative_path, options = {})
55
+ safe_response(put(relative_path, options)) rescue nil
56
+ end
57
+
58
+ def safe_delete(relative_path, options = {})
59
+ safe_response(delete(relative_path, options)) rescue nil
60
+ end
61
+
62
+ def safe_response(response)
63
+ successful_response?(response) ? response.parsed_response : nil
64
+ end
65
+
66
+
67
+ def make_the_call(http_verb, relative_path, options = {})
68
+ url = build_url(relative_path)
69
+ options = build_options(options)
70
+ response = SmoothOperator::ProtocolHandler.send(http_verb, url, options)
71
+ response
72
+ end
73
+
74
+ def build_url(relative_path)
75
+ slash = '/' if relative_path.present?
76
+ extention = '.json'
77
+ [endpoint, table_name, slash, relative_path.to_s, extention].join
78
+ end
79
+
80
+ def set_basic_auth_credentials(options)
81
+ if endpoint_user.present? || endpoint_pass.present? && !options.include?(:basic_auth)
82
+ options.merge({ basic_auth: { username: endpoint_user, password: endpoint_pass } })
83
+ else
84
+ options
85
+ end
86
+ end
87
+
88
+ def build_options(options)
89
+ options = {} if options.blank?
90
+ set_basic_auth_credentials(options)
91
+ end
92
+
93
+ def successful_response?(response)
94
+ HTTP_SUCCESS_CODES.include?(response.code) || response.blank?
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,154 @@
1
+ require "smooth_operator/exceptions"
2
+
3
+ module SmoothOperator
4
+ module ORM
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ base.send(:attr_reader, :last_response)
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ attr_writer :save_attr_black_list
14
+ def save_attr_black_list
15
+ @save_attr_black_list ||= [:id, :created_at, :updated_at]
16
+ end
17
+
18
+ attr_writer :save_attr_white_list
19
+ def save_attr_white_list
20
+ @save_attr_white_list ||= []
21
+ end
22
+
23
+ def find(id, options = {})
24
+ if id == :all
25
+ find_each(options)
26
+ else
27
+ find_one(id, options)
28
+ end
29
+ end
30
+
31
+ def safe_find(id, options = {})
32
+ begin
33
+ find(id, options)
34
+ rescue Exception => exception #exception.response contains the server response
35
+ id == :all ? [] : nil
36
+ end
37
+ end
38
+
39
+ attr_writer :model_name
40
+ def model_name
41
+ @model_name ||= name.split('::').last.underscore.capitalize
42
+ end
43
+
44
+ def model_name_downcase
45
+ model_name.downcase
46
+ end
47
+
48
+ attr_writer :table_name
49
+ def table_name
50
+ @table_name ||= model_name_downcase.to_s.pluralize
51
+ end
52
+
53
+ private #------------------------------------------------ private
54
+
55
+ def instantiate_each(objects)
56
+ objects.map { |object| new(object) }
57
+ end
58
+
59
+ def find_each(options)
60
+ response = get(nil, options)
61
+ parsed_response = parse_response_or_raise_proper_exception(response)
62
+ parsed_response.kind_of?(Array) ? instantiate_each(parsed_response) : parsed_response
63
+ end
64
+
65
+ def find_one(id, options)
66
+ response = get(id, options)
67
+ parsed_response = parse_response_or_raise_proper_exception(response)
68
+ new(parsed_response)
69
+ end
70
+
71
+ def parse_response_or_raise_proper_exception(response)
72
+ if successful_response?(response)
73
+ response.parsed_response
74
+ else
75
+ SmoothOperator::Exceptions.raise_proper_exception(response)
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ def save
82
+ begin
83
+ save!
84
+ rescue Exception => exception
85
+ false
86
+ end
87
+ end
88
+
89
+ def save!
90
+ @last_response = create_or_update
91
+ import_response_errors(@last_response)
92
+ SmoothOperator::Exceptions.raise_proper_exception(@last_response) unless self.class.successful_response?(@last_response)
93
+ true
94
+ end
95
+
96
+ def destroy
97
+ return true if new_record?
98
+
99
+ @last_response = self.class.delete(self.id)
100
+ import_response_errors(@last_response)
101
+ self.class.successful_response?(@last_response)
102
+ end
103
+
104
+ def new_record?
105
+ !persisted?
106
+ end
107
+
108
+ def persisted?
109
+ try(:id).present?
110
+ end
111
+
112
+ def to_partial_path
113
+ class_name_plural = self.class.table_name
114
+ "#{class_name_plural}/#{class_name_plural.singularize}"
115
+ end
116
+
117
+ def valid?
118
+ errors.blank?
119
+ end
120
+
121
+ def invalid?
122
+ !valid?
123
+ end
124
+
125
+ def hash_of_safe_content
126
+ safe_hash = hash_of_full_content.dup
127
+
128
+ if self.class.save_attr_white_list.present?
129
+ safe_hash.slice!(*self.class.save_attr_white_list)
130
+ else
131
+ self.class.save_attr_black_list.each { |attribute| safe_hash.delete(attribute) }
132
+ end
133
+
134
+ safe_hash
135
+ end
136
+
137
+ private #-------------------------------------- private
138
+
139
+ def create_or_update
140
+ if new_record?
141
+ self.class.post('', { self.class.model_name_downcase => hash_of_safe_content })
142
+ else
143
+ self.class.put(self.id, { self.class.model_name_downcase => hash_of_safe_content })
144
+ end
145
+ end
146
+
147
+ def import_response_errors(response)
148
+ if response.present? && response.parsed_response.include?('errors')
149
+ self.errors = response.parsed_response['errors']
150
+ end
151
+ end
152
+
153
+ end
154
+ end
@@ -0,0 +1,26 @@
1
+ require 'httparty'
2
+
3
+ module SmoothOperator
4
+ class ProtocolHandler
5
+
6
+ include ::HTTParty
7
+ format :json
8
+
9
+ def self.get_options(options)
10
+ { query: options }
11
+ end
12
+
13
+ def self.post_options(options)
14
+ { body: options }
15
+ end
16
+
17
+ def self.put_options(options)
18
+ { body: options }
19
+ end
20
+
21
+ def self.delete_options(options)
22
+ { body: options }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module SmoothOperator
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'smooth_operator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "smooth_operator"
8
+ spec.version = SmoothOperator::VERSION
9
+ spec.authors = ["João Gonçalves"]
10
+ spec.email = ["goncalves.joao@gmail.com"]
11
+ spec.description = %q{ActiveResource alternative}
12
+ spec.summary = %q{Simple and fully customizable alternative to ActiveResource, based on httparty gem}
13
+ spec.homepage = "https://github.com/goncalvesjoao/smooth_operator"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency(%q<httparty>, [">= 0.11.0"])
25
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smooth_operator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - João Gonçalves
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: httparty
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.11.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.11.0
62
+ description: ActiveResource alternative
63
+ email:
64
+ - goncalves.joao@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/smooth_operator.rb
75
+ - lib/smooth_operator/base.rb
76
+ - lib/smooth_operator/exceptions.rb
77
+ - lib/smooth_operator/open_struct.rb
78
+ - lib/smooth_operator/operator.rb
79
+ - lib/smooth_operator/orm.rb
80
+ - lib/smooth_operator/protocol_handler.rb
81
+ - lib/smooth_operator/version.rb
82
+ - smooth_operator.gemspec
83
+ homepage: https://github.com/goncalvesjoao/smooth_operator
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.23
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Simple and fully customizable alternative to ActiveResource, based on httparty
108
+ gem
109
+ test_files: []
110
+ has_rdoc: