Class: Bandsintown::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bandsintown/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ Connection

Returns a new instance of Connection.



5
6
7
# File 'lib/bandsintown/connection.rb', line 5

def initialize(base_url)
  @base_url = base_url
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



3
4
5
# File 'lib/bandsintown/connection.rb', line 3

def base_url
  @base_url
end

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/bandsintown/connection.rb', line 3

def client
  @client
end

Instance Method Details

#encode(args = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bandsintown/connection.rb', line 27

def encode(args = {})
  start_date = args.delete(:start_date)
  end_date   = args.delete(:end_date)
  if start_date && end_date
    start_date  = start_date.strftime("%Y-%m-%d") unless start_date.is_a?(String)
    end_date    = end_date.strftime("%Y-%m-%d")   unless end_date.is_a?(String)
    args[:date] = "#{start_date},#{end_date}"
  elsif args.has_key?(:date)
    args[:date] = args[:date].strftime("%Y-%m-%d") unless args[:date].is_a?(String)
  end
  args[:format] = "json"
  args[:app_id] = Bandsintown.app_id
  args.to_param
end

#get(resource_path, method_path, params = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/bandsintown/connection.rb', line 9

def get(resource_path, method_path, params = {})
  request_url = "#{@base_url}/#{resource_path}/#{method_path}?#{encode(params.symbolize_keys)}"
  begin
    RestClient.get(request_url)
  rescue RestClient::ResourceNotFound => error_response
    error_response.response
  end
end

#post(resource_path, method_path, body = {}) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/bandsintown/connection.rb', line 18

def post(resource_path, method_path, body = {})
  request_url = "#{@base_url}/#{resource_path}/#{method_path}?#{encode({})}"
  begin
    RestClient.post(request_url, body.to_json, :content_type => :json, :accept => :json)
  rescue RestClient::ResourceNotFound => error_response
    error_response.response
  end
end