brocktail 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,10 @@
1
+ Ruby wrapper for Blocktail API v1 - your number one festival guide
2
+
3
+
4
+ TODO:
5
+
6
+ * specs ("Oh Ivan! you should've write specs first..." - I know :) )
7
+ * Endpoint for Act
8
+ * Endpoint for Venue
9
+ * Endpoint for User
10
+ * Deep (nested) JSON parsing
data/brocktail.gemspec CHANGED
@@ -12,7 +12,11 @@ Gem::Specification.new do |s|
12
12
  s.summary = %q{Brocktail is a Ruby wrapper for Blocktail API v1}
13
13
  s.description = %q{Brocktail is a Ruby wrapper for Blocktail API v1}
14
14
 
15
- s.add_development_dependency "typhoeus"
15
+ s.add_dependency "typhoeus"
16
+ s.add_dependency "crack"
17
+ s.add_dependency "activesupport"
18
+ s.add_dependency "i18n"
19
+
16
20
 
17
21
  s.rubyforge_project = "brocktail"
18
22
 
data/lib/brocktail.rb CHANGED
@@ -1,13 +1,17 @@
1
1
  require 'typhoeus'
2
- require 'json'
3
2
  require 'base64'
3
+ require 'crack'
4
+ require 'active_support/all'
4
5
 
5
- require 'brocktail/errors'
6
- require 'brocktail/credentials'
7
- require 'brocktail/behavior/crud'
6
+ require File.dirname(__FILE__) + '/brocktail/version'
7
+ require File.dirname(__FILE__) + '/brocktail/credentials'
8
+ require File.dirname(__FILE__) + '/brocktail/errors'
9
+ require File.dirname(__FILE__) + '/brocktail/behavior/crud'
8
10
 
11
+ require File.dirname(__FILE__) + '/brocktail/base'
9
12
 
10
- require 'brocktail/base'
13
+ %w(base_model event act).each { |f| require File.dirname(__FILE__) + "/brocktail/#{f}" }
14
+ %w(base events acts).each { |f| require File.dirname(__FILE__) + "/brocktail/api/#{f}" }
11
15
 
12
16
  module Brocktail
13
17
 
@@ -0,0 +1,19 @@
1
+ module Brocktail
2
+ class Act < BaseModel
3
+
4
+ api_path "/acts"
5
+
6
+ attr_accessor :id, # Integer
7
+ :event_id, # Integer
8
+ :item_id, # Integer
9
+ :venue_id, # Integer
10
+ :day_number, # Integer
11
+ :starttime, # Date
12
+ :duration, # Integer
13
+ :attendees_count, # Integer
14
+ :created_at, # DateTime
15
+ :updated_at, # DateTime
16
+ :item # Object
17
+
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module Brocktail
2
+ module API
3
+ class Acts < Base
4
+ api_model Brocktail::Act
5
+
6
+ include Brocktail::Behavior::Crud
7
+
8
+ attr_reader :event_id
9
+
10
+ def initialize(credentials, event_id)
11
+ @event_id = event_id
12
+ super(credentials)
13
+ end
14
+
15
+ def all
16
+ response = request(:get, credentials, "#{Brocktail::Event.api_path}/#{@event_id}/#{api_model.api_path}")
17
+ api_model.parse(response.body)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,45 @@
1
+ module Brocktail
2
+ module API
3
+ class Base
4
+ attr_reader :credentials
5
+
6
+ def initialize(credentials)
7
+ @credentials = credentials
8
+ end
9
+
10
+ class << self
11
+ def api_model(klass)
12
+ class_eval <<-END
13
+ def api_model
14
+ #{klass}
15
+ end
16
+ END
17
+ end
18
+ end
19
+
20
+
21
+ protected #-------------------------------------------------------------|
22
+
23
+ def request(method, credentials, path, options = {})
24
+ request = Typhoeus::Request.new("#{credentials.host}#{path}")
25
+
26
+ hydra = Typhoeus::Hydra.new
27
+ hydra.queue(request)
28
+ hydra.run
29
+
30
+ response = request.response
31
+
32
+ case response.code
33
+ when 200..201 then response
34
+ when 400 then raise Brocktail::BadRequest.new(response)
35
+ when 404 then raise Brocktail::NotFound.new(response)
36
+ when 500 then raise Brocktail::ServerError.new(response)
37
+ when 502 then raise Brocktail::Unavailable.new(response)
38
+ when 503 then raise Brocktail::RateLimited.new(response)
39
+ else raise Brocktail::InformBrocktail.new(response)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ module Brocktail
2
+ module API
3
+ class Events < Base
4
+ api_model Brocktail::Event
5
+
6
+ include Brocktail::Behavior::Crud
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ module Brocktail
2
+ class Base
3
+
4
+ attr_reader :request, :credentials
5
+
6
+ def initialize(username, password, options = {})
7
+ options[:ssl] = true if options[:ssl].nil?
8
+ @credentials = Credentials.new(username, password, options[:ssl])
9
+ raise InvalidCredentials unless credentials.valid?
10
+ end
11
+
12
+
13
+ def events
14
+ @events ||= Brocktail::API::Events.new(credentials)
15
+ end
16
+
17
+ def acts(event_id)
18
+ @events ||= Brocktail::API::Acts.new(credentials, event_id)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ module Brocktail
2
+ class BaseModel
3
+
4
+ def initialize(attributes = {})
5
+ self.attributes = attributes
6
+ end
7
+
8
+ def attributes=(attributes)
9
+ attributes.each { |k,v| send("#{k}=", v)}
10
+ end
11
+
12
+ def ==(other)
13
+ id == other.id
14
+ end
15
+
16
+ def to_i
17
+ id
18
+ end
19
+
20
+ def to_json
21
+ # TODO
22
+ end
23
+
24
+ def self.parse(json, options = {})
25
+ options[:resource] ||= self.name.split('::').last.downcase
26
+ puts options
27
+
28
+ if options[:singular]
29
+ return self.new(Crack::JSON.parse(json)[options[:resource].to_s])
30
+ else
31
+ resources = []
32
+
33
+ # Rails 3 way: Arrayified objects
34
+ # [{"object":{...}}, {"object":{...}}, ..., {"object":{...}}]
35
+ Crack::JSON.parse(json).each { |obj| resources << self.new(obj[options[:resource].to_s]) }
36
+
37
+ # Rails old way: Rooted JSON
38
+ # {"objects":[{...}, {...}, ..., {...}]}
39
+ # Crack::JSON.parse(json)[options[:resource].to_s.pluralize].each { |attrs| resources << self.new(attrs) }
40
+ return resources
41
+ end
42
+ end
43
+
44
+ class << self
45
+ def api_path(path = nil)
46
+ @path ||= path
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,29 @@
1
+ module Brocktail
2
+ module Behavior
3
+ module Crud
4
+
5
+ def all
6
+ response = request(:get, credentials, api_model.api_path)
7
+ api_model.parse(response.body)
8
+ end
9
+
10
+ def find(id)
11
+ response = request(:get, credentials, "#{api_model.api_path}/#{id}")
12
+ api_model.parse(response.body, :singular => true)
13
+ end
14
+
15
+ # def create(model)
16
+ #
17
+ # end
18
+ #
19
+ # def update(model)
20
+ #
21
+ # end
22
+ #
23
+ # def delete(model)
24
+ #
25
+ # end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ module Brocktail
2
+ class Credentials
3
+
4
+ attr_accessor :username, :password, :ssl
5
+
6
+ def initialize(username, password, ssl = true)
7
+ @username, @password, @ssl = username, password, ssl
8
+ end
9
+
10
+ def valid?
11
+ !username.nil? && !password.nil?
12
+ end
13
+
14
+ def basic_auth
15
+ Base64.encode64("#{username}:#{password}").delete("\r\n")
16
+ end
17
+
18
+ def host
19
+ "#{ssl ? "https" : "http"}://blocktail.heroku.com/api/v1"
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module Brocktail
2
+
3
+ class InvalidCredentials < StandardError; end
4
+
5
+ class HTTPError < StandardError
6
+ attr_reader :response
7
+
8
+ def initialize(response)
9
+ @response = response
10
+ super
11
+ end
12
+
13
+ def to_s
14
+ hint = response.headers["hint"].nil? ? nil : response.headers["hint"].first
15
+ "#{self.class.to_s} : #{response.code}#{" - #{hint}" if hint}"
16
+ end
17
+ end
18
+
19
+ class RateLimited < HTTPError; end
20
+ class NotFound < HTTPError; end
21
+ class Unavailable < HTTPError; end
22
+ class InformHarvest < HTTPError; end
23
+ class BadRequest < HTTPError; end
24
+ class ServerError < HTTPError; end
25
+
26
+ end
@@ -0,0 +1,24 @@
1
+ module Brocktail
2
+ class Event < BaseModel
3
+
4
+ api_path "/events"
5
+
6
+ attr_accessor :id, # Integer
7
+ :name, # String
8
+ :slug, # String
9
+ :introduction, # Text
10
+ :description, # Text
11
+ :startdate, # Date
12
+ :number_of_days, # Integer
13
+ :attendees_count, # Integer
14
+ :acts_count, # Integer
15
+ :address, # String
16
+ :zip, # String
17
+ :locality, # String
18
+ :state, # String
19
+ :country, # String
20
+ :created_at, # DateTime
21
+ :updated_at # DateTime
22
+
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Brocktail
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brocktail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ivan Malijkh
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-01 00:00:00 +02:00
18
+ date: 2011-04-02 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -30,8 +30,50 @@ dependencies:
30
30
  segments:
31
31
  - 0
32
32
  version: "0"
33
- type: :development
33
+ type: :runtime
34
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: crack
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: activesupport
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: i18n
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
35
77
  description: Brocktail is a Ruby wrapper for Blocktail API v1
36
78
  email:
37
79
  - ivan@lesslines.com
@@ -44,9 +86,20 @@ extra_rdoc_files: []
44
86
  files:
45
87
  - .gitignore
46
88
  - Gemfile
89
+ - README
47
90
  - Rakefile
48
91
  - brocktail.gemspec
49
92
  - lib/brocktail.rb
93
+ - lib/brocktail/act.rb
94
+ - lib/brocktail/api/acts.rb
95
+ - lib/brocktail/api/base.rb
96
+ - lib/brocktail/api/events.rb
97
+ - lib/brocktail/base.rb
98
+ - lib/brocktail/base_model.rb
99
+ - lib/brocktail/behavior/crud.rb
100
+ - lib/brocktail/credentials.rb
101
+ - lib/brocktail/errors.rb
102
+ - lib/brocktail/event.rb
50
103
  - lib/brocktail/version.rb
51
104
  has_rdoc: true
52
105
  homepage: ""