flix 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,11 +2,18 @@
2
2
  require File.expand_path('../lib/flix/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
+ gem.add_dependency 'faraday', '~> 0.8'
6
+ gem.add_dependency 'faraday_middleware', '~> 0.8'
7
+ gem.add_dependency 'multi_json', '~> 1.3'
8
+ gem.add_dependency 'simple_oauth', '~> 0.1.6' #not sure if this is needed
9
+
10
+ gem.add_development_dependency 'json'
11
+
5
12
  gem.authors = ["Andrew Gertig"]
6
13
  gem.email = ["andrew@otherscreen.com"]
7
- gem.description = %q{API wrapper}
8
- gem.summary = %q{Walking through the process of creating an API client}
9
- gem.homepage = ""
14
+ gem.description = %q{A Ruby wrapper for the API}
15
+ gem.summary = %q{API client}
16
+ gem.homepage = "https://github.com/AndrewGertig/flix"
10
17
 
11
18
  gem.files = `git ls-files`.split($\)
12
19
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -1,5 +1,33 @@
1
- require "flix/version"
1
+ # require "flix/version"
2
+ require 'flix/client'
3
+ require 'flix/configurable'
2
4
 
3
5
  module Flix
4
- # Your code goes here...
6
+ class << self
7
+ include Flix::Configurable
8
+
9
+ # Delegate to a Flix::Client
10
+ #
11
+ # @return [Flix::Client]
12
+ def client
13
+ if @client && @client.cache_key == options.hash
14
+ @client
15
+ else
16
+ @client = Flix::Client.new(options)
17
+ end
18
+ end
19
+
20
+ def respond_to_missing?(method_name, include_private=false); client.respond_to?(method_name, include_private); end if RUBY_VERSION >= "1.9"
21
+ def respond_to?(method_name, include_private=false); client.respond_to?(method_name, include_private) || super; end if RUBY_VERSION < "1.9"
22
+
23
+ private
24
+
25
+ def method_missing(method_name, *args, &block)
26
+ return super unless client.respond_to?(method_name)
27
+ client.send(method_name, *args, &block)
28
+ end
29
+
30
+ end
5
31
  end
32
+
33
+ Flix.setup
@@ -0,0 +1,58 @@
1
+ # require 'twitter/user'
2
+
3
+ module Twitter
4
+ module API
5
+
6
+ # def user(*args)
7
+ # options = args.extract_options!
8
+ # if user = args.pop
9
+ # options.merge_user!(user)
10
+ # object_from_response(Twitter::User, :get, "/1/users/show.json", options)
11
+ # end
12
+ # end
13
+
14
+ def user(*args)
15
+ options = args.extract_options!
16
+ if netflix_uid = args.pop
17
+ client = shared_client(user)
18
+ url = "/users/#{netflix_uid}"
19
+ # puts "get this URL #{url}"
20
+
21
+ response = from_response(:get, url, {output: "json"}, options)
22
+
23
+ # response = client.get do |req|
24
+ # req.url url
25
+ # req.params['output'] = "json"
26
+ # end
27
+
28
+ puts "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
29
+ puts "RECEIVED FROM NETFLIX"
30
+ puts "-------------HEADERS-----------------"
31
+ puts "#{response.headers}"
32
+ puts "----------------BODY--------------"
33
+ puts "#{response.body}"
34
+ puts "----------------------------------"
35
+
36
+ return response.body
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # # @param klass [Class]
43
+ # # @param request_method [Symbol]
44
+ # # @param url [String]
45
+ # # @param params [Hash]
46
+ # # @param options [Hash]
47
+ # # @return [Object]
48
+ # def object_from_response(klass, request_method, url, params={}, options={})
49
+ # response = send(request_method.to_sym, url, params, options)
50
+ # klass.from_response(response)
51
+ # end
52
+
53
+ def from_response(request_method, url, params={}, options={})
54
+ response = send(request_method.to_sym, url, params, options)
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,103 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'flix/configurable'
4
+
5
+ require 'multi_json'
6
+ require 'flix/api'
7
+ # require 'twitter/error/client_error'
8
+ # require 'twitter/error/decode_error'
9
+ # require 'twitter/rate_limit'
10
+ require 'simple_oauth'
11
+ require 'uri'
12
+ module Flix
13
+
14
+ class Client
15
+ include Flix::API
16
+ include Flix::Configurable
17
+
18
+ # Initializes a new Client object
19
+ #
20
+ # @param options [Hash]
21
+ # @return [Flix::Client]
22
+ def initialize(options={})
23
+ Flix::Configurable.keys.each do |key|
24
+ instance_variable_set(:"@#{key}", options[key] || Flix.instance_variable_get(:"@#{key}"))
25
+ end
26
+ end
27
+
28
+ # Perform an HTTP DELETE request
29
+ def delete(path, params={}, options={})
30
+ request(:delete, path, params, options)
31
+ end
32
+
33
+ # Perform an HTTP GET request
34
+ def get(path, params={}, options={})
35
+ request(:get, path, params, options)
36
+ end
37
+
38
+ # Perform an HTTP POST request
39
+ def post(path, params={}, options={})
40
+ request(:post, path, params, options)
41
+ end
42
+
43
+ # Perform an HTTP UPDATE request
44
+ def put(path, params={}, options={})
45
+ request(:put, path, params, options)
46
+ end
47
+
48
+ private
49
+
50
+ # Returns a Faraday::Connection object
51
+ #
52
+ # @return [Faraday::Connection]
53
+ def connection
54
+ @connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware))
55
+ end
56
+
57
+ # Perform an HTTP request
58
+ #
59
+ # @raise [Flix::Error::ClientError, Flix::Error::DecodeError]
60
+ def request(method, path, params={}, options={})
61
+ uri = options[:endpoint] || @endpoint
62
+ uri = URI(uri) unless uri.respond_to?(:host)
63
+ uri += path
64
+ request_headers = {}
65
+
66
+ if credentials?
67
+ authorization = auth_header(method, uri, params)
68
+ request_headers[:authorization] = authorization.to_s
69
+ end
70
+
71
+ connection.url_prefix = options[:endpoint] || @endpoint
72
+
73
+ response = connection.run_request(method.to_sym, path, nil, request_headers) do |request|
74
+ unless params.empty?
75
+ case request.method
76
+ when :post, :put
77
+ request.body = params
78
+ else
79
+ request.params.update(params)
80
+ end
81
+ end
82
+ yield request if block_given?
83
+ end.env
84
+
85
+ # @rate_limit.update(response[:response_headers])
86
+
87
+ response
88
+
89
+ rescue Faraday::Error::ClientError
90
+ # raise Flix::Error::ClientError
91
+ raise "Faraday Oops".to_yaml
92
+ rescue MultiJson::DecodeError
93
+ # raise Flix::Error::DecodeError
94
+ raise "MultiJson Oops".to_yaml
95
+ end
96
+
97
+ def auth_header(method, uri, params={})
98
+ # When posting a file, don't sign any params
99
+ signature_params = [:post, :put].include?(method.to_sym) && params.values.any?{|value| value.is_a?(File) || (value.is_a?(Hash) && (value[:io].is_a?(IO) || value[:io].is_a?(StringIO)))} ? {} : params
100
+ SimpleOAuth::Header.new(method, uri, signature_params, credentials)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,69 @@
1
+ require 'flix/default'
2
+
3
+ module Flix
4
+ module Configurable
5
+ attr_writer :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret
6
+ attr_accessor :endpoint, :connection_options, :middleware, :identity_map
7
+ # attr_accessor :media_endpoint, :search_endpoint,
8
+ class << self
9
+
10
+ def keys
11
+ @keys ||= [
12
+ :consumer_key,
13
+ :consumer_secret,
14
+ :oauth_token,
15
+ :oauth_token_secret,
16
+ :endpoint,
17
+ :connection_options,
18
+ :middleware,
19
+ :identity_map,
20
+ # :media_endpoint,
21
+ # :search_endpoint,
22
+ ]
23
+ end
24
+
25
+ end
26
+
27
+ # Convenience method to allow configuration options to be set in a block
28
+ def configure
29
+ yield self
30
+ self
31
+ end
32
+
33
+ # @return [Boolean]
34
+ def credentials?
35
+ credentials.values.all?
36
+ end
37
+
38
+ # @return [Fixnum]
39
+ def cache_key
40
+ options.hash
41
+ end
42
+
43
+ def reset!
44
+ Flix::Configurable.keys.each do |key|
45
+ instance_variable_set(:"@#{key}", Flix::Default.options[key])
46
+ end
47
+ self
48
+ end
49
+ alias setup reset!
50
+
51
+ private
52
+
53
+ # @return [Hash]
54
+ def credentials
55
+ {
56
+ :consumer_key => @consumer_key,
57
+ :consumer_secret => @consumer_secret,
58
+ :token => @oauth_token,
59
+ :token_secret => @oauth_token_secret,
60
+ }
61
+ end
62
+
63
+ # @return [Hash]
64
+ def options
65
+ Hash[Flix::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,115 @@
1
+ require 'faraday'
2
+ require 'flix/configurable'
3
+ # require 'flix/error/client_error'
4
+ # require 'flix/error/server_error'
5
+ require 'flix/identity_map'
6
+ # require 'flix/request/multipart_with_file'
7
+ # require 'flix/response/parse_json'
8
+ # require 'flix/response/raise_error'
9
+ require 'flix/version'
10
+
11
+ module Flix
12
+ module Default
13
+ ENDPOINT = 'http://api.netflix.com' unless defined? ENDPOINT
14
+ # MEDIA_ENDPOINT = 'https://upload.flix.com' unless defined? MEDIA_ENDPOINT
15
+ # SEARCH_ENDPOINT = 'https://search.flix.com' unless defined? SEARCH_ENDPOINT
16
+ CONNECTION_OPTIONS = {
17
+ :headers => {
18
+ :accept => 'application/json',
19
+ :user_agent => "Flix Ruby Gem #{Flix::VERSION}"
20
+ },
21
+ :open_timeout => 5,
22
+ :raw => true,
23
+ :ssl => {:verify => false},
24
+ :timeout => 10,
25
+ } unless defined? CONNECTION_OPTIONS
26
+ IDENTITY_MAP = Flix::IdentityMap unless defined? IDENTITY_MAP
27
+ MIDDLEWARE = Faraday::Builder.new(
28
+ &Proc.new do |builder|
29
+ # # Convert file uploads to Faraday::UploadIO objects
30
+ # builder.use Flix::Request::MultipartWithFile
31
+ # # Checks for files in the payload
32
+ # builder.use Faraday::Request::Multipart
33
+
34
+ builder.use Faraday::Request::OAuth, Flix::Default.options
35
+ # Convert request params to "www-form-urlencoded"
36
+ builder.use Faraday::Request::UrlEncoded
37
+ # Handle 4xx server responses
38
+ # builder.use Flix::Response::RaiseError, Flix::Error::ClientError
39
+ # # Parse JSON response bodies using MultiJson
40
+ # builder.use Flix::Response::ParseJson
41
+ # # Handle 5xx server responses
42
+ # builder.use Flix::Response::RaiseError, Flix::Error::ServerError
43
+ # Set Faraday's HTTP adapter
44
+ builder.adapter Faraday.default_adapter
45
+ end
46
+ )
47
+
48
+ class << self
49
+
50
+ # @return [Hash]
51
+ def options
52
+ Hash[Flix::Configurable.keys.map{|key| [key, send(key)]}]
53
+ end
54
+
55
+ # @return [String]
56
+ def consumer_key
57
+ ENV['NETFLIX_CONSUMER_KEY']
58
+ end
59
+
60
+ # @return [String]
61
+ def consumer_secret
62
+ ENV['NETFLIX_CONSUMER_SECRET']
63
+ end
64
+
65
+ # @return [String]
66
+ def oauth_token
67
+ ENV['NETFLIX_OAUTH_TOKEN']
68
+ end
69
+
70
+ # @return [String]
71
+ def oauth_token_secret
72
+ ENV['NETFLIX_OAUTH_TOKEN_SECRET']
73
+ end
74
+
75
+ # @note This is configurable in case you want to use HTTP instead of HTTPS or use a Netflix-compatible endpoint.
76
+ # @see http://status.net/wiki/Flix-compatible_API
77
+ # @see http://en.blog.wordpress.com/2009/12/12/flix-api/
78
+ # @see http://staff.tumblr.com/post/287703110/api
79
+ # @see http://developer.typepad.com/typepad-flix-api/flix-api.html
80
+ # @return [String]
81
+ def endpoint
82
+ ENDPOINT
83
+ end
84
+
85
+ # # @return [String]
86
+ # def media_endpoint
87
+ # MEDIA_ENDPOINT
88
+ # end
89
+ #
90
+ # # @return [String]
91
+ # def search_endpoint
92
+ # SEARCH_ENDPOINT
93
+ # end
94
+
95
+ # @return [Hash]
96
+ def connection_options
97
+ CONNECTION_OPTIONS
98
+ end
99
+
100
+ # @return [Flix::IdentityMap]
101
+ def identity_map
102
+ IDENTITY_MAP
103
+ end
104
+
105
+ # @note Faraday's middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one.
106
+ # @see https://github.com/technoweenie/faraday#advanced-middleware-usage
107
+ # @see http://mislav.uniqpath.com/2011/07/faraday-advanced-http/
108
+ # @return [Faraday::Builder]
109
+ def middleware
110
+ MIDDLEWARE
111
+ end
112
+
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,22 @@
1
+ module Flix
2
+
3
+ # Tracks objects to help ensure that each object gets loaded only once.
4
+ # See: http://www.martinfowler.com/eaaCatalog/identityMap.html
5
+ class IdentityMap < Hash
6
+
7
+ # @param id
8
+ # @return [Object]
9
+ def fetch(id)
10
+ self[id]
11
+ end
12
+
13
+ # @param id
14
+ # @param object
15
+ # @return [Object]
16
+ def store(id, object)
17
+ self[id] = object
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,9 @@
1
+ require 'flix/basic_user'
2
+ require 'flix/creatable'
3
+
4
+ module Flix
5
+ class User < Flix::BasicUser
6
+ include Flix::Creatable
7
+
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Flix
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,8 +10,88 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-08-24 00:00:00.000000000 Z
13
- dependencies: []
14
- description: API wrapper
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.8'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.8'
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.8'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.8'
46
+ - !ruby/object:Gem::Dependency
47
+ name: multi_json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simple_oauth
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.1.6
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.1.6
78
+ - !ruby/object:Gem::Dependency
79
+ name: json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A Ruby wrapper for the API
15
95
  email:
16
96
  - andrew@otherscreen.com
17
97
  executables: []
@@ -25,8 +105,14 @@ files:
25
105
  - Rakefile
26
106
  - flix.gemspec
27
107
  - lib/flix.rb
108
+ - lib/flix/api.rb
109
+ - lib/flix/client.rb
110
+ - lib/flix/configurable.rb
111
+ - lib/flix/default.rb
112
+ - lib/flix/identity_map.rb
113
+ - lib/flix/user.rb
28
114
  - lib/flix/version.rb
29
- homepage: ''
115
+ homepage: https://github.com/AndrewGertig/flix
30
116
  licenses: []
31
117
  post_install_message:
32
118
  rdoc_options: []
@@ -49,5 +135,5 @@ rubyforge_project:
49
135
  rubygems_version: 1.8.24
50
136
  signing_key:
51
137
  specification_version: 3
52
- summary: Walking through the process of creating an API client
138
+ summary: API client
53
139
  test_files: []