binder_sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Binder SDK
2
+
3
+ [![Build Status](https://travis-ci.org/meducation/binder_sdk.png)](https://travis-ci.org/meducation/binder_sdk)
4
+ [![Dependencies](https://gemnasium.com/meducation/binder_sdk.png?travis)](https://gemnasium.com/meducation/binder_sdk)
5
+ [![Code Climate](https://codeclimate.com/github/meducation/binder_sdk.png)](https://codeclimate.com/github/meducation/binder_sdk)
6
+
7
+ A wrapper for Binder's SDK.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'binder_sdk'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ Resources accept the following methods:
24
+
25
+ ```ruby
26
+ BinderSDK::MediaFile.find(id)
27
+ BinderSDK::MediaFile.find_each #=> do ... end
28
+ BinderSDK::MediaFile.where(foo: 'bar') #=> []
29
+ BinderSDK::MediaFile.where(foo: 'bar').select([:id, :name]) #=> []
30
+ BinderSDK::MediaFile.create(foo: 'bar')
31
+ ```
32
+
33
+ The following objects are currently supported:
34
+
35
+ ```ruby
36
+ User
37
+ ```
38
+
39
+ ### Is it any good?
40
+
41
+ [Yes.](http://news.ycombinator.com/item?id=3067434)
42
+
43
+ ## Contributing
44
+
45
+ Firstly, thank you!! :heart::sparkling_heart::heart:
46
+
47
+ We'd love to have you involved. Please read our [contributing guide](https://github.com/meducation/propono/tree/master/CONTRIBUTING.md) for information on how to get stuck in.
48
+
49
+ ### Contributors
50
+
51
+ This project is managed by the [Meducation team](http://company.meducation.net/about#team).
52
+
53
+ These individuals have come up with the ideas and written the code that made this possible:
54
+
55
+ - [Jeremy Walker](http://github.com/iHID)
56
+
57
+ ## Licence
58
+
59
+ Copyright (C) 2013 New Media Education Ltd
60
+
61
+ This program is free software: you can redistribute it and/or modify
62
+ it under the terms of the GNU Affero General Public License as published by
63
+ the Free Software Foundation, either version 3 of the License, or
64
+ (at your option) any later version.
65
+
66
+ This program is distributed in the hope that it will be useful,
67
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
68
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69
+ GNU Affero General Public License for more details.
70
+
71
+ A copy of the GNU Affero General Public License is available in [Licence.md](https://github.com/meducation/propono/blob/master/LICENCE.md)
72
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ end
7
+ task default: :test
8
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'binder_sdk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "binder_sdk"
8
+ spec.version = BinderSDK::VERSION
9
+ spec.authors = ["Jeremy Walker"]
10
+ spec.email = ["jez.walker@gmail.com"]
11
+ spec.description = "Binder's SDK"
12
+ spec.summary = "The SDK for Binder"
13
+ spec.homepage = "http://github.com/meducation/binder_sdk"
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_dependency "activesupport", '>= 3.2.15'
22
+ spec.add_dependency "loquor", '~> 1.2'
23
+ spec.add_dependency "filum", '~> 2.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "minitest", '~> 5.0.8'
28
+ spec.add_development_dependency "mocha"
29
+ end
@@ -0,0 +1,43 @@
1
+ module BinderSDK
2
+ class BinderSDKError < StandardError
3
+ end
4
+ class BinderSDKConfigurationError < BinderSDKError
5
+ end
6
+
7
+ class Configuration
8
+
9
+ SETTINGS = [
10
+ :logger
11
+ ]
12
+
13
+ attr_writer *SETTINGS
14
+
15
+ def initialize
16
+ self.logger = Filum.logger
17
+ end
18
+
19
+ [:access_id, :secret_key, :endpoint, :cache].each do |setting|
20
+ define_method "#{setting}=" do |val|
21
+ Loquor.config.send("#{setting}=", val)
22
+ end
23
+ end
24
+
25
+ def logger=(val)
26
+ @logger = val
27
+ Loquor.config.logger = val
28
+ end
29
+
30
+ SETTINGS.each do |setting|
31
+ define_method setting do
32
+ get_or_raise(setting)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def get_or_raise(setting)
39
+ instance_variable_get("@#{setting.to_s}") ||
40
+ raise(BinderSDKConfigurationError.new("Configuration for #{setting} is not set"))
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,6 @@
1
+ module BinderSDK
2
+ EmailShortcodes = {
3
+ "b_nwusr" => "New User",
4
+ "b_pswdr" => "Password Reset"
5
+ }
6
+ end
@@ -0,0 +1,27 @@
1
+ module BinderSDK
2
+ module Helpers
3
+ SDK_TO_SPI_MAPPINGS = {}
4
+
5
+ def sdk_class_for(spi_type)
6
+ sdk_type_for(spi_type).constantize
7
+ end
8
+
9
+ def sdk_type_for(spi_type)
10
+ if SDK_TO_SPI_MAPPINGS.has_value?(spi_type)
11
+ SDK_TO_SPI_MAPPINGS.key(spi_type)
12
+ else
13
+ "BinderSDK::#{spi_type.gsub("::", "")}"
14
+ end
15
+ end
16
+
17
+ def spi_type_for(sdk_type)
18
+ sdk_type = sdk_type.gsub(/Mock$/, "")
19
+ if SDK_TO_SPI_MAPPINGS.has_key?(sdk_type)
20
+ SDK_TO_SPI_MAPPINGS[sdk_type]
21
+ else
22
+ sdk_type.gsub("BinderSDK::", "")
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,72 @@
1
+ module BinderSDK
2
+ class Mocker
3
+ def self.mock!
4
+ RESOURCES.each do |resource|
5
+ new(resource).mock!
6
+ end
7
+ end
8
+
9
+ def self.unmock!
10
+ RESOURCES.each do |resource|
11
+ new(resource).unmock!
12
+ end
13
+ end
14
+
15
+ def initialize(resource_name)
16
+ @resource_name = resource_name
17
+ end
18
+
19
+ def mock!
20
+ return if mock_module.const_defined?(original_class_name)
21
+
22
+ resource = "#{mock_module_name}::#{resource_class_name}".constantize
23
+ mock = "#{mock_module_name}::#{mock_class_name}".constantize
24
+
25
+ mock_module.const_set(original_class_name, resource)
26
+ mock_module.send(:remove_const, resource_class_name)
27
+ mock_module.const_set(resource_class_name, mock)
28
+ mock_module.send(:remove_const, mock_class_name)
29
+ end
30
+
31
+ def unmock!
32
+ return unless mock_module.const_defined?(original_class_name)
33
+
34
+ original = "#{mock_module_name}::#{original_class_name}".constantize
35
+ resource = "#{mock_module_name}::#{resource_class_name}".constantize
36
+
37
+ mock_module.const_set(mock_class_name, resource)
38
+ mock_module.send(:remove_const, resource_class_name)
39
+ mock_module.const_set(resource_class_name, original)
40
+ mock_module.send(:remove_const, original_class_name)
41
+ end
42
+
43
+ private
44
+ def resource_class_name
45
+ @resource_class_name ||= @resource_name.camelize.split("::").last
46
+ end
47
+
48
+ def mock_class_name
49
+ @mock_class_name ||= "#{@resource_name}_mock".camelize.split("::").last
50
+ end
51
+
52
+ def original_class_name
53
+ @original_class_name ||= "#{@resource_name}_original".camelize.split("::").last
54
+ end
55
+
56
+ def mock_module
57
+ @mock_module ||= mock_module_name.constantize
58
+ end
59
+
60
+ def mock_module_name
61
+ @mock_module_name ||= begin
62
+ parts = @resource_name.camelize.split("::")
63
+ if parts.size == 1
64
+ "BinderSDK"
65
+ else
66
+ parts.pop
67
+ "BinderSDK::#{parts.join("::")}"
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ module BinderSDK
2
+ class Resource < Loquor::Resource
3
+ include Helpers
4
+
5
+ def self.spi_type=(type)
6
+ Helpers::SDK_TO_SPI_MAPPINGS[self.name] = type
7
+ end
8
+
9
+ def created_at
10
+ DateTime.parse(@data[:created_at])
11
+ end
12
+
13
+ def updated_at
14
+ DateTime.parse(@data[:updated_at])
15
+ end
16
+ end
17
+ end
18
+
19
+ RESOURCES = %w{
20
+ user
21
+ }
22
+
23
+ require "binder_sdk/resources/user"
24
+ RESOURCES.each do |resource|
25
+ require "binder_sdk/resources/#{resource}"
26
+ end
@@ -0,0 +1,23 @@
1
+ module BinderSDK
2
+ class User < Resource
3
+ self.path = "/users"
4
+ end
5
+
6
+ class UserMock < User
7
+ extend Loquor::ResourceMock
8
+
9
+ self.attributes = {
10
+ id: 1,
11
+ first_name: "Jeremy",
12
+ surname: "Walker",
13
+ name: "Jeremy Walker",
14
+ short_display_name: "Jeremy",
15
+ photo_url: "http://i.telegraph.co.uk/multimedia/archive/02351/cross-eyed-cat_2351472k.jpg",
16
+ primary_email_address: "jez.walker@gmail.com",
17
+ url_code: "asd1234567",
18
+ is_site_admin: false,
19
+ reputation: 1023,
20
+ viewable_collections: []
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module BinderSDK
2
+ VERSION = "0.0.1"
3
+ end
data/lib/binder_sdk.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'active_support/all'
2
+ require 'loquor'
3
+
4
+ require 'binder_sdk/version'
5
+ require 'binder_sdk/configuration'
6
+ require 'binder_sdk/mocker'
7
+
8
+ require 'binder_sdk/helpers'
9
+ require 'binder_sdk/resource'
10
+ require 'binder_sdk/email_shortcodes'
11
+
12
+ module BinderSDK
13
+ def self.config
14
+ @config ||= Configuration.new
15
+ if block_given?
16
+ yield @config
17
+ else
18
+ @config
19
+ end
20
+ end
21
+
22
+ def self.mock!
23
+ BinderSDK::Mocker.mock!
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'test_helper'
2
+ module BinderSDK
3
+ class ArbitaryTest < Minitest::Test
4
+ def test_shit_happens
5
+ skip
6
+ MediaFile.update(8, title: "Foobar to the Malcy Max")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,51 @@
1
+ require_relative 'test_helper'
2
+
3
+ module BinderSDK
4
+ class ConfigurationTest < Minitest::Test
5
+
6
+ def setup
7
+ super
8
+ BinderSDK.instance_variable_set("@config", nil)
9
+ end
10
+
11
+ def test_obtaining_singletion
12
+ refute BinderSDK.config.nil?
13
+ end
14
+
15
+ def test_block_syntax
16
+ test_key = "foobar-123-access"
17
+ BinderSDK.config do |config|
18
+ config.access_id = test_key
19
+ end
20
+ assert_equal test_key, Loquor.config.access_id
21
+ end
22
+
23
+ def test_endpoint_is_set_correctly
24
+ Configuration.new
25
+ assert_equal "http://localhost:3000/spi", Loquor.config.endpoint
26
+ end
27
+
28
+ def test_retry_404s_is_set_correctly
29
+ Configuration.new
30
+ assert_equal false, Loquor.config.retry_404s
31
+ end
32
+
33
+ def test_access_id_proxies_to_loquor
34
+ access_id = "test-access-id"
35
+ BinderSDK.config.access_id = access_id
36
+ assert_equal access_id, Loquor.config.access_id
37
+ end
38
+
39
+ def test_secret_key_proxies_to_loquor
40
+ key = "test-secret-key"
41
+ BinderSDK.config.secret_key = key
42
+ assert_equal key, Loquor.config.secret_key
43
+ end
44
+
45
+ def test_cache_proxies_to_loquor
46
+ cache = mock()
47
+ BinderSDK.config.cache = cache
48
+ assert_equal cache, Loquor.config.cache
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'test_helper'
2
+
3
+ module BinderSDK
4
+ class MockerTest < Minitest::Test
5
+
6
+ def test_mock_switches_classes
7
+ BinderSDK.mock!
8
+ assert BinderSDK.const_defined?(:UserOriginal)
9
+ assert BinderSDK.const_defined?(:User)
10
+ refute BinderSDK.const_defined?(:UserMock)
11
+ ensure
12
+ BinderSDK::Mocker.unmock!
13
+ end
14
+
15
+ def test_mock_substitutes_in_mock_classes
16
+ BinderSDK.mock!
17
+
18
+ id = 1
19
+ Loquor::HttpAction::Get.expects(:get).never
20
+ assert id, User.find('id').id
21
+ ensure
22
+ BinderSDK::Mocker.unmock!
23
+ end
24
+
25
+ def test_mock_sample_returns_media_file_with_attributes
26
+ BinderSDK.mock!
27
+
28
+ user = User.sample
29
+ assert_equal user.first_name, "Jeremy"
30
+ ensure
31
+ BinderSDK::Mocker.unmock!
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'test_helper'
2
+ module BinderSDK
3
+ class ResourceTest < Minitest::Test
4
+ def self.test_resource(klass, url)
5
+ define_method :klass do klass end
6
+ define_method :url do url end
7
+
8
+ class_eval do
9
+ def test_path
10
+ assert_equal url, klass.path
11
+ end
12
+
13
+ def test_generates_find_url
14
+ id = 7
15
+ Loquor::HttpAction::Get.expects(:get).with("#{url}/#{id}", anything())
16
+ klass.find(id)
17
+ end
18
+ end
19
+ end
20
+ define_method 'test_helpers_included' do
21
+ assert Resource.new({}).respond_to?(:sdk_class_for)
22
+ end
23
+
24
+ def test_created_at_is_a_datetime
25
+ resource = Resource.new(created_at: '2014-02-23T23:34:49Z')
26
+ assert resource.created_at.is_a?(DateTime)
27
+ assert_equal DateTime.new(2014, 2, 23, 23, 34, 49), resource.created_at
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ require_relative '../test_helper'
2
+ require_relative '../resource_test'
3
+
4
+ module BinderSDK
5
+ class UserTest < ResourceTest
6
+ test_resource(User, '/users')
7
+ end
8
+ end
9
+
10
+
@@ -0,0 +1,29 @@
1
+ gem 'minitest'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+ require 'minitest/mock'
5
+ require 'mocha/setup'
6
+
7
+ lib = File.expand_path('../../lib', __FILE__)
8
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
9
+
10
+ require 'binder_sdk'
11
+ require_relative 'resources/user_test.rb'
12
+
13
+ class Minitest::Test
14
+ def setup
15
+ Filum.setup("./log/test.log")
16
+ BinderSDK.config do |config|
17
+ config.logger = mock()
18
+ config.logger.stubs(:debug)
19
+ config.logger.stubs(:info)
20
+ config.logger.stubs(:error)
21
+ end
22
+ Loquor.config do |config|
23
+ config.logger = BinderSDK.config.logger
24
+ config.access_id = "Sermo"
25
+ config.secret_key = "foobar"
26
+ config.endpoint = "http://localhost:3000/spi"
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: binder_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Walker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.15
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.15
27
+ - !ruby/object:Gem::Dependency
28
+ name: loquor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: filum
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 5.0.8
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 5.0.8
97
+ - !ruby/object:Gem::Dependency
98
+ name: mocha
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
+ description: Binder's SDK
112
+ email:
113
+ - jez.walker@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .travis.yml
120
+ - CHANGELOG.md
121
+ - CONTRIBUTING.md
122
+ - Gemfile
123
+ - LICENCE.md
124
+ - README.md
125
+ - Rakefile
126
+ - binder_sdk.gemspec
127
+ - lib/binder_sdk.rb
128
+ - lib/binder_sdk/configuration.rb
129
+ - lib/binder_sdk/email_shortcodes.rb
130
+ - lib/binder_sdk/helpers.rb
131
+ - lib/binder_sdk/mocker.rb
132
+ - lib/binder_sdk/resource.rb
133
+ - lib/binder_sdk/resources/user.rb
134
+ - lib/binder_sdk/version.rb
135
+ - test/arbitary_test.rb
136
+ - test/configuration_test.rb
137
+ - test/mocker_test.rb
138
+ - test/resource_test.rb
139
+ - test/resources/user_test.rb
140
+ - test/test_helper.rb
141
+ homepage: http://github.com/meducation/binder_sdk
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.2.1
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: The SDK for Binder
165
+ test_files:
166
+ - test/arbitary_test.rb
167
+ - test/configuration_test.rb
168
+ - test/mocker_test.rb
169
+ - test/resource_test.rb
170
+ - test/resources/user_test.rb
171
+ - test/test_helper.rb