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.



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
80
# File 'lib/audiosearch.rb', line 52

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.



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

def agent
  @agent
end

#api_endpointObject

Returns the value of attribute api_endpoint.



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

def api_endpoint
  @api_endpoint
end

#cookiesObject

Returns the value of attribute cookies.



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

def cookies
  @cookies
end

#croak_on_404Object

Returns the value of attribute croak_on_404.



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

def croak_on_404
  @croak_on_404
end

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#user_agentObject

Returns the value of attribute user_agent.



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

def user_agent
  @user_agent
end

Instance Method Details

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



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

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



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
137
# File 'lib/audiosearch.rb', line 109

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



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

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

#get_oauth_token(options = {}) ⇒ Object



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
107
# File 'lib/audiosearch.rb', line 82

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



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

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


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

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



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

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

#get_tastemakers(params = {}) ⇒ Object



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

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


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

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

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



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

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

#versionObject



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

def version
  return "1.0.1"
end