Class: Songkick::Transport::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/songkick/transport/service.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ancestorObject



31
32
33
# File 'lib/songkick/transport/service.rb', line 31

def self.ancestor
  self.ancestors.select {|a| a.respond_to?(:get_user_agent)}[1]
end

.endpoint(name) ⇒ Object



4
5
6
# File 'lib/songkick/transport/service.rb', line 4

def self.endpoint(name)
  @endpoint_name = name.to_s
end

.get_endpoint_nameObject



35
36
37
# File 'lib/songkick/transport/service.rb', line 35

def self.get_endpoint_name
  @endpoint_name || (ancestor && ancestor.get_endpoint_name)
end

.get_endpointsObject



47
48
49
# File 'lib/songkick/transport/service.rb', line 47

def self.get_endpoints
  @endpoints || {}
end

.get_stub_transportObject



55
56
57
# File 'lib/songkick/transport/service.rb', line 55

def self.get_stub_transport
  @stub_transport || (ancestor && ancestor.get_stub_transport) || nil
end

.get_timeoutObject



39
40
41
# File 'lib/songkick/transport/service.rb', line 39

def self.get_timeout
  @timeout || (ancestor && ancestor.get_timeout) || 10
end

.get_transport_layerObject



51
52
53
# File 'lib/songkick/transport/service.rb', line 51

def self.get_transport_layer
  @transport_layer || (ancestor && ancestor.get_transport_layer) || Songkick::Transport::Curb
end

.get_user_agentObject



43
44
45
# File 'lib/songkick/transport/service.rb', line 43

def self.get_user_agent
  @user_agent || (ancestor && ancestor.get_user_agent)
end

.new_transportObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/songkick/transport/service.rb', line 59

def self.new_transport
  unless name = get_endpoint_name
    raise "no endpoint specified for #{self}, call endpoint 'foo' inside #{self}"
  end
  unless endpoint = Service.get_endpoints[name]
    raise "can't find endpoint for '#{name}', should have called Songkick::Transport::Service.set_endpoints"
  end
  unless user_agent = get_user_agent
    raise "no user agent specified for #{self}, call user_agent 'foo' inside #{self} or on Songkick::Transport::Service"
  end
  get_stub_transport || get_transport_layer.new(endpoint, :user_agent => user_agent, 
                                                          :timeout    => get_timeout)
end

.set_endpoints(hash) ⇒ Object



24
25
26
27
28
29
# File 'lib/songkick/transport/service.rb', line 24

def self.set_endpoints(hash)
  unless self == Songkick::Transport::Service
    raise "set_endpoints only on Songkick::Transport::Service"
  end
  @endpoints = hash
end

.stub_transport(stub) ⇒ Object



20
21
22
# File 'lib/songkick/transport/service.rb', line 20

def self.stub_transport(stub)
  @stub_transport = stub
end

.timeout(value) ⇒ Object



8
9
10
# File 'lib/songkick/transport/service.rb', line 8

def self.timeout(value)
  @timeout = value
end

.transport_layer(value) ⇒ Object



16
17
18
# File 'lib/songkick/transport/service.rb', line 16

def self.transport_layer(value)
  @transport_layer = value
end

.user_agent(value) ⇒ Object



12
13
14
# File 'lib/songkick/transport/service.rb', line 12

def self.user_agent(value)
  @user_agent = value
end

Instance Method Details

#httpObject



73
74
75
# File 'lib/songkick/transport/service.rb', line 73

def http
  @http ||= self.class.new_transport
end

#rescue_404(response = nil) ⇒ Object



81
82
83
84
85
# File 'lib/songkick/transport/service.rb', line 81

def rescue_404(response=nil)
  yield
rescue Songkick::Transport::HttpError => e
  e.status == 404 ? response : (raise e)
end

#stub_transport(http) ⇒ Object



77
78
79
# File 'lib/songkick/transport/service.rb', line 77

def stub_transport(http)
  @http = http
end