tbg_api_consumer 0.0.6.pre

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b468006c1114a60637f89b0fb60a5d4b878be0b1
4
+ data.tar.gz: a06016b46f7d0a2379baa2b8e774c04406027a40
5
+ SHA512:
6
+ metadata.gz: 6e69021539cff89be68c3339fe46fdb88a28a43bc247a01bd2183ccd05cba9569a298ab12e91389976705e83f0cb12fb46f5c25f9605d58e1611201f6c260e2b
7
+ data.tar.gz: 3c422c0c2add9895a9f988edc7fbc7bf50c5effb7fbccf52cc6d77692a179874e21e6cfd1bec793fd6e622c0ce1a5aea4be70fc31850255a94dbbc8a5b871d74
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.pryrc ADDED
@@ -0,0 +1,62 @@
1
+ Pry.config.editor = "vim"
2
+
3
+ # https://github.com/kyrylo/pry-theme
4
+ # $ gem install pry-theme
5
+ Pry.config.theme = "solarized"
6
+
7
+ # Custom prompt
8
+ prompt = "ruby-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
9
+ Pry.config.prompt = [
10
+ proc { |obj, nest_level, _| "#{prompt} (#{obj}):#{nest_level} > " },
11
+ proc { |obj, nest_level, _| "#{prompt} (#{obj}):#{nest_level} * " }
12
+ ]
13
+
14
+ # Default Command Set, add custom methods here:
15
+ default_command_set = Pry::CommandSet.new do
16
+ command "pccopy", "Copy argument to the clip-board" do |str|
17
+ IO.popen('pbcopy', 'w') { |f| f << str.to_s }
18
+ end
19
+ end
20
+
21
+ # https://github.com/michaeldv/awesome_print/
22
+ # $ gem install awesome_print
23
+ begin
24
+ require 'awesome_print'
25
+ Pry.config.print = proc { |output, value| output.puts value.ai(:indent => 2) }
26
+ rescue LoadError => e
27
+ warn "[WARN] #{e}"
28
+ puts "$ gem install awesome_print"
29
+ end
30
+
31
+ # Use Array.toy to get an array to play with
32
+ class Array
33
+ def self.toy(n = 10, &block)
34
+ block_given? ? Array.new(n, &block) : Array.new(n) { |i| i+1 }
35
+ end
36
+ end
37
+
38
+ # Use Hash.toy to get an hash to play with
39
+ class Hash
40
+ def self.toy(n = 10)
41
+ Hash[Array.toy(n).zip(Array.toy(n){ |c| (96+(c+1)).chr })]
42
+ end
43
+ end
44
+
45
+ # Launch Pry with access to the entire Rails stack.
46
+ rails = File.join(Dir.getwd, 'config', 'environment.rb')
47
+ if File.exist?(rails) && ENV['SKIP_RAILS'].nil?
48
+ require rails
49
+ case Rails.version.to_i
50
+ when 2
51
+ require 'console_app'
52
+ require 'console_with_helpers'
53
+ when 3
54
+ require 'rails/console/app'
55
+ require 'rails/console/helpers'
56
+ extend Rails::ConsoleMethods if Rails.version.to_f >= 3.2
57
+ else
58
+ warn "[WARN] cannot load Rails console commands"
59
+ end
60
+ end
61
+
62
+ Pry.config.commands.import(default_command_set)
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api_consumer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 luxerama
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
+ # ApiConsumer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'api_consumer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install api_consumer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/api_consumer/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"
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_consumer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tbg_api_consumer"
8
+ spec.version = ApiConsumer::VERSION
9
+ spec.authors = ["luxerama"]
10
+ spec.email = ["vincent@siebert.im"]
11
+ spec.summary = %q{API module}
12
+ spec.description = %q{API module to make the consumtion of standardised API responses easier}
13
+ spec.homepage = "https://github.com/thebeansgroup/api_consumer"
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.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_dependency "activeresource", "~> 4.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.5"
26
+ spec.add_development_dependency 'pry-byebug', '~> 1.3'
27
+ spec.add_development_dependency 'pry-rails', '~> 0.3'
28
+ spec.add_development_dependency 'pry-stack_explorer', '~> 0.4'
29
+ spec.add_development_dependency 'awesome_print', '~> 1.2'
30
+ spec.add_development_dependency "rake", '~> 10.3'
31
+ end
@@ -0,0 +1,16 @@
1
+ module ApiConsumer
2
+ module Attributes
3
+ class Base
4
+ STANDARDIZATIONS_KEY = "meta"
5
+ attr_accessor :attributes
6
+
7
+ def initialize(attributes)
8
+ self.attributes = attributes
9
+ end
10
+
11
+ def standardized?
12
+ self.attributes.has_key? STANDARDIZATIONS_KEY
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ module ApiConsumer
2
+ module Elements
3
+ Meta = Struct.new(:code, :error_type, :error_message) do
4
+
5
+ def initialize(attributes)
6
+ raise "Meta required a status code" unless attributes.has_key? 'code'
7
+ attributes.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module ApiConsumer
2
+ module Elements
3
+ Order = Struct.new :order_by do
4
+ def initialize(attributes)
5
+ attributes.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
6
+ end
7
+
8
+ def to_s
9
+ self.order_by
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module ApiConsumer
2
+ module Elements
3
+ Pagination = Struct.new(:page, :limit) do
4
+
5
+ def initialize(attributes)
6
+ attributes.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module ApiConsumer
2
+ module Elements
3
+ class Response
4
+ def add_attribute(attr)
5
+ self.class.send(:define_method, "#{attr}=".to_sym) { |v| instance_variable_set "@#{attr}", v }
6
+ self.class.send(:define_method, attr.to_sym) { instance_variable_get "@#{attr}" }
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module ApiConsumer
2
+ module Parser
3
+
4
+ class << self
5
+ def parse(data)
6
+ data.map do |key, item|
7
+ parser = ParserRegistry.find_parser_for key
8
+ parser.load item
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ module ApiConsumer
2
+ class ParserRegistry
3
+
4
+ DEFAULT_NAMESPACE = :root
5
+
6
+ class_attribute :registered_parsers
7
+
8
+ self.registered_parsers = {}
9
+
10
+ class << self
11
+
12
+ def registered
13
+ self.registered_parsers
14
+ end
15
+
16
+ # Return whether or not the namespace has a parser in the registry
17
+ # with the given name
18
+ def registered?(name, namespace = DEFAULT_NAMESPACE)
19
+ registered = has_namespace? namespace
20
+ if registered
21
+ registered = self.registered_parsers[namespace].has_key? name
22
+ end
23
+ registered
24
+ end
25
+
26
+ def has_namespace?(namespace)
27
+ self.registered_parsers.has_key? namespace
28
+ end
29
+
30
+ def registered_namespaces
31
+ registered.keys
32
+ end
33
+
34
+ def register(name, parser, namespace = DEFAULT_NAMESPACE)
35
+ self.registered_parsers[namespace] ||= {}
36
+ registered[namespace][name] = parser
37
+ end
38
+
39
+ def parser_for(key, namespace = DEFAULT_NAMESPACE)
40
+ return self.registered_parsers.fetch(namespace).fetch(key) if registered? key, namespace
41
+ return self.registered_parsers.fetch(DEFAULT_NAMESPACE).fetch(key) if registered? key
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ module ApiConsumer
2
+ module Parsers
3
+ class Base
4
+ def self.parse(attributes)
5
+ raise "Not defined"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_resource'
2
+
3
+ module ApiConsumer
4
+ module Parsers
5
+ class Collection < ::ActiveResource::Collection
6
+ attr_accessor :response
7
+
8
+ def initialize(parsed = {})
9
+ @elements = parsed.fetch('data')
10
+ parsed.delete 'data'
11
+ @response = parsed
12
+ end
13
+
14
+ def collect!
15
+ @response = ApiConsumer::Processor.process @response, resource_class.to_s
16
+ super
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module ApiConsumer
2
+ module Parsers
3
+ class Meta < Base
4
+ def self.parse(parsed = {})
5
+ ApiConsumer::Elements::Meta.new parsed
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module ApiConsumer
2
+ module Parsers
3
+ class Order
4
+ def self.parse(parsed = {})
5
+ ApiConsumer::Elements::Order.new parsed
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module ApiConsumer
2
+ module Parsers
3
+ class Pagination
4
+ def self.parse(parsed = {})
5
+ ApiConsumer::Elements::Pagination.new parsed
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module ApiConsumer
2
+ class Processor
3
+ class << self
4
+ def process(data, namespace)
5
+ response = Elements::Response.new
6
+ data.each do |key, item|
7
+ parser = ParserRegistry.parser_for key, namespace
8
+ unless parser.nil?
9
+ response.add_attribute key
10
+ response.send "#{key}=", parser.parse(item)
11
+ end
12
+ end
13
+ response
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module ApiConsumer
2
+ class Railtie < Rails::Railtie
3
+ config.to_prepare do
4
+ #ApiConsumer.setup!
5
+ end
6
+
7
+ initializer "api_consumer.configure_default_parsers" do |app|
8
+ ApiConsumer.configure do |config|
9
+ config.build_parser_registry do |reg|
10
+ reg.register 'meta', Parsers::Meta
11
+ reg.register 'pagination', Parsers::Pagination
12
+ reg.register 'order', Parsers::Order
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ApiConsumer
2
+ VERSION = "0.0.6.pre"
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'api_consumer/version'
2
+ require 'api_consumer/parser_registry'
3
+ require 'api_consumer/processor'
4
+ require 'api_consumer/parsers/base'
5
+ require 'api_consumer/parsers/collection'
6
+ require 'api_consumer/parsers/meta'
7
+ require 'api_consumer/parsers/pagination'
8
+ require 'api_consumer/parsers/order'
9
+ require 'api_consumer/elements/response'
10
+ require 'api_consumer/elements/pagination'
11
+ require 'api_consumer/elements/order'
12
+ require 'api_consumer/elements/meta'
13
+ require 'api_consumer/attributes/base'
14
+ require 'api_consumer/railtie' if defined?(Rails)
15
+
16
+ module ApiConsumer
17
+
18
+ class << self
19
+ attr_accessor :configuration
20
+ end
21
+
22
+ def self.configure
23
+ self.configuration ||= Configuration.new
24
+ yield configuration
25
+ end
26
+
27
+ module Base
28
+ module Initializer
29
+ def initialize(attributes, persisted = false)
30
+ if Attributes::Base.new(attributes).standardized?
31
+ attributes = attributes.fetch('data')
32
+ end
33
+
34
+ super attributes, persisted
35
+ end
36
+ end
37
+
38
+ mattr_accessor :parser_registry
39
+ def self.included(base)
40
+ base.class_eval do
41
+ base.collection_parser = Parsers::Collection
42
+ base.send :prepend, Initializer
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ class Configuration
49
+ attr_accessor :parser_registry
50
+
51
+ def initialize
52
+ self.parser_registry = ParserRegistry
53
+ end
54
+
55
+ def build_parser_registry(&block)
56
+ yield self.parser_registry
57
+ end
58
+ end
59
+
60
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tbg_api_consumer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6.pre
5
+ platform: ruby
6
+ authors:
7
+ - luxerama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeresource
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-stack_explorer
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.3'
111
+ description: API module to make the consumtion of standardised API responses easier
112
+ email:
113
+ - vincent@siebert.im
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".pryrc"
120
+ - ".ruby-version"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - api_consumer.gemspec
126
+ - lib/api_consumer.rb
127
+ - lib/api_consumer/attributes/base.rb
128
+ - lib/api_consumer/elements/meta.rb
129
+ - lib/api_consumer/elements/order.rb
130
+ - lib/api_consumer/elements/pagination.rb
131
+ - lib/api_consumer/elements/response.rb
132
+ - lib/api_consumer/parser.rb
133
+ - lib/api_consumer/parser_registry.rb
134
+ - lib/api_consumer/parsers/base.rb
135
+ - lib/api_consumer/parsers/collection.rb
136
+ - lib/api_consumer/parsers/meta.rb
137
+ - lib/api_consumer/parsers/order.rb
138
+ - lib/api_consumer/parsers/pagination.rb
139
+ - lib/api_consumer/processor.rb
140
+ - lib/api_consumer/railtie.rb
141
+ - lib/api_consumer/version.rb
142
+ homepage: https://github.com/thebeansgroup/api_consumer
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: 2.0.0
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.3.1
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.2.2
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: API module
166
+ test_files: []
167
+ has_rdoc: