Class: Audiosearch::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/audiosearch.rb', line 51

def initialize(args)
  #puts args.inspect
  @un                  = args[:username]
  @pw                  = args[:password]
  @oauth_id            = args[:id]
  @oauth_secret        = args[:secret]
  @oauth_redir_uri     = args[:redir_uri] || 'urn:ietf:wg:oauth:2.0:oob'
  @host                = args[:host] || 'https://www.audiosear.ch'
  @debug               = args[:debug]
  @user_agent          = args[:user_agent] || 'audiosearch-ruby-client/'+version()
  @api_endpoint        = args[:api_endpoint] || '/api'
  @croak_on_404        = args[:croak_on_404] || false

  # normalize host
  @host.gsub!(/\/$/, '')

  # sanity check
  begin
    uri = URI.parse(@host)
  rescue URI::InvalidURIError => err
    raise "Bad :host value " + err
  end
  if (!uri.host || !uri.port)
    raise "Bad :host value " + @server
  end

  @agent = get_agent()

end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



41
42
43
# File 'lib/audiosearch.rb', line 41

def agent
  @agent
end

#api_endpointObject

Returns the value of attribute api_endpoint.



44
45
46
# File 'lib/audiosearch.rb', line 44

def api_endpoint
  @api_endpoint
end

#cookiesObject

Returns the value of attribute cookies.



43
44
45
# File 'lib/audiosearch.rb', line 43

def cookies
  @cookies
end

#croak_on_404Object

Returns the value of attribute croak_on_404.



45
46
47
# File 'lib/audiosearch.rb', line 45

def croak_on_404
  @croak_on_404
end

#debugObject

Returns the value of attribute debug.



40
41
42
# File 'lib/audiosearch.rb', line 40

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



39
40
41
# File 'lib/audiosearch.rb', line 39

def host
  @host
end

#user_agentObject

Returns the value of attribute user_agent.



42
43
44
# File 'lib/audiosearch.rb', line 42

def user_agent
  @user_agent
end

Instance Method Details

#get(path, params = {}) ⇒ Object



138
139
140
141
142
143
# File 'lib/audiosearch.rb', line 138

def get(path, params={})
  url = path.match(/^https?:/) ? path : @api_endpoint + path
  resp = @agent.get url, params
  @debug and pp(resp)
  return Audiosearch::Response.new resp
end

#get_agentObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/audiosearch.rb', line 108

def get_agent()
  uri = @host + @api_endpoint
  opts = {
    :url => uri,
    :ssl => {:verify => false},
    :headers => {
      'User-Agent'   => @user_agent,
      'Accept'       => 'application/json',
      'Cookie'       => @cookies
    }
  }
  @token = get_oauth_token
  #puts "token="
  #puts pp(@token)
  conn = Faraday.new(opts) do |faraday|
    faraday.request :url_encoded
    [:mashify, :json].each{|mw| faraday.response(mw) }
    if !@croak_on_404
      faraday.use Audiosearch::FaradayErrHandler
    else 
      faraday.response(:raise_error)
    end
    faraday.request :authorization, 'Bearer', @token.token
    faraday.response :logger if @debug
    faraday.adapter  :excon   # IMPORTANT this is last
  end

  return conn
end

#get_episode(ep_id) ⇒ Object



150
151
152
153
# File 'lib/audiosearch.rb', line 150

def get_episode(ep_id)
  resp = get('/episodes/'+ep_id.to_s)
  return resp.http_resp.body
end

#get_oauth_token(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/audiosearch.rb', line 81

def get_oauth_token(options={})
  oauth_options = {
    site:            @host + @api_endpoint,
    authorize_url:   @host + '/oauth/authorize',
    token_url:       @host + '/oauth/token',
    redirect_uri:    @oauth_redir_uri,
    connection_opts: options.merge( { :ssl => {:verify => false}, } )
  }

  # TODO

  client = OAuth2::Client.new(@oauth_id, @oauth_secret, oauth_options) do |faraday|
    faraday.request  :url_encoded
    faraday.response :logger if @debug
    faraday.adapter  :excon
  end

  token = nil
  if @un && @pw
    # TODO 3-legged oauth to @authorize_url
  else
    token = client.client_credentials.get_token()
  end

  return token
end

#get_person(p_id) ⇒ Object



172
173
174
175
# File 'lib/audiosearch.rb', line 172

def get_person(p_id)
  resp = get('/people/'+p_id.to_s)
  return resp.http_resp.body
end


177
178
179
180
181
# File 'lib/audiosearch.rb', line 177

def get_related(id, params={})
  type = params.has_key?(:type) ? params[:type] : 'episodes'
  path = "/#{type}/#{id}/related"
  get(path, params)
end

#get_show(id) ⇒ Object



145
146
147
148
# File 'lib/audiosearch.rb', line 145

def get_show(id)
  resp = get('/shows/'+id.to_s)
  return resp.http_resp.body
end

#get_tastemakers(params = {}) ⇒ Object



165
166
167
168
169
170
# File 'lib/audiosearch.rb', line 165

def get_tastemakers(params={})
  type = params.has_key?(:type) ? params[:type] : 'episodes'
  n = params.has_key?(:n) ? params[:n] : 10
  resp = get("/tastemakers/#{type}/#{n}")
  return resp.http_resp.body
end


160
161
162
163
# File 'lib/audiosearch.rb', line 160

def get_trending
  resp = get('/trending/')
  return resp.http_resp.body
end

#search(params, type = 'episodes') ⇒ Object



155
156
157
158
# File 'lib/audiosearch.rb', line 155

def search(params, type='episodes')
  path = '/search/'+type
  get(path, params)
end

#versionObject



47
48
49
# File 'lib/audiosearch.rb', line 47

def version
  return "1.0.1"
end