arke 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f2b484cebed2989532174c0001b0016ac4fe3e31
4
+ data.tar.gz: 94cc8cd064543d16f67a99ea951bd57179418bcf
5
+ SHA512:
6
+ metadata.gz: f43786d9a78de90132bf3b8c710da9281df73d726713fbd4e5cd570633c7ef051660464aba64722c521e45a67a61189ab90550a55f339f84cad1fc5a7f283706
7
+ data.tar.gz: 4f5e32f17f5e0bc727a0506d155a26f4a871e477a40c79d126ba5614d4d0014140f89d11ec14c6f5aeef37554a5dd6dcaa0377c61adfdd32d4dbac7feda880e7
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .idea/**
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in arke.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 jarod
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
+ # Arke
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'arke'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install arke
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/arke/fork )
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"
data/arke.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arke/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'arke'
8
+ spec.version = Arke::VERSION
9
+ spec.authors = ['Jarod Reid']
10
+ spec.email = ['jreid@voyst.com']
11
+ spec.summary = %q{Arke is a service oriented api client, providing similar functionality to that of Active Resource}
12
+ spec.description = %q{Arke is a service oriented api client, providing similar functionality to that of Active Resource}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency 'httparty'
22
+ spec.add_dependency 'activemodel'
23
+ spec.add_dependency 'addressable'
24
+ spec.add_dependency 'activesupport'
25
+
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.5'
28
+ spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'pry-debugger'
30
+ spec.add_development_dependency 'yard'
31
+ spec.add_development_dependency 'rspec'
32
+
33
+ end
data/lib/arke.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+ require 'httparty'
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/class/attribute'
5
+ require 'active_support/core_ext/module/delegation'
6
+ require 'active_support/core_ext/string/inflections'
7
+ require 'active_support/core_ext/string/conversions'
8
+
9
+
10
+
11
+ require 'arke/version'
12
+ require 'arke/errors'
13
+ require 'arke/resource'
14
+ require 'arke/mime_types'
@@ -0,0 +1,29 @@
1
+ module Arke
2
+ module Errors
3
+
4
+ class InvalidHandler < StandardError; end
5
+
6
+ class DeserializationError < StandardError
7
+
8
+ def initialize(error)
9
+ message = "There was an error deserializing the response, failed with error: #{error.class} - #{error.message}"
10
+ super message
11
+ end
12
+
13
+ end
14
+
15
+ class MissingType < StandardError; end
16
+
17
+ class ConnectionRefused < StandardError
18
+
19
+ def initialize(host)
20
+ message = "There was an error connecting to the resource at #{host}"
21
+ super message
22
+ end
23
+
24
+ end
25
+
26
+ class MissingHandlerBlock < StandardError; end
27
+
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ module Arke
2
+ MIME_TYPES = {
3
+ 'json' => 'application/json',
4
+ 'xml' => 'application/xml'
5
+ }
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'httparty'
2
+ require 'active_model'
3
+ require 'addressable/uri'
4
+ require 'addressable/template'
5
+
6
+ require 'arke/resource/errors'
7
+ require 'arke/resource/fields'
8
+ require 'arke/resource/collection'
9
+ require 'arke/resource/model'
10
+ require 'arke/resource/associations'
@@ -0,0 +1,2 @@
1
+ require_relative './associations/base'
2
+ require_relative './associations/has_many_association'
@@ -0,0 +1,27 @@
1
+ module Arke
2
+ module Resource
3
+ module Associations
4
+ class Base
5
+
6
+ attr_reader :name, :options
7
+
8
+ def initialize(parent, name, options={})
9
+ @parent = parent
10
+ @name = name
11
+ @options = options
12
+ end
13
+
14
+ def target
15
+ klass.constantize
16
+ end
17
+
18
+ private
19
+
20
+ def klass
21
+ "#{@parent.class.name.split('::')[0..-2].join('::')}::#{name.to_s.classify}"
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module Arke
2
+ module Resource
3
+ module Associations
4
+
5
+ class HasManyAssociation < Base
6
+ include Enumerable
7
+ delegate :find, to: :target
8
+
9
+ def all(params={})
10
+ target.all(params.merge({
11
+ template: "/#{@parent.class.table_name}/#{@parent.id}/{endpoint}"
12
+ }))
13
+ end
14
+
15
+ def each(&block)
16
+ self.all.each &block
17
+ end
18
+
19
+ def count(params={})
20
+ all(params).count
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ module Arke
2
+ module Resource
3
+ class Collection
4
+ include Enumerable
5
+
6
+ def initialize(parent, array)
7
+ raise Errors::UnexpectedResponse.
8
+ new("#all expected to receive an Array from the resource, instead a #{array.class} was received.") unless array.is_a? Array
9
+ @array = array.collect { |item| parent.new(item) }
10
+ end
11
+
12
+ def each(&block)
13
+ @array.each &block
14
+ end
15
+
16
+ def [](key)
17
+ @array[key]
18
+ end
19
+
20
+ def first
21
+ self[0]
22
+ end
23
+
24
+ def last
25
+ self[-1]
26
+ end
27
+
28
+ def length
29
+ @array.length
30
+ end
31
+
32
+ def inspect
33
+ "#<#{self.class.name}:0x%08x record_count: #{self.length}>" % (object_id * 2)
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,27 @@
1
+ module Arke
2
+
3
+ module Resource
4
+ module Errors
5
+
6
+ class UnexpectedResponse < StandardError; end
7
+
8
+ class NotFound < StandardError
9
+
10
+ def initialize
11
+ message = "The specificed resource was not found"
12
+ super message
13
+ end
14
+
15
+ end
16
+
17
+ class Exception < StandardError
18
+
19
+ def initialize(resource)
20
+ message "An error on the specificed resource: Status Code #{resource.code}"
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ module Arke
2
+ module Resource
3
+ module Fields
4
+ extend ActiveSupport::Concern
5
+
6
+
7
+ module ClassMethods
8
+
9
+ def field(name, options={})
10
+ raise Errors::MissingType.new('The field type is required when defining a field.') unless options[:type]
11
+ define_getter(name.to_s, options[:type])
12
+ define_setter(name.to_s, options[:type])
13
+ end
14
+
15
+ private
16
+
17
+ def define_getter(name, klass)
18
+ instance_eval do
19
+ define_method(name) do
20
+ value = @_attributes[name]
21
+ value ? klass.new(@_attributes[name]) : klass.new
22
+ end
23
+ end
24
+ end
25
+
26
+ def define_setter(name, klass)
27
+ instance_eval do
28
+ define_method("#{name}=") do |value|
29
+ @_attributes[name] = value
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+ end
36
+
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,199 @@
1
+ module Arke
2
+ module Resource
3
+ module Model
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ included do |base|
8
+ base.class_eval do
9
+ include ActiveModel
10
+ include ActiveModel::Conversion
11
+ include ActiveModel::Validations
12
+ include HTTParty
13
+
14
+ extend ActiveModel::Naming
15
+
16
+ include Resource::Fields
17
+
18
+ class_attribute :_collection, :_url_template, :_deserializer
19
+
20
+ cattr_accessor :associations, :after_initializers
21
+
22
+
23
+ collection Arke::Resource::Collection
24
+ url_template DEFAULT_URL_TEMPLATE
25
+ deserializer { |body| JSON.parse(body) }
26
+
27
+ handle([200, 201]) { |response| JSON.parse(response.body) }
28
+
29
+ handle(404) { raise Errors::NotFound }
30
+
31
+ field :id, type: String
32
+
33
+
34
+ end
35
+ end
36
+
37
+ # The default template used for generating resource urls.
38
+ DEFAULT_URL_TEMPLATE = '/{endpoint}{/id}{?query*}'
39
+
40
+ RESPONSE_HANDLERS = {}
41
+
42
+ module ClassMethods
43
+
44
+ def find(id)
45
+ body = make_request(:get, {}, id: id)
46
+ new(body)
47
+ end
48
+
49
+ def all(params={})
50
+ body = make_request(:get, {}, params)
51
+ self._collection.new(self, body)
52
+ end
53
+
54
+ def has_many(name, options={})
55
+ self.associations ||= {}
56
+ self.associations[name] = Associations::HasManyAssociation
57
+ end
58
+
59
+ def after_initialize(&block)
60
+ @after_initializers ||= []
61
+ @after_initializers << block
62
+ end
63
+
64
+ def handle(code, &block)
65
+ raise Arke::Errors::MissingHandlerBlock unless block_given?
66
+ case code
67
+ when String, Integer, Fixnum
68
+ response_handlers[code.to_i] = block
69
+ return true
70
+ when Range, Array
71
+ code.to_a.each do |c|
72
+ response_handlers[c.to_i] = block
73
+ end
74
+ return true
75
+ else
76
+ raise Errors::InvalidHandler.
77
+ new("#{code.class.name} is an invalid class, please user an Integer, Fixnum, String, Range or Array")
78
+ end
79
+ end
80
+
81
+ def response_handlers
82
+ RESPONSE_HANDLERS
83
+ end
84
+
85
+ def collection(collection)
86
+ self._collection = collection if collection
87
+ end
88
+
89
+ # Gets or sets the url template for the resource. The url template is a
90
+ # {https://github.com/sporkmonger/addressable Addressable} template and defaults to {DEFAULT_URL_TEMPLATE}.
91
+ # If a parameter is passed to this method it will set the url template to the passed parameter, otherwise
92
+ # it will just return the URL template.
93
+ #
94
+ # @param [String] template the url template to set, if left nil, the method will simply return the current
95
+ # url template
96
+ #
97
+ # @return [Addressable::Template] the url template. If none has been set, it references {DEFAULT_URL_TEMPLATE}
98
+ def url_template(template)
99
+ self._url_template = Addressable::Template.new(template)
100
+ end
101
+
102
+ # Gets or sets the resource host.
103
+ #
104
+ # @param [String] host the resource host. if left nil, the method will simply return the current resource host
105
+ #
106
+ # @return [String] the current resource host
107
+ def host(host=nil)
108
+ @host = host if host
109
+ @host
110
+ end
111
+
112
+ # Gets the resource url. This compiles the url template using the passed options and returns the resulting
113
+ # string.
114
+ #
115
+ # @param [Hash] options the url options to pass to the template. By default the template looks for a an id and a
116
+ # query.
117
+ #
118
+ # @return [String] the full resource url
119
+ def url(options={})
120
+ template = options[:template] ? Addressable::Template.new(options.delete(:template)) : nil || _url_template
121
+ host + template.expand(options.merge(endpoint: endpoint)).to_s
122
+ end
123
+
124
+ # Gets or sets the device endpoint. By default the endpoint uses {#resource_name} if an endpoint has not been
125
+ # provided.
126
+ #
127
+ # @param [String] the service endpoint. if left nil it will simply return the existing endpoint
128
+ #
129
+ # @return [String] the service endpoint
130
+ def endpoint(endpoint=nil)
131
+ @endpoint = endpoint if endpoint
132
+ @endpoint ||= resource_name
133
+ end
134
+
135
+ def deserializer(&block)
136
+ self._deserializer = block if block_given?
137
+ end
138
+
139
+ def deserialize(body)
140
+ begin
141
+ self._deserializer.call(body)
142
+ rescue => e
143
+ raise Errors::DeserializationError.new(e)
144
+ end
145
+ end
146
+
147
+ def table_name
148
+ self.name.split('::')[-1].tableize
149
+ end
150
+
151
+ private
152
+
153
+ def resource_name
154
+ @resource_name ||= self.name.tableize
155
+ end
156
+
157
+ def make_request(method, body={}, params={})
158
+ begin
159
+ response = send(method, url(params), body)
160
+ rescue Errno::ECONNREFUSED
161
+ raise Errors::ConnectionRefused.new(host)
162
+ end
163
+ handle_response(response)
164
+ end
165
+
166
+ def handle_response(response)
167
+ response_handlers[response.code.to_i].call(response)
168
+ end
169
+
170
+ end
171
+
172
+ def initialize(attributes={})
173
+ @_attributes = attributes
174
+ self.class.after_initializers.each { |ai| ai.call } if self.class.after_initializers
175
+ end
176
+
177
+ def to_param
178
+ self.id
179
+ end
180
+
181
+ def persisted?
182
+ !self.id.nil?
183
+ end
184
+
185
+
186
+ def inspect
187
+ id = "id: ''#{self.id}'" if try(:id)
188
+ "#<#{self.class.name}:0x%08x>" % (object_id * 2)
189
+ end
190
+
191
+ private
192
+
193
+ def method_missing(meth, *args)
194
+ return self.class.associations[meth.to_sym].new(self, meth.to_sym) if self.class.associations && self.class.associations[meth.to_sym]
195
+ super
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,3 @@
1
+ module Arke
2
+ VERSION = "0.0.1"
3
+ end
File without changes
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arke
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jarod Reid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: addressable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-debugger
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Arke is a service oriented api client, providing similar functionality
140
+ to that of Active Resource
141
+ email:
142
+ - jreid@voyst.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - .gitignore
148
+ - .ruby-version
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - arke.gemspec
154
+ - lib/arke.rb
155
+ - lib/arke/errors.rb
156
+ - lib/arke/mime_types.rb
157
+ - lib/arke/resource.rb
158
+ - lib/arke/resource/associations.rb
159
+ - lib/arke/resource/associations/base.rb
160
+ - lib/arke/resource/associations/has_many_association.rb
161
+ - lib/arke/resource/collection.rb
162
+ - lib/arke/resource/errors.rb
163
+ - lib/arke/resource/fields.rb
164
+ - lib/arke/resource/model.rb
165
+ - lib/arke/version.rb
166
+ - spec/resource_spec.rb
167
+ homepage: ''
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - '>='
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - '>='
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.2.1
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Arke is a service oriented api client, providing similar functionality to
191
+ that of Active Resource
192
+ test_files:
193
+ - spec/resource_spec.rb
194
+ has_rdoc: