vidsy-data-api 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93f40e4e9c553a989d40f3c585dbd3e1cb57f72c
4
- data.tar.gz: b379b58644a2bfdc978dc1d8da777e306d004ec1
3
+ metadata.gz: b630829b895f90fdfac65adee62b293654f69d50
4
+ data.tar.gz: 417e2cfb904fd6a102789aa854b40b82a557196b
5
5
  SHA512:
6
- metadata.gz: 170bc521276ffb37cf0ea1cf64ad478546ab29a520e4dae31768e78cdfefd679956fd4c7332e213a4e3b1023265e5ce457c5ce6e8929c7e020454fe44a958e7b
7
- data.tar.gz: ec0362660160e90858633f58db0e130c7d03648857bfae9b6c679a3bc4be18afc09ba33b9a7f998e3495601cc614cf108db940f22c34c77fdb411b737fb91093
6
+ metadata.gz: 81d0cf0afde69d3778fe5558683dabba24b18c8e9d1614875efa644f3dfb4c7ba1b54eeba42181af73bba3c190325a63fc9d8902c2bb9000b85e9421fdad434d
7
+ data.tar.gz: 8e308eeece5fe54c1af6663d3f53305edaeba907535d16597675cf7b8790c45b1590070e319ed88beea6def83d2e9b2134c09f366097aa18f26a2c2574c7a33f
@@ -1,4 +1,4 @@
1
- require "vidsy/data/api/client"
1
+ require "vidsy/data/api/configuration"
2
2
  require "vidsy/data/api/version"
3
3
 
4
4
  module Vidsy
@@ -1,34 +1,41 @@
1
1
  require "vidsy/data/api/client"
2
+ require "vidsy/data/api/configuration"
2
3
 
3
4
  module Vidsy
4
5
  module Data
5
6
  module API
6
- class Brand < Client
7
+ class Brand
7
8
  def initialize(id = nil)
8
9
  @id = id
9
10
  end
10
11
 
11
- def challenges
12
- raise(ArgumentError, "ID not set for Brand") if id.nil?
13
- challenges = get "#{routes.brand}/#{id}/#{routes.challenges}"
14
- challenges.size == 0 ? nil : challenges
12
+ def self.all
13
+ new.all
15
14
  end
16
15
 
17
- def self.all
18
- brands = get "#{routes.brands}"
19
- brands.size == 0 ? nil : brands
16
+ def all
17
+ client.get "#{routes.brands}"
18
+ end
19
+
20
+ def challenges
21
+ raise(ArgumentError, "ID not set for Brand") if id.nil?
22
+ client.get "#{routes.brand}/#{id}/#{routes.challenges}"
20
23
  end
21
24
 
22
25
  private
23
26
 
24
27
  attr_reader :id
25
28
 
26
- def get(path)
27
- self.class.superclass.method(:get).call path
29
+ def client
30
+ Vidsy::Data::API::Client.new
31
+ end
32
+
33
+ def configuration
34
+ Vidsy::Data::API::Configuration
28
35
  end
29
36
 
30
37
  def routes
31
- self.class.superclass.method(:routes).call
38
+ configuration.routes
32
39
  end
33
40
  end
34
41
  end
@@ -1,78 +1,28 @@
1
- require "faraday"
2
- require "json"
3
- require "ostruct"
1
+ require "vidsy/data/api/request"
2
+ require "vidsy/data/api/response"
4
3
 
5
4
  module Vidsy
6
5
  module Data
7
6
  module API
8
7
  class Client
9
- @@config = OpenStruct.new
10
-
11
- def self.set_api_key(s)
12
- raise(ArgumentError, "API key must be String") unless s.is_a? String
13
- @@config.tap { |c| c.api_key = s }
14
- end
15
-
16
- def self.set_endpoint(s)
17
- raise(ArgumentError, "Endpoint must be String") unless s.is_a? String
18
- @@config.tap { |c| c.endpoint = s }
19
- end
20
-
21
- def self.set_routes(h)
22
- raise(ArgumentError, "Routes must be Hash") unless h.is_a? Hash
23
- @@config.tap { |c| c.routes = OpenStruct.new h }
24
- end
25
-
26
- def self.set_version(s)
27
- raise(ArgumentError, "Version must be String") unless s.is_a? String
28
- @@config.tap { |c| c.version = s }
29
- end
30
-
31
- protected
32
-
33
- def self.api_key
34
- @@config.api_key
35
- end
36
-
37
- def self.endpoint
38
- @@config.endpoint
39
- end
40
-
41
- def self.get(path)
42
- parse connection.get("/#{version}#{path}")
43
- end
44
-
45
- def self.post(path, params)
46
- response = connection.post do |req|
47
- req.url "/#{version}#{path}"
48
- req.headers["Content-Type"] = "application/json"
49
- req.body = params.to_json
50
- end
51
-
52
- parse response
53
- end
54
-
55
- def self.routes
56
- @@config.routes
8
+ def get(path)
9
+ api_response = request.get path
10
+ response api_response
57
11
  end
58
12
 
59
- def self.version
60
- @@config.version || "v1"
13
+ def post(path, options)
14
+ api_response = request.post(path, options)
15
+ response api_response
61
16
  end
62
17
 
63
18
  private
64
19
 
65
- def self.connection
66
- @connection ||= Faraday.new(:url => endpoint) do |f|
67
- f.response :logger
68
- f.adapter Faraday.default_adapter
69
- end
20
+ def request
21
+ Vidsy::Data::API::Request
70
22
  end
71
23
 
72
- def self.parse(response)
73
- JSON.parse(response.body)["data"].map do |result|
74
- OpenStruct.new result
75
- end
24
+ def response(api_response)
25
+ Vidsy::Data::API::Response.build api_response
76
26
  end
77
27
  end
78
28
  end
@@ -0,0 +1,29 @@
1
+ require "ostruct"
2
+
3
+ module Vidsy
4
+ module Data
5
+ module API
6
+ class Configuration
7
+ DEFAULT_VERSION = "v1"
8
+
9
+ class << self
10
+ attr_accessor :endpoint
11
+ attr_writer :routes, :version
12
+
13
+ def configure(&block)
14
+ yield self
15
+ self
16
+ end
17
+
18
+ def routes
19
+ OpenStruct.new @routes
20
+ end
21
+
22
+ def version
23
+ @version ||= DEFAULT_VERSION
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module Vidsy
2
+ module Data
3
+ module API
4
+ module Error
5
+ class Response < StandardError ; end
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Vidsy
2
+ module Data
3
+ module API
4
+ module Error
5
+ class Unexpected < StandardError ; end
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require "json"
4
+ require "vidsy/data/api/configuration"
5
+
6
+ module Vidsy
7
+ module Data
8
+ module API
9
+ class Request
10
+ def self.get(path)
11
+ new.get path
12
+ end
13
+
14
+ def self.post(path, options = {})
15
+ new.post(path, options)
16
+ end
17
+
18
+ def get(path)
19
+ connection.get complete_path(path)
20
+ end
21
+
22
+ def post(path, options)
23
+ connection.post do |r|
24
+ r.body = options.to_json
25
+ r.headers["Content-Type"] = "application/json"
26
+ r.url complete_path(path)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def complete_path(path)
33
+ "/#{version}#{path}"
34
+ end
35
+
36
+ def configuration
37
+ Vidsy::Data::API::Configuration
38
+ end
39
+
40
+ def connection
41
+ Faraday.new(:url => endpoint) do |c|
42
+ c.adapter Faraday.default_adapter
43
+ c.use Faraday::Response::RaiseError
44
+ c.response :json, :content_type => /\bjson$/
45
+ end
46
+ end
47
+
48
+ def endpoint
49
+ configuration.endpoint
50
+ end
51
+
52
+ def version
53
+ configuration.version
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,46 @@
1
+ require "ostruct"
2
+ require "vidsy/data/api/error/unexpected"
3
+
4
+ module Vidsy
5
+ module Data
6
+ module API
7
+ class Response
8
+ def self.build(response)
9
+ new(response).data
10
+ end
11
+
12
+ def initialize(response)
13
+ @response = response
14
+ end
15
+
16
+ def data
17
+ raise unexpected_error unless response.body
18
+ data = response.body["data"] || raise(response_error)
19
+ data.map { |e| OpenStruct.new e }
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :response
25
+
26
+ def error_details
27
+ response.body.fetch "details"
28
+ end
29
+
30
+ def error_status
31
+ response.body.fetch "status"
32
+ end
33
+
34
+ def response_error
35
+ Vidsy::Data::API::Error::Response.new(
36
+ "#{error_status} - #{error_details}"
37
+ )
38
+ end
39
+
40
+ def unexpected_error
41
+ Vidsy::Data::API::Error::Unexpected
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,17 +1,38 @@
1
1
  require "vidsy/data/api/client"
2
+ require "vidsy/data/api/configuration"
2
3
 
3
4
  module Vidsy
4
5
  module Data
5
6
  module API
6
- class User < Client
7
+ class User
8
+ def self.create(options)
9
+ new.create options
10
+ end
11
+
7
12
  def self.find(id)
8
- users = get "#{routes.user}/#{id}"
9
- users.size == 0 ? nil : users.first
13
+ new.find id
14
+ end
15
+
16
+ def create(options)
17
+ client.post(routes.user, options).first
18
+ end
19
+
20
+ def find(id)
21
+ client.get "#{routes.user}/#{id}"
22
+ end
23
+
24
+ private
25
+
26
+ def client
27
+ Vidsy::Data::API::Client.new
28
+ end
29
+
30
+ def configuration
31
+ Vidsy::Data::API::Configuration
10
32
  end
11
33
 
12
- def self.create(params)
13
- users = post(routes.user, params)
14
- users.first.id
34
+ def routes
35
+ configuration.routes
15
36
  end
16
37
  end
17
38
  end
@@ -1,7 +1,7 @@
1
1
  module Vidsy
2
2
  module Data
3
3
  module API
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,20 +1,30 @@
1
1
  require "vidsy/data/api/client"
2
+ require "vidsy/data/api/configuration"
2
3
 
3
4
  module Vidsy
4
5
  module Data
5
6
  module API
6
- class Video < Client
7
- def self.create(params, user_id)
8
- videos = post(routes.video, post_params(user_id))
9
- videos.first.id
7
+ class Video
8
+ def self.create(options)
9
+ new.create params
10
+ end
11
+
12
+ def create(options)
13
+ client.post(routes.video, options).first
10
14
  end
11
15
 
12
16
  private
13
17
 
14
- def self.post_params(user_id)
15
- {
16
- :user_id => user_id.to_i
17
- }
18
+ def client
19
+ Vidsy::Data::API::Client.new
20
+ end
21
+
22
+ def configuration
23
+ Vidsy::Data::API::Configuration
24
+ end
25
+
26
+ def routes
27
+ configuration.routes
18
28
  end
19
29
  end
20
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidsy-data-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Revett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-29 00:00:00.000000000 Z
11
+ date: 2016-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -105,6 +105,11 @@ files:
105
105
  - lib/vidsy/data/api.rb
106
106
  - lib/vidsy/data/api/brand.rb
107
107
  - lib/vidsy/data/api/client.rb
108
+ - lib/vidsy/data/api/configuration.rb
109
+ - lib/vidsy/data/api/error/response.rb
110
+ - lib/vidsy/data/api/error/unexpected.rb
111
+ - lib/vidsy/data/api/request.rb
112
+ - lib/vidsy/data/api/response.rb
108
113
  - lib/vidsy/data/api/user.rb
109
114
  - lib/vidsy/data/api/version.rb
110
115
  - lib/vidsy/data/api/video.rb