web-connect 0.1.2 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46c01a037f963376c2475ba338353470e4927227
4
- data.tar.gz: 8abec4e3235754d5b02bbfbd1314c42fba11a4d9
3
+ metadata.gz: 7f2d79d7e04cd133c4f3ac7ebfec21fa50742650
4
+ data.tar.gz: c8db33715f544a85a17cb89e5a1be90d6bdf5f2b
5
5
  SHA512:
6
- metadata.gz: 335be258f02a2d6dd1af9a7a51f83072f97ee145b8d26030b8940ee0e35326f4bcf98de1134b475f198297edc0a53f30de1bdc4c95c1109f697fc8f0f10a84f2
7
- data.tar.gz: 02025fc9226f02c71bd60950adf7ef870d6dc4b9041156d64a16d41bd71e91e846a7affc76ccd9f845b39df5e44ca70866954d2a0062ab309a99754a48086e32
6
+ metadata.gz: ab76e029cfa6d7d95e7490bd87c87de34872e40ac8f4153f49214035991880d3f757e60d56ccaad0d172ed0c5cd42c494ab6b9a87ae36b6c4ceb620d5ffd01d0
7
+ data.tar.gz: bb3cee43adee9ac75af954f63e4ef4649764000ab2ce6a2d6d82cf49367377efc190ab35f08de2192e9b34fd5cfb88e4e29ace99edce3e217644fd389a1da8c1
@@ -91,10 +91,10 @@ class Alpha < Netfira::WebConnect::Migration
91
91
  create_file_table :images do |t|
92
92
  t.float :focus_point_top
93
93
  t.float :focus_point_left
94
- t.integer :size
95
94
  t.integer :width
96
95
  t.integer :height
97
96
  end
97
+
98
98
  create_file_table :attachments
99
99
 
100
100
  create_relation_table :products, :images
@@ -52,8 +52,10 @@ module Netfira::WebConnect
52
52
  create_table table_name do |t|
53
53
  t.references :shop
54
54
  t.string :file_name, index: true
55
+ t.string :remote_location
55
56
  t.string :locale
56
57
  yield t if block_given?
58
+ t.integer :size
57
59
  t.binary :checksum, limit: 16
58
60
  t.binary :digest, limit: 16
59
61
  t.timestamps
@@ -1,4 +1,5 @@
1
1
  require 'paperclip'
2
+ require 'open-uri'
2
3
 
3
4
  Paperclip.interpolates :shop_id do |record, x|
4
5
  record.instance.shop_id
@@ -24,6 +25,19 @@ module Netfira::WebConnect
24
25
 
25
26
  end
26
27
 
28
+ class Validator < ActiveModel::Validator
29
+ def self.remote_file_sources_filter
30
+ @remote_file_sources_filter ||= Regexp.new(ENV['REMOTE_FILE_SOURCES_FILTER'] || '^https?://', Regexp::IGNORECASE)
31
+ end
32
+ def validate(record)
33
+ if record.remote_location && !(record.remote_location =~ self.class.remote_file_sources_filter)
34
+ record.errors[:remote_location] << 'Unacceptable format'
35
+ end
36
+ end
37
+ end
38
+
39
+ validates_with Validator
40
+
27
41
  after_initialize :set_file_defaults
28
42
 
29
43
  def <<(input)
@@ -41,14 +55,19 @@ module Netfira::WebConnect
41
55
 
42
56
  def checksum=(value)
43
57
  @checksum = nil
44
- self[:checksum] = Checksum === value ? value.b : value
58
+ self[:checksum] = normalize_checksum(value)
45
59
  end
46
60
 
47
61
  def data
48
- attachment.present? ? Paperclip.io_adapters.for(attachment) : nil
62
+ @stream ||= if remote_location
63
+ open remote_location.sub(/^file:/, '')
64
+ elsif attachment.present?
65
+ Paperclip.io_adapters.for attachment
66
+ end
49
67
  end
50
68
 
51
69
  def data=(value)
70
+ @stream = nil
52
71
  self.attachment = value
53
72
  value.rewind
54
73
  self.checksum = Checksum.of(value.read)
@@ -75,5 +94,20 @@ module Netfira::WebConnect
75
94
  def set_file_defaults
76
95
  self.locale ||= shop.locale
77
96
  end
97
+
98
+ def normalize_checksum(value)
99
+ if Checksum === value
100
+ value.b
101
+ elsif String === value
102
+ case value.length
103
+ when 22 then OctaWord.from_base64(value).b
104
+ when 32 then OctaWord.from_hex(value).b
105
+ else value
106
+ end
107
+ else
108
+ value
109
+ end
110
+ end
111
+
78
112
  end
79
113
  end
@@ -1,15 +1,15 @@
1
+ require 'ostruct'
2
+
1
3
  module Netfira::WebConnect
2
4
  class Models::Setting < Model::Support
3
5
 
4
6
  belongs_to :shop, inverse_of: :setting_models
5
7
 
8
+ around_save :dispatch_change_event
9
+ before_destroy :dispatch_destroy_event
10
+
6
11
  def typed_value
7
- case content_type
8
- when 'application/json' then JSON.parse(value, quirks_mode: true)
9
- when 'application/octet-stream' then value.b
10
- when /^text\/plain;charset=(.+)$/ then value.force_encoding($1)
11
- else value
12
- end
12
+ cast_by_content_type value, content_type
13
13
  end
14
14
 
15
15
  def typed_value=(value)
@@ -27,5 +27,30 @@ module Netfira::WebConnect
27
27
  end
28
28
  end
29
29
 
30
+ private
31
+
32
+ def cast_by_content_type(value, content_type)
33
+ case content_type
34
+ when 'application/json' then JSON.parse(value, quirks_mode: true)
35
+ when 'application/octet-stream' then value.b
36
+ when /^text\/plain;charset=(.+)$/ then value.force_encoding($1)
37
+ else value
38
+ end
39
+ end
40
+
41
+ def dispatch_change_event
42
+ old = persisted? ? cast_by_content_type(changed_attributes['value'], changed_attributes['content_type']) : nil
43
+ yield
44
+ new = OpenStruct.new(shop: shop, key: key, value: typed_value)
45
+ Netfira::WebConnect.dispatch_event 'change_setting', new, old unless old == new.value
46
+ end
47
+
48
+ def dispatch_destroy_event
49
+ return unless persisted?
50
+ new = OpenStruct.new(shop: shop, key: key, value: nil)
51
+ old = typed_value
52
+ Netfira::WebConnect.dispatch_event 'change_setting', new, old unless old.nil?
53
+ end
54
+
30
55
  end
31
56
  end
@@ -20,13 +20,15 @@ module Netfira::WebConnect
20
20
  attributes.each do |key, value|
21
21
  key = key.underscore
22
22
  next if key == origin_key
23
- if key == 'checksum' && klass < Model::Record::FileRecord
23
+ if key == 'checksum' && klass < Model::Record::FileRecord && !attributes['remoteLocation']
24
24
  request_file klass, attributes['fileName'] if record.checksum != value
25
25
  next
26
26
  end
27
27
  record.write_with_type_casting(key.to_sym, value) or raise BadRequest, "#{klass.name.demodulize.pluralize} don't have #{key} fields"
28
28
  end
29
- record.save or raise InternalServerError, record.errors
29
+ record.save or
30
+ raise BadRequest.new "#{klass.name.demodulize} record could not be saved",
31
+ errors: record.errors.messages.map { |k, v| [k.to_s.camelize(:lower), v] }.to_h
30
32
  complete_record record
31
33
  end
32
34
 
@@ -36,12 +36,8 @@ class Netfira::WebConnect::RackApp
36
36
  def get_single(key)
37
37
  setting = shop.setting_models.find_by(key: key)
38
38
  raise NotFound unless setting
39
- # if setting.content_type == 'application/json'
40
- # JSON.parse(setting.value, quirks_mode: true)
41
- # else
42
- header 'Content-Type', setting.content_type
43
- setting.value
44
- # end
39
+ header 'Content-Type', setting.content_type
40
+ setting.value
45
41
  end
46
42
 
47
43
  def delete_single(key)
@@ -13,13 +13,26 @@ module Netfira::WebConnect::RackApp::Exceptions
13
13
 
14
14
  def_delegators :'self.class', :status, :code, :category
15
15
 
16
- def initialize(*args, &block)
17
- super *args, &block
16
+ def initialize(message = nil, details = nil)
17
+ super message
18
18
  @headers = self.class.headers.dup
19
+ @details = details
20
+ end
21
+
22
+ def body
23
+ @body ||= make_body
19
24
  end
20
25
 
21
26
  attr_reader :headers
22
27
 
28
+ private
29
+
30
+ def make_body
31
+ body = {errorMessage: message}
32
+ body.merge! @details if Hash === @details
33
+ body
34
+ end
35
+
23
36
  end
24
37
 
25
38
  module HttpExceptions
@@ -11,7 +11,7 @@ module Netfira::WebConnect
11
11
  make_response 200, body, headers
12
12
  end
13
13
  rescue Exceptions::HttpException => e
14
- make_response e.code, {errorMessage: e.message}, e.headers, e.code
14
+ make_response e.code, e.body, e.headers, e.code
15
15
  end
16
16
  end
17
17
 
@@ -1,5 +1,5 @@
1
1
  module Netfira
2
2
  module WebConnect
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-04 00:00:00.000000000 Z
12
+ date: 2014-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord