helix 0.0.1.2.pre → 0.0.1.3.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,8 @@ module Helix
10
10
  METHODS_DELEGATED_TO_CLASS = [ :guid_name, :media_type_sym, :plural_media_type ]
11
11
  end
12
12
 
13
- attr_accessor :attributes, :config
13
+ attr_accessor :attributes
14
+ attr_writer :config
14
15
 
15
16
  # Creates a new record via API and then returns an instance of that record.
16
17
  #
@@ -23,7 +24,6 @@ module Helix
23
24
  # @param [Hash] attributes a hash containing the attributes used in the create
24
25
  # @return [Base] An instance of Helix::Base
25
26
  def self.create(attributes={})
26
- config = Helix::Config.instance
27
27
  url = config.build_url(media_type: plural_media_type,
28
28
  format: :xml)
29
29
  response = RestClient.post(url, attributes.merge(signature: config.signature(:update)))
@@ -41,7 +41,6 @@ module Helix
41
41
  # @param [String] guid an id in guid form.
42
42
  # @return [Base] An instance of Helix::Base
43
43
  def self.find(guid)
44
- config = Helix::Config.instance
45
44
  item = self.new(attributes: { guid_name => guid }, config: config)
46
45
  item.load
47
46
  end
@@ -57,11 +56,10 @@ module Helix
57
56
  def self.find_all(opts)
58
57
  data_sets = get_data_sets(opts)
59
58
  return [] if data_sets.nil?
60
- data_sets.map { |attrs| self.new(attributes: attrs) }
59
+ data_sets.map { |attrs| self.new(attributes: attrs, config: config) }
61
60
  end
62
61
 
63
62
  def self.get_data_sets(opts)
64
- config = Helix::Config.instance
65
63
  url = config.build_url(format: :json)
66
64
  # We allow opts[:sig_type] for internal negative testing only.
67
65
  raw_response = config.get_response(url, {sig_type: :view}.merge(opts))
@@ -97,6 +95,14 @@ module Helix
97
95
  @config = opts[:config]
98
96
  end
99
97
 
98
+ def self.config
99
+ Helix::Config.instance
100
+ end
101
+
102
+ def config
103
+ @config ||= Helix::Config.instance
104
+ end
105
+
100
106
  # Deletes the record of the Helix::Base instance.
101
107
  #
102
108
  # @example
@@ -105,7 +111,6 @@ module Helix
105
111
  #
106
112
  # @return [String] The response from the HTTP DELETE call.
107
113
  def destroy
108
- config = Helix::Config.instance
109
114
  url = config.build_url(format: :xml, guid: guid, media_type: plural_media_type)
110
115
  RestClient.delete(url, params: {signature: config.signature(:update)})
111
116
  end
@@ -88,7 +88,7 @@ module Helix
88
88
  sig_type = opts.delete(:sig_type)
89
89
  params = opts.merge(signature: signature(sig_type, opts))
90
90
  response = RestClient.get(url, params: params)
91
- JSON.parse(response)
91
+ parse_response_by_url_format(response, url)
92
92
  end
93
93
 
94
94
  # Fetches the signature for a specific license key.
@@ -123,6 +123,19 @@ module Helix
123
123
  @credentials['license_key']
124
124
  end
125
125
 
126
+ def parse_response_by_url_format(response, url)
127
+ ### FIXME: This is ugly. Clean it up.
128
+ if url =~ /json/
129
+ JSON.parse(response)
130
+ elsif url =~ /xml/
131
+ Hash.from_xml(response)
132
+ elsif url =~ /csv/
133
+ response
134
+ else
135
+ raise "Could not parse #{url}"
136
+ end
137
+ end
138
+
126
139
  def prepare_signature_memoization
127
140
  lk = license_key
128
141
  @signature_for ||= {}
@@ -119,10 +119,12 @@ module Helix
119
119
 
120
120
  def self.delivery(media_type, opts)
121
121
  memo_cfg = Helix::Config.instance
122
+ format = opts.delete(:format)
122
123
  guid = opts.delete("#{media_type}_id".to_sym)
123
124
  url_opts = guid ?
124
125
  {guid: guid, media_type: "#{media_type}s".to_sym, action: :statistics} :
125
126
  {media_type: :statistics, action: "#{media_type}_delivery".to_sym}
127
+ url_opts.merge!(format: format) if format
126
128
  url = memo_cfg.build_url(url_opts)
127
129
  # We allow opts[:sig_type] for internal negative testing only.
128
130
  memo_cfg.get_response(url, {sig_type: :view}.merge(opts))
@@ -130,7 +132,9 @@ module Helix
130
132
 
131
133
  def self.storage(media_type, opts)
132
134
  memo_cfg = Helix::Config.instance
135
+ format = opts.delete(:format)
133
136
  url_opts = {media_type: :statistics, action: storage_action_for(media_type)}
137
+ url_opts.merge!(format: format) if format
134
138
  url = memo_cfg.build_url(url_opts)
135
139
  # We allow opts[:sig_type] for internal negative testing only.
136
140
  memo_cfg.get_response(url, {sig_type: :view}.merge(opts))
@@ -19,16 +19,16 @@ module Helix
19
19
  # Doc reference: /doc/api/video/import
20
20
  #
21
21
  # @example
22
- # video = Helix::Video.import(src: "www.google.com/video.mp4",
23
- # title: "Some Title,
22
+ # video = Helix::Video.import(src: "www.google.com/video.mp4",
23
+ # title: "Some Title,
24
24
  # description: "A random video.")
25
25
  # new_video.video_id # => dd891b83ba39e
26
26
  #
27
27
  # @param [Hash] attrs The attributes for creating a video.
28
28
  # @return [RestClient] The response object.
29
29
  def self.import(attrs={})
30
- RestClient.post(self.get_url,
31
- self.get_xml(attrs),
30
+ RestClient.post(self.get_url,
31
+ self.get_xml(attrs),
32
32
  self.get_params(self.extract_params(attrs)))
33
33
  end
34
34
 
@@ -50,9 +50,9 @@ module Helix
50
50
  # url.
51
51
  #
52
52
  #
53
- # @return [Hash]
53
+ # @return [Hash]
54
54
  def self.get_url_opts
55
- { action: :create_many,
55
+ { action: :create_many,
56
56
  media_type: plural_media_type,
57
57
  format: :xml }
58
58
  end
@@ -73,11 +73,11 @@ module Helix
73
73
  # Gets the hash used in adding the signature to the API
74
74
  # call.
75
75
  #
76
- # @return [Hash] Returns a formatted hash for passing in the signature to the API call.
76
+ # @return [Hash] Returns a formatted hash for passing in the signature to the API call.
77
77
  def self.get_params(opts={})
78
78
  opts = { contributor: :helix, library_id: :development }.merge(opts)
79
79
  sig = Helix::Config.instance.signature(:ingest, opts)
80
- { params: { signature: sig } }
80
+ { params: { signature: sig } }
81
81
  end
82
82
 
83
83
  end
@@ -24,6 +24,16 @@ describe Helix::Base do
24
24
 
25
25
  ### CLASS METHODS
26
26
 
27
+ describe ".config" do
28
+ let(:meth) { :config }
29
+ subject { klass.method(meth) }
30
+ its(:arity) { should eq(0) }
31
+ describe "when called" do
32
+ subject { klass.send(meth) }
33
+ it { should eq(Helix::Config.instance) }
34
+ end
35
+ end
36
+
27
37
  describe ".create" do
28
38
  let(:meth) { :create }
29
39
  let(:mock_config) { mock(Helix::Config) }
@@ -118,9 +128,9 @@ describe Helix::Base do
118
128
  let(:data_set) { (0..2).to_a }
119
129
  before(:each) do mock_config.stub(:get_response) { {plural_media_type => data_set } } end
120
130
  it "should map instantiation with attributes: each data set element" do
121
- klass.should_receive(:new).with(attributes: data_set[0]) { :a }
122
- klass.should_receive(:new).with(attributes: data_set[1]) { :b }
123
- klass.should_receive(:new).with(attributes: data_set[2]) { :c }
131
+ klass.should_receive(:new).with(attributes: data_set[0], config: mock_config) { :a }
132
+ klass.should_receive(:new).with(attributes: data_set[1], config: mock_config) { :b }
133
+ klass.should_receive(:new).with(attributes: data_set[2], config: mock_config) { :c }
124
134
  expect(klass.send(meth, opts)).to eq([:a, :b, :c])
125
135
  end
126
136
  end
@@ -132,6 +142,23 @@ describe Helix::Base do
132
142
 
133
143
  ### INSTANCE METHODS
134
144
 
145
+ describe ".config" do
146
+ let(:meth) { :config }
147
+ subject { obj.method(meth) }
148
+ its(:arity) { should eq(0) }
149
+ describe "when called" do
150
+ subject { obj.send(meth) }
151
+ context "and @config is already set" do
152
+ before(:each) do obj.instance_variable_set(:@config, :cached_val) end
153
+ it { should be(:cached_val) }
154
+ end
155
+ context "and @config is NOT already set" do
156
+ before(:each) do obj.instance_variable_set(:@config, nil) end
157
+ it { should eq(Helix::Config.instance) }
158
+ end
159
+ end
160
+ end
161
+
135
162
  describe "#destroy" do
136
163
  let(:meth) { :destroy }
137
164
  let(:mock_config) { mock(Helix::Config, build_url: :the_built_url, signature: :some_sig) }
@@ -273,15 +273,43 @@ describe Helix::Config do
273
273
  subject { obj.method(meth) }
274
274
  its(:arity) { should eq(-2) }
275
275
  context "when given a url and options" do
276
- let(:string) { String.new }
277
276
  let(:opts) { {sig_type: :the_sig_type} }
278
- let(:params) { { params: { signature: string } } }
277
+ let(:params) { { params: { signature: 'mock_sig' } } }
278
+ let(:returned_csv) { 'x,y,z' }
279
279
  let(:returned_json) { '{"key": "val"}' }
280
+ let(:returned_xml) { '<root><inner>inner value</inner></root>' }
280
281
  let(:json_parsed) { { "key" => "val" } }
281
- it "should call RestClient.get and return a hash from parsed JSON" do
282
- obj.stub(:signature).with(:the_sig_type, opts) { string }
283
- RestClient.should_receive(:get).with(string, params) { returned_json }
284
- expect(obj.send(meth, string, opts)).to eq(json_parsed)
282
+ let(:xml_parsed) { { "root" => { "inner" => "inner value" } } }
283
+ before(:each) do
284
+ obj.stub(:signature).with(:the_sig_type, opts) { 'mock_sig' }
285
+ end
286
+ context "and the URL matches /json/" do
287
+ let(:url) { 'blah.json' }
288
+ it "should call RestClient.get and return a hash from parsed JSON" do
289
+ RestClient.should_receive(:get).with(url, params) { returned_json }
290
+ expect(obj.send(meth, url, opts)).to eq(json_parsed)
291
+ end
292
+ end
293
+ context "and the URL matches /json/" do
294
+ let(:url) { 'blah.xml' }
295
+ it "should call RestClient.get and return a hash from parsed XML" do
296
+ RestClient.should_receive(:get).with(url, params) { returned_xml }
297
+ expect(obj.send(meth, url, opts)).to eq(xml_parsed)
298
+ end
299
+ end
300
+ context "and the URL matches /csv/" do
301
+ let(:url) { 'blah.csv' }
302
+ it "should call RestClient.get and return the raw CSV response" do
303
+ RestClient.should_receive(:get).with(url, params) { returned_csv }
304
+ expect(obj.send(meth, url, opts)).to eq(returned_csv)
305
+ end
306
+ end
307
+ context "and the URL matches none of /json/, /xml/, or /csv/" do
308
+ let(:url) { 'blah.yml' }
309
+ it "should raise an exception" do
310
+ RestClient.should_receive(:get).with(url, params) { returned_csv }
311
+ expect(lambda { obj.send(meth, url, opts) }).to raise_error
312
+ end
285
313
  end
286
314
  end
287
315
  end
@@ -38,13 +38,34 @@ describe Helix::Statistics do
38
38
  Helix::Config.should_receive(:instance) { mock_config }
39
39
  mod.send(meth, opts)
40
40
  end
41
+ it "should delete :format from opts" do
42
+ opts.stub(:delete)
43
+ opts.should_receive(:delete).with(:format) { "the_#{media_name}_id".to_sym }
44
+ mod.send(meth, opts)
45
+ end
41
46
  it "should delete :#{media_name}_id from opts" do
47
+ opts.stub(:delete)
42
48
  opts.should_receive(:delete).with("#{media_name}_id".to_sym) { "the_#{media_name}_id".to_sym }
43
49
  mod.send(meth, opts)
44
50
  end
45
- it "should call config.build_url(guid: the_#{media_name}_id, media_type: :#{media_name}s, action: :statistics)" do
46
- mock_config.should_receive(:build_url).with({guid: "the_#{media_name}_id".to_sym, media_type: "#{media_name}s".to_sym, action: :statistics}) { :built_url }
47
- mod.send(meth, opts)
51
+ context "when opts contains a :format" do
52
+ before(:each) do opts.merge!(format: :the_format) end
53
+ it "should call config.build_url(guid: the_#{media_name}_id, media_type: :#{media_name}s, action: :statistics, format: :the_format)" do
54
+ build_opts_url = {
55
+ guid: "the_#{media_name}_id".to_sym,
56
+ media_type: "#{media_name}s".to_sym,
57
+ action: :statistics,
58
+ format: :the_format
59
+ }
60
+ mock_config.should_receive(:build_url).with(build_opts_url) { :built_url }
61
+ mod.send(meth, opts)
62
+ end
63
+ end
64
+ context "when opts did NOT contain a :format" do
65
+ it "should call config.build_url(guid: the_#{media_name}_id, media_type: :#{media_name}s, action: :statistics)" do
66
+ mock_config.should_receive(:build_url).with({guid: "the_#{media_name}_id".to_sym, media_type: "#{media_name}s".to_sym, action: :statistics}) { :built_url }
67
+ mod.send(meth, opts)
68
+ end
48
69
  end
49
70
  it "should return config.get_response(built_url, opts.merge(sig_type: :view)" do
50
71
  mock_config.should_receive(:get_response).with(:built_url, {group: :daily, sig_type: :view}) { :response }
@@ -57,13 +78,29 @@ describe Helix::Statistics do
57
78
  Helix::Config.should_receive(:instance) { mock_config }
58
79
  mod.send(meth, opts)
59
80
  end
81
+ it "should delete :format from opts" do
82
+ opts.stub(:delete)
83
+ opts.should_receive(:delete).with(:format) { nil }
84
+ mod.send(meth, opts)
85
+ end
60
86
  it "should (fail to) delete :#{media_name}_id from opts" do
87
+ opts.stub(:delete)
61
88
  opts.should_receive(:delete).with("#{media_name}_id".to_sym) { nil }
62
89
  mod.send(meth, opts)
63
90
  end
64
- it "should call config.build_url(media_type: :statistics, action: :#{media_name}_delivery)" do
65
- mock_config.should_receive(:build_url).with({media_type: :statistics, action: "#{media_name}_delivery".to_sym}) { :built_url }
66
- mod.send(meth, opts)
91
+ context "when opts contains a :format" do
92
+ before(:each) do opts.merge!(format: :the_format) end
93
+ it "should call config.build_url(media_type: :statistics, action: :#{media_name}_delivery, format: :the_format)" do
94
+ build_url_opts = {media_type: :statistics, action: "#{media_name}_delivery".to_sym, format: :the_format}
95
+ mock_config.should_receive(:build_url).with(build_url_opts) { :built_url }
96
+ mod.send(meth, opts)
97
+ end
98
+ end
99
+ context "when opts did NOT contain a :format" do
100
+ it "should call config.build_url(media_type: :statistics, action: :#{media_name}_delivery)" do
101
+ mock_config.should_receive(:build_url).with({media_type: :statistics, action: "#{media_name}_delivery".to_sym}) { :built_url }
102
+ mod.send(meth, opts)
103
+ end
67
104
  end
68
105
  it "should return config.get_response(built_url, opts.merge(sig_type: :view)" do
69
106
  mock_config.should_receive(:get_response).with(:built_url, {group: :daily, sig_type: :view}) { :response }
metadata CHANGED
@@ -1,112 +1,104 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: helix
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1.2.pre
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease: 8
5
+ version: 0.0.1.3.pre
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Twistage, Inc
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-28 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2012-11-29 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
15
16
  name: json
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 1.5.4
22
- type: :runtime
23
17
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
18
+ requirement: &id001 !ruby/object:Gem::Requirement
25
19
  none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
29
23
  version: 1.5.4
30
- - !ruby/object:Gem::Dependency
31
- name: rest-client
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: 1.6.7
38
24
  type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rest-client
39
28
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
29
+ requirement: &id002 !ruby/object:Gem::Requirement
41
30
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
45
34
  version: 1.6.7
46
- - !ruby/object:Gem::Dependency
47
- name: activesupport
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: 3.0.9
54
35
  type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: activesupport
55
39
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
40
+ requirement: &id003 !ruby/object:Gem::Requirement
57
41
  none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
61
45
  version: 3.0.9
46
+ type: :runtime
47
+ version_requirements: *id003
62
48
  description: Provides helper libraries for Ruby access to the Twistage API
63
49
  email: kbaird@twistage.com
64
50
  executables: []
51
+
65
52
  extensions: []
53
+
66
54
  extra_rdoc_files: []
67
- files:
55
+
56
+ files:
68
57
  - lib/helix.rb
58
+ - lib/helix/album.rb
69
59
  - lib/helix/track.rb
70
- - lib/helix/video.rb
60
+ - lib/helix/base.rb
71
61
  - lib/helix/statistics.rb
72
- - lib/helix/image.rb
73
- - lib/helix/album.rb
62
+ - lib/helix/video.rb
74
63
  - lib/helix/config.rb
75
- - lib/helix/base.rb
76
- - spec/video_spec.rb
77
- - spec/album_spec.rb
78
- - spec/config_spec.rb
64
+ - lib/helix/image.rb
79
65
  - spec/base_spec.rb
80
66
  - spec/statistics_spec.rb
81
- - spec/spec_helper.rb
67
+ - spec/config_spec.rb
82
68
  - spec/track_spec.rb
83
69
  - spec/image_spec.rb
70
+ - spec/spec_helper.rb
71
+ - spec/video_spec.rb
72
+ - spec/album_spec.rb
84
73
  - LICENSE
85
74
  - README.md
86
75
  homepage: https://github.com/Twistage/helix/
87
- licenses:
76
+ licenses:
88
77
  - 3-Clause BSD
89
78
  post_install_message:
90
79
  rdoc_options: []
91
- require_paths:
80
+
81
+ require_paths:
92
82
  - lib
93
- required_ruby_version: !ruby/object:Gem::Requirement
83
+ required_ruby_version: !ruby/object:Gem::Requirement
94
84
  none: false
95
- requirements:
96
- - - ! '>='
97
- - !ruby/object:Gem::Version
98
- version: '0'
99
- required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
90
  none: false
101
- requirements:
102
- - - ! '>'
103
- - !ruby/object:Gem::Version
91
+ requirements:
92
+ - - ">"
93
+ - !ruby/object:Gem::Version
104
94
  version: 1.3.1
105
95
  requirements: []
96
+
106
97
  rubyforge_project:
107
98
  rubygems_version: 1.8.24
108
99
  signing_key:
109
100
  specification_version: 3
110
101
  summary: Wrapper library for the video API
111
102
  test_files: []
103
+
112
104
  has_rdoc: true