boxxspring 2.0.0

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: 903a310c247814ac0da4ec2d2c9c61ea80073807
4
+ data.tar.gz: 5bcbd825027a3c54432dd67ec82ec1c288228b62
5
+ SHA512:
6
+ metadata.gz: 5f976c76df940417ecd427c125d672192c2621b6542d736744842eb8e671dc83b62809a05869b24e2031a0eec8b8f4cfb49b68854a51b92796023c7e0eb915d5
7
+ data.tar.gz: f7efecfe2ffd0af273b5c2fb6bb78e8c558813a642596a6159cf43864e259508144ae62d8538d46e62fb1b80419fdb0d2c6777f375933437ac8231007875033a
@@ -0,0 +1,48 @@
1
+ require 'singleton'
2
+
3
+ module Boxxspring
4
+
5
+ def self.configuration( &block )
6
+ Configuration.instance().instance_eval( &block ) unless block.nil?
7
+ Configuration.instance()
8
+ end
9
+
10
+ class Configuration
11
+
12
+ include Singleton
13
+
14
+ def self.reloadable?
15
+ false
16
+ end
17
+
18
+ def self.define_attribute( attribute_name, options = {} )
19
+
20
+ class_eval(
21
+ "def #{attribute_name}( *arguments ); " +
22
+ "@#{attribute_name} = arguments.first unless arguments.empty?; " +
23
+ "@#{attribute_name} || " +
24
+ ( options[ :default ].nil? ?
25
+ "nil" :
26
+ ( options[ :default ].is_a?( String ) ?
27
+ "'#{options[ :default ]}'" :
28
+ "#{options[ :default ]}" ) ) + ";" +
29
+ "end",
30
+ __FILE__,
31
+ __LINE__
32
+ )
33
+ end
34
+
35
+ # the api uri
36
+ define_attribute :api_uri, default: 'http://api-staging.bedrocketplatform.com/'
37
+
38
+ def from_hash( configuration )
39
+
40
+ configuration.each_pair do | name, value |
41
+ self.instance_variable_set( "@#{name}", value )
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,6 @@
1
+ module Boxxspring
2
+
3
+ class Error < RuntimeError
4
+ end
5
+
6
+ end
@@ -0,0 +1,23 @@
1
+ module Boxxspring
2
+
3
+ class Operation
4
+
5
+ def initialize( path )
6
+ @path = path
7
+ @parameters = {}
8
+ end
9
+
10
+ def query
11
+ result = nil
12
+ Boxxspring::Request.new.tap do | request |
13
+ request.get( @path ).tap do | response |
14
+ parser = Boxxspring::Parser.new( response.content )
15
+ result = parser.resources
16
+ end
17
+ end
18
+ result
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,101 @@
1
+ require 'pry'
2
+
3
+ module Boxxspring
4
+
5
+ class Parser
6
+
7
+ def initialize( content = {} )
8
+ @content = content
9
+ end
10
+
11
+ def name
12
+ @content.include?( '$this' ) ?
13
+ @content[ '$this' ][ 'name' ] :
14
+ nil
15
+ end
16
+
17
+ def type_name
18
+ @content.include?( '$this' ) ?
19
+ @content[ '$this' ][ 'name' ] :
20
+ nil
21
+ end
22
+
23
+ def key
24
+ 'id'
25
+ end
26
+
27
+ def keys
28
+ @content.include?( '$this' ) ?
29
+ @content[ '$this' ][ 'ids' ] :
30
+ nil
31
+ end
32
+
33
+ def associations
34
+ @content.include?( '$associations' ) ?
35
+ @content[ '$associations' ] :
36
+ nil
37
+ end
38
+
39
+ def resources
40
+ result = nil
41
+ unless self.name.blank?
42
+ result = self.keys.map { | key | self.resource_by( name, key ) }
43
+ end
44
+ result
45
+ end
46
+
47
+ def resource_by( name, key )
48
+ @resources_index ||= Hash.new { | hash, key | hash[ key ] = {} }
49
+ @resources_index[ name ][ key ] ||= begin
50
+ result = nil
51
+ resource_attributes = resource_attribute_index[ name ][ key ]
52
+ if resource_attributes.present?
53
+ type_name = resource_attributes.delete( 'type_name' ) || self.type_name
54
+ klass = Boxxspring.const_get( type_name.classify ) rescue nil
55
+ if klass.present?
56
+ result = klass.new(
57
+ resource_attributes,
58
+ self.resource_associations_by( name, key )
59
+ )
60
+ end
61
+ end
62
+ result
63
+ end
64
+ end
65
+
66
+ def resource_associations_by( name, key )
67
+ result = Hash.new { | hash, key | hash[ key ] = [] }
68
+ associations = self.associations
69
+ if associations && associations.include?( name )
70
+ associations = associations[ name ].detect do | association |
71
+ association[ 'id' ] == key
72
+ end
73
+ associations.each do | key, value |
74
+ unless key == 'id'
75
+ result[ key ] = value.map do | associated_id |
76
+ self.resource_by( key, associated_id )
77
+ end
78
+ result[ key ].compact!
79
+ end
80
+ end
81
+ end
82
+ result
83
+ end
84
+
85
+ def resource_attribute_index
86
+ @resource_attribute_index ||= begin
87
+ index = Hash.new { | hash, key | hash[ key ] = {} }
88
+ @content.each do | key, resources_attributes |
89
+ unless key[0] == '$'
90
+ resources_attributes.each do | resource_attributes |
91
+ index[ key ][ resource_attributes[ 'id' ] ] = resource_attributes
92
+ end
93
+ end
94
+ end
95
+ index
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,84 @@
1
+ require 'net/https'
2
+ require 'addressable/uri'
3
+
4
+ module Boxxspring
5
+
6
+ class Request
7
+
8
+ def initialize( default_parameters = {} )
9
+
10
+ # parse the API uri
11
+ uri = URI.parse( Boxxspring.configuration.api_uri )
12
+
13
+ # construct http request
14
+ @http = Net::HTTP.new( uri.host, uri.port )
15
+
16
+ # use ssl when https is the uri scheme
17
+ @http.use_ssl = ( uri.scheme == 'https' )
18
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
19
+
20
+ # retain the default parameters
21
+ @default_parameters = default_parameters.stringify_keys
22
+
23
+ end
24
+
25
+ def get( path, parameters = {} )
26
+
27
+ response = nil
28
+
29
+ begin
30
+
31
+ response =
32
+ Response.new( @http.get( compose_request_path( path, parameters ) ) )
33
+
34
+ rescue Timeout::Error
35
+ response = nil
36
+ end
37
+
38
+ response
39
+
40
+ end
41
+
42
+ def post( path, parameters = {} )
43
+
44
+ response = nil
45
+
46
+ begin
47
+
48
+ data = []
49
+
50
+ parameters.each_pair do | key, value |
51
+ data << "#{key.to_s}=#{value.to_s}"
52
+ end
53
+
54
+ response =
55
+ Response.new(
56
+ @http.post(
57
+ compose_request_path( path ),
58
+ data.join( '&' )
59
+ )
60
+ )
61
+
62
+ rescue Timeout::Error
63
+ response = nil
64
+ end
65
+
66
+ response
67
+
68
+ end
69
+
70
+ protected; def compose_request_path( path, parameters = {} )
71
+
72
+ parameters = @default_parameters.merge( parameters.stringify_keys )
73
+ addressable = Addressable::URI.new
74
+
75
+ addressable.path = path
76
+ addressable.query = parameters.to_param unless parameters.blank?
77
+
78
+ addressable.to_s
79
+
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,29 @@
1
+ module Boxxspring
2
+
3
+ class Artifact < Base
4
+
5
+ field :created_at
6
+ field :updated_at
7
+ field :edited_at
8
+ field :published_at
9
+ field :featured_at
10
+
11
+ field :status
12
+ field :original
13
+
14
+ field :type_name
15
+ field :id
16
+ field :name
17
+ field :short_description
18
+ field :description
19
+ field :note
20
+
21
+ field :picture_id
22
+
23
+ field :meta_description
24
+ field :meta_title
25
+ field :slug
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,6 @@
1
+ module Boxxspring
2
+
3
+ class AtomicVideoCard < VideoCard
4
+ end
5
+
6
+ end
@@ -0,0 +1,51 @@
1
+ module Boxxspring
2
+
3
+ class Base
4
+
5
+ def self.field( name, options = {} )
6
+
7
+ class_eval(
8
+ "def #{name}(); " +
9
+ "@#{name} || " +
10
+ ( options[ :default ].nil? ?
11
+ "nil" :
12
+ ( options[ :default ].is_a?( String ) ?
13
+ "'#{options[ :default ]}'" :
14
+ "#{options[ :default ]}" ) ) + ";" +
15
+ "end;" +
16
+ " " +
17
+ "attr_writer :#{name};",
18
+ __FILE__,
19
+ __LINE__
20
+ )
21
+
22
+ end
23
+
24
+ def self.has_one( name, options = {} )
25
+ define_method name do
26
+ associations = self.instance_variable_get( "@_#{name.to_s.pluralize}" )
27
+ associations.present? ? associations.first : options[ :default]
28
+ end
29
+ end
30
+
31
+ def self.has_many( name, options = {} )
32
+ define_method name do
33
+ self.instance_variable_get( "@_#{name}" ) || options[ :default ]
34
+ end
35
+ end
36
+
37
+ def initialize( attributes = {}, associations = {} )
38
+ attributes.symbolize_keys!
39
+ @attributes = attributes
40
+ attributes.dup.each do | key, value |
41
+ send( "#{key}=", value ) if respond_to?( "#{key}=" )
42
+ end
43
+ associations.each do | key, value |
44
+ self.instance_variable_set( "@_#{key}", value )
45
+ end
46
+ yield self if block_given?
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,23 @@
1
+ module Boxxspring
2
+
3
+ class Card < Base
4
+
5
+ field :created_at
6
+ field :updated_at
7
+
8
+ field :type_name
9
+ field :id
10
+ field :name
11
+ field :body
12
+
13
+ field :picture_id
14
+
15
+ field :provider
16
+ field :provider_uid
17
+ field :provider_url
18
+
19
+ field :properties
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,6 @@
1
+ module Boxxspring
2
+
3
+ class Error < RuntimeError
4
+ end
5
+
6
+ end
@@ -0,0 +1,14 @@
1
+ module Boxxspring
2
+
3
+ class Picture < Base
4
+
5
+ field :created_at
6
+ field :updated_at
7
+
8
+ field :id
9
+ field :code_name
10
+ field :filename
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,25 @@
1
+ module Boxxspring
2
+
3
+ class Property < Base
4
+
5
+ field :created_at
6
+ field :updated_at
7
+ field :destroyed_at
8
+
9
+ field :name
10
+ field :code_name
11
+ field :domain_name
12
+
13
+ field :meta_description
14
+ field :meta_title
15
+
16
+ field :authentication_enabled, default: false
17
+ field :authentication_username
18
+ field :authentication_password
19
+
20
+ has_many :pictures
21
+ has_one :theme
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,14 @@
1
+ module Boxxspring
2
+
3
+ class Service < Base
4
+
5
+ field :created_at
6
+ field :updated_at
7
+
8
+ field :id
9
+ field :provider
10
+ field :public_settings
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,15 @@
1
+ module Boxxspring
2
+
3
+ class Sponsor < Base
4
+
5
+ field :created_at
6
+ field :updated_at
7
+
8
+ field :id
9
+ field :name
10
+ field :picture_id
11
+ field :stories_count
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,16 @@
1
+ module Boxxspring
2
+
3
+ class Theme < Base
4
+
5
+ field :id
6
+ field :code_name
7
+ field :name
8
+
9
+ field :default_javascript_uri
10
+ field :default_stylesheet_uri
11
+
12
+ has_many :theme_environments
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,18 @@
1
+ module Boxxspring
2
+
3
+ class ThemeEnvironment < Base
4
+
5
+ field :created_at
6
+ field :updated_at
7
+
8
+ field :code_name
9
+ field :name
10
+ field :description
11
+
12
+ field :uri
13
+
14
+ has_one :user_agent
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,11 @@
1
+ module Boxxspring
2
+
3
+ class UserAgent < Base
4
+
5
+ field :id
6
+ field :name
7
+ field :pattern
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,6 @@
1
+ module Boxxspring
2
+
3
+ class VideoCard < Card
4
+ end
5
+
6
+ end
@@ -0,0 +1,54 @@
1
+ module Boxxspring
2
+
3
+ class Response
4
+
5
+ attr_reader :content
6
+ attr_reader :error
7
+ attr_reader :code
8
+
9
+ def initialize( http_response )
10
+
11
+ @code = http_response.code
12
+ @content = decode_response_body( http_response )
13
+ error = @content.respond_to?( :keys ) ? @content[ 'errors' ] : nil
14
+
15
+ if @content.respond_to?( :include? ) && @content.include?( 'errors' )
16
+ @error =
17
+ Boxxspring::Error.new( @content[ 'errors' ][ 'message' ] )
18
+ end
19
+
20
+ @success =
21
+ http_response.is_a?( Net::HTTPOK ) &&
22
+ @content.present? &&
23
+ @error.nil?
24
+
25
+ @error = Boxxspring::Error.new( 'unknown' ) \
26
+ if !@success && @error.nil?
27
+
28
+ end
29
+
30
+ def success?
31
+ @success
32
+ end
33
+
34
+ def failure?
35
+ not @success
36
+ end
37
+
38
+ protected; def decode_response_body( http_response )
39
+
40
+ response = nil
41
+
42
+ body = http_response.body;
43
+ unless body.nil? || body.empty?
44
+ response =
45
+ MultiJson.decode( http_response.body ) rescue nil
46
+ end
47
+
48
+ response
49
+
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,3 @@
1
+ module Boxxspring
2
+ VERSION = '2.0.0'
3
+ end
data/lib/boxxspring.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'active_support'
2
+ require 'active_support/all'
3
+ require 'multi_json'
4
+
5
+ require 'boxxspring/error'
6
+ require 'boxxspring/configuration'
7
+ require 'boxxspring/response'
8
+ require 'boxxspring/request'
9
+ require 'boxxspring/parser'
10
+ require 'boxxspring/operation'
11
+
12
+ require 'boxxspring/resources/base'
13
+ require 'boxxspring/resources/picture'
14
+ require 'boxxspring/resources/user_agent'
15
+ require 'boxxspring/resources/theme_environment'
16
+ require 'boxxspring/resources/theme'
17
+ require 'boxxspring/resources/card'
18
+ require 'boxxspring/resources/video_card'
19
+ require 'boxxspring/resources/atomic_video_card'
20
+ require 'boxxspring/resources/artifact'
21
+ require 'boxxspring/resources/property'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boxxspring
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kristoph Cichocki-Romanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.1
55
+ description: The boxxspring gem is implements a client to the Bedrocket Media Ventrures
56
+ Boxxspring property API.
57
+ email: kristoph@bedrocket.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/boxxspring.rb
63
+ - lib/boxxspring/configuration.rb
64
+ - lib/boxxspring/error.rb
65
+ - lib/boxxspring/operation.rb
66
+ - lib/boxxspring/parser.rb
67
+ - lib/boxxspring/request.rb
68
+ - lib/boxxspring/resources/artifact.rb
69
+ - lib/boxxspring/resources/atomic_video_card.rb
70
+ - lib/boxxspring/resources/base.rb
71
+ - lib/boxxspring/resources/card.rb
72
+ - lib/boxxspring/resources/error.rb
73
+ - lib/boxxspring/resources/picture.rb
74
+ - lib/boxxspring/resources/property.rb
75
+ - lib/boxxspring/resources/service.rb
76
+ - lib/boxxspring/resources/sponsor.rb
77
+ - lib/boxxspring/resources/theme.rb
78
+ - lib/boxxspring/resources/theme_environment.rb
79
+ - lib/boxxspring/resources/user_agent.rb
80
+ - lib/boxxspring/resources/video_card.rb
81
+ - lib/boxxspring/response.rb
82
+ - lib/boxxspring/version.rb
83
+ homepage: http://bedrocket.com
84
+ licenses:
85
+ - MS-RL
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.2.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Bedrocket Media Ventrures Boxxspring property API.
107
+ test_files: []