helix 0.0.1.4.pre → 0.0.1.5.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
3
  require 'yaml'
4
- require 'active_support/core_ext'
4
+ require 'nori'
5
5
 
6
6
  module Helix
7
7
  class Base
@@ -60,7 +60,8 @@ module Helix
60
60
  end
61
61
 
62
62
  def self.get_data_sets(opts)
63
- url = config.build_url(format: :json)
63
+ url = config.build_url(format: opts[:format] || :json,
64
+ media_type: self.plural_media_type)
64
65
  # We allow opts[:sig_type] for internal negative testing only.
65
66
  raw_response = config.get_response(url, {sig_type: :view}.merge(opts))
66
67
  data_sets = raw_response[plural_media_type]
@@ -128,7 +128,10 @@ module Helix
128
128
  if url =~ /json/
129
129
  JSON.parse(response)
130
130
  elsif url =~ /xml/
131
- Hash.from_xml(response)
131
+ #TODO: Cleanup Nori and response gsub.
132
+ Nori.parser = :nokogiri
133
+ xml = response.gsub(/<custom-fields type='array'>/, '<custom-fields type=\'hash\'>')
134
+ Nori.parse(xml)
132
135
  elsif url =~ /csv/
133
136
  response
134
137
  else
@@ -11,7 +11,68 @@ module Helix
11
11
  #
12
12
  # @return [Symbol] Name of the class.
13
13
  def self.media_type_sym; :track; end
14
+
15
+ # Used to import tracks from a URL into the Twistage system.
16
+ # Doc reference: /doc/api/track/import
17
+ #
18
+ # @example
19
+ # track = Helix::Track.import(src: "www.google.com/track.mp4",
20
+ # title: "Some Title,
21
+ # description: "A random track.")
22
+ # new_track.track_id # => dd891b83ba39e
23
+ #
24
+ # @param [Hash] attrs The attributes for creating a track.
25
+ # @return [RestClient] The response object.
26
+ def self.import(attrs={})
27
+ #TODO: Pull shared logic (with video) into a shared lib file.
28
+ RestClient.post(self.get_url,
29
+ self.get_xml(attrs),
30
+ self.get_params(attrs))
31
+ end
32
+
33
+ private
34
+
35
+ # Method allows for :use_raw_xml to be passed into attributes.
36
+ # Normally attributes would be converted to xml, but use_raw_xml
37
+ # takes raw xml as an argument. Allowing for xml files to be
38
+ # used in place of attributes.
39
+ #
40
+ # @param [Hash] attrs The attributes for creating xml.
41
+ # @return [String] Returns xml either from a raw entry or generated from attributes.
42
+ def self.get_xml(attrs={})
43
+ return attrs[:use_raw_xml] if attrs[:use_raw_xml].present?
44
+ { list: { entry: attrs } }.to_xml(root: :add)
45
+ end
14
46
 
47
+ # Standard hash values used to generate the create_many
48
+ # url.
49
+ #
50
+ # @return [Hash]
51
+ def self.get_url_opts
52
+ { action: :create_many,
53
+ media_type: plural_media_type,
54
+ format: :xml }
55
+ end
56
+
57
+ # Gets the url used in the create_many import call.
58
+ #
59
+ # @return [String] Returns the valid url used for the API call.
60
+ def self.get_url
61
+ Helix::Config.instance.build_url(self.get_url_opts)
62
+ end
63
+
64
+ # Gets the hash used in adding the signature to the API
65
+ # call.
66
+ #
67
+ # @return [Hash] Returns a formatted hash for passing in the signature to the API call.
68
+ def self.get_params(opts={})
69
+ opts = { contributor: :helix, library_id: :development }.merge(opts)
70
+ sig = Helix::Config.instance.signature(:ingest, opts)
71
+ #TODO: Find a better way to handle all the different params needed for a call, such as
72
+ #attributes vs signature params vs process params. :url params is a temp fix.
73
+ url_params = (opts[:url_params].nil? ? {} : opts[:url_params])
74
+ { params: (params = { signature: sig }.merge(url_params)) }
75
+ end
15
76
  end
16
77
 
17
78
  end
@@ -29,7 +29,7 @@ module Helix
29
29
  def self.import(attrs={})
30
30
  RestClient.post(self.get_url,
31
31
  self.get_xml(attrs),
32
- self.get_params(self.extract_params(attrs)))
32
+ self.get_params(attrs))
33
33
  end
34
34
 
35
35
  private
@@ -49,7 +49,6 @@ module Helix
49
49
  # Standard hash values used to generate the create_many
50
50
  # url.
51
51
  #
52
- #
53
52
  # @return [Hash]
54
53
  def self.get_url_opts
55
54
  { action: :create_many,
@@ -64,21 +63,17 @@ module Helix
64
63
  Helix::Config.instance.build_url(self.get_url_opts)
65
64
  end
66
65
 
67
- def self.extract_params(attrs)
68
- [:contributor, :library_id].each_with_object({}) do |param, hash|
69
- hash[param] = attrs[param] unless attrs[param].nil?
70
- end
71
- end
72
-
73
66
  # Gets the hash used in adding the signature to the API
74
67
  # call.
75
68
  #
76
69
  # @return [Hash] Returns a formatted hash for passing in the signature to the API call.
77
70
  def self.get_params(opts={})
78
- opts = { contributor: :helix, library_id: :development }.merge(opts)
79
- sig = Helix::Config.instance.signature(:ingest, opts)
80
- { params: { signature: sig } }
71
+ opts = { contributor: :helix, library_id: :development }.merge(opts)
72
+ sig = Helix::Config.instance.signature(:ingest, opts)
73
+ #TODO: Find a better way to handle all the different params needed for a call, such as
74
+ #attributes vs signature params vs process params. :url params is a temp fix.
75
+ url_params = (opts[:url_params].nil? ? {} : opts[:url_params])
76
+ { params: (params = { signature: sig }.merge(url_params)) }
81
77
  end
82
-
83
78
  end
84
79
  end
@@ -108,7 +108,8 @@ describe Helix::Base do
108
108
  let(:plural_media_type) { :videos }
109
109
  before(:each) do klass.stub(:plural_media_type) { plural_media_type } end
110
110
  it "should build a JSON URL -> the_url" do
111
- mock_config.should_receive(:build_url).with(format: :json)
111
+ mock_config.should_receive(:build_url).with(format: :json,
112
+ media_type: plural_media_type)
112
113
  klass.send(meth, opts)
113
114
  end
114
115
  it "should get_response(the_url, {sig_type: :view}.merge(opts) -> raw_response" do
@@ -2,20 +2,99 @@ require File.expand_path('../spec_helper', __FILE__)
2
2
  require 'helix'
3
3
 
4
4
  describe Helix::Track do
5
+
6
+ def import_xml(values={})
7
+ { list: { entry: values } }.to_xml(root: :add)
8
+ end
9
+
5
10
  let(:klass) { Helix::Track }
6
11
 
7
- subject { klass }
8
- its(:ancestors) { should include(Helix::Base) }
9
- its(:guid_name) { should eq('track_id') }
12
+ subject { klass }
13
+ its(:ancestors) { should include(Helix::Base) }
14
+ its(:guid_name) { should eq('track_id') }
10
15
  its(:media_type_sym) { should be(:track) }
11
16
  its(:plural_media_type) { should eq('tracks') }
12
17
 
13
18
  describe "Constants"
14
19
 
20
+ let(:sig_opts) { { contributor: :helix,
21
+ library_id: :development } }
22
+ let(:url_opts) { { action: :create_many,
23
+ media_type: "tracks",
24
+ format: :xml } }
25
+
15
26
  describe "an instance" do
16
- let(:obj) { klass.new({'track_id' => 'some_track_guid'}) }
17
- subject { obj }
27
+ let(:obj) { klass.new({'track_id' => 'some_track_guid'}) }
28
+ subject { obj }
18
29
  its(:media_type_sym) { should be(:track) }
19
30
  end
20
31
 
32
+ describe ".import" do
33
+ let(:meth) { :import }
34
+ let(:mock_config) { mock(Helix::Config) }
35
+ subject { klass.method(meth) }
36
+ its(:arity) { should eq(-1) }
37
+ let(:params) { { params: { signature: :some_sig } } }
38
+ before { Helix::Config.stub(:instance) { mock_config } }
39
+
40
+ it "should get an ingest signature" do
41
+ mock_config.should_receive(:build_url).with(url_opts)
42
+ mock_config.should_receive(:signature).with(:ingest, sig_opts) { :some_sig }
43
+ RestClient.should_receive(:post).with(nil, import_xml, params)
44
+ klass.send(meth)
45
+ end
46
+ end
47
+
48
+ describe ".get_xml" do
49
+ let(:meth) { :get_xml }
50
+ subject { klass.method(meth) }
51
+ its(:arity) { should eq(-1) }
52
+ context "when :use_raw_xml is present in attrs" do
53
+ let(:use_raw_xml) { { use_raw_xml: :xml } }
54
+ it "should return the value of attrs[:use_raw_xml]" do
55
+ expect(klass.send(meth, use_raw_xml)).to eq(:xml)
56
+ end
57
+ end
58
+ context "when hash is passed without :use_raw_xml" do
59
+ let(:attrs) { { attribute: :value } }
60
+ it "should convert attrs into xml" do
61
+ expect(klass.send(meth, attrs)).to eq(import_xml(attrs))
62
+ end
63
+ end
64
+ context "when nothing in passed in" do
65
+ it "should return valid xml" do
66
+ expect(klass.send(meth)).to eq(import_xml)
67
+ end
68
+ end
69
+ end
70
+
71
+ describe ".get_url_opts" do
72
+ let(:meth) { :get_url_opts }
73
+ subject { klass.method(meth) }
74
+ its(:arity) { should eq(0) }
75
+ it "should return a valid hash url options for Helix::Config#build_url" do
76
+ expect(klass.send(meth)).to eq(url_opts)
77
+ end
78
+ end
79
+
80
+ describe ".get_url" do
81
+ let(:meth) { :get_url }
82
+ subject { klass.method(meth) }
83
+ its(:arity) { should eq(0) }
84
+ it "should call Helix::Config#build_url with url opts" do
85
+ Helix::Config.instance.should_receive(:build_url).with(klass.send(:get_url_opts))
86
+ klass.send(meth)
87
+ end
88
+ end
89
+
90
+ describe ".get_params" do
91
+ let(:meth) { :get_params }
92
+ subject { klass.method(meth) }
93
+ its(:arity) { should eq(-1) }
94
+ it "should call Helix::Config#signature and return a hash of params" do
95
+ Helix::Config.instance.should_receive(:signature).with(:ingest, sig_opts) { :sig }
96
+ expect(klass.send(meth)).to eq({ params: { signature: :sig } })
97
+ end
98
+ end
99
+
21
100
  end
@@ -96,18 +96,4 @@ describe Helix::Video do
96
96
  expect(klass.send(meth)).to eq({ params: { signature: :sig } })
97
97
  end
98
98
  end
99
-
100
- describe ".extract_params" do
101
- let(:meth) { :extract_params }
102
- subject { klass.method(meth) }
103
- its(:arity) { should eq(1) }
104
- let(:expected_hash) { { contributor: :con, library_id: :id } }
105
- let(:attrs) { { extra_key_one: :one,
106
- extra_key_two: :two }.merge(expected_hash) }
107
- it "should return the correct key values from attributes" do
108
- expect(klass.send(meth, attrs)).to eq(expected_hash)
109
- end
110
- end
111
-
112
-
113
99
  end
metadata CHANGED
@@ -1,104 +1,112 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: helix
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.5.pre
4
5
  prerelease: 8
5
- version: 0.0.1.4.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
-
13
- date: 2012-12-03 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: json
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
23
21
  version: 1.5.4
24
22
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: rest-client
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
30
25
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ 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
34
37
  version: 1.6.7
35
38
  type: :runtime
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: activesupport
39
39
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: nori
48
+ requirement: !ruby/object:Gem::Requirement
41
49
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: 3.0.9
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.1.3
46
54
  type: :runtime
47
- version_requirements: *id003
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.3
48
62
  description: Provides helper libraries for Ruby access to the Twistage API
49
63
  email: kbaird@twistage.com
50
64
  executables: []
51
-
52
65
  extensions: []
53
-
54
66
  extra_rdoc_files: []
55
-
56
- files:
67
+ files:
57
68
  - lib/helix.rb
58
69
  - lib/helix/album.rb
59
- - lib/helix/track.rb
60
70
  - lib/helix/base.rb
61
- - lib/helix/statistics.rb
62
- - lib/helix/video.rb
63
- - lib/helix/config.rb
64
71
  - lib/helix/image.rb
65
- - spec/base_spec.rb
72
+ - lib/helix/config.rb
73
+ - lib/helix/video.rb
74
+ - lib/helix/track.rb
75
+ - lib/helix/statistics.rb
76
+ - spec/video_spec.rb
77
+ - spec/spec_helper.rb
66
78
  - spec/statistics_spec.rb
67
79
  - spec/config_spec.rb
68
- - spec/track_spec.rb
69
80
  - spec/image_spec.rb
70
- - spec/spec_helper.rb
71
- - spec/video_spec.rb
72
81
  - spec/album_spec.rb
82
+ - spec/track_spec.rb
83
+ - spec/base_spec.rb
73
84
  - LICENSE
74
85
  - README.md
75
86
  homepage: https://github.com/Twistage/helix/
76
- licenses:
87
+ licenses:
77
88
  - 3-Clause BSD
78
89
  post_install_message:
79
90
  rdoc_options: []
80
-
81
- require_paths:
91
+ require_paths:
82
92
  - lib
83
- required_ruby_version: !ruby/object:Gem::Requirement
93
+ required_ruby_version: !ruby/object:Gem::Requirement
84
94
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- version: "0"
89
- required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
100
  none: false
91
- requirements:
92
- - - ">"
93
- - !ruby/object:Gem::Version
101
+ requirements:
102
+ - - ! '>'
103
+ - !ruby/object:Gem::Version
94
104
  version: 1.3.1
95
105
  requirements: []
96
-
97
106
  rubyforge_project:
98
107
  rubygems_version: 1.8.24
99
108
  signing_key:
100
109
  specification_version: 3
101
110
  summary: Wrapper library for the video API
102
111
  test_files: []
103
-
104
112
  has_rdoc: true