Class: Lbry::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/lbry/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth: {}) ⇒ Api

Returns a new instance of Api.



21
22
23
24
25
# File 'lib/lbry/api.rb', line 21

def initialize(auth: {})
  @auth = { username: auth["user"], password: auth["password"] }
  @default_options = {}
  default_options[:basic_auth] = auth if auth.values.any?
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



7
8
9
# File 'lib/lbry/api.rb', line 7

def auth
  @auth
end

#default_optionsObject

Returns the value of attribute default_options.



7
8
9
# File 'lib/lbry/api.rb', line 7

def default_options
  @default_options
end

Instance Method Details

#get(lbry_command, lbry_params = {}, options: {}) ⇒ Object



34
35
36
# File 'lib/lbry/api.rb', line 34

def get(lbry_command, lbry_params = {}, options: {})
  request(:get, lbry_command, lbry_params)
end

#parse_response(raw_response) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
# File 'lib/lbry/api.rb', line 12

def parse_response(raw_response)
  # binding.pry
  struct = JSON.parse(raw_response.body, object_class: OpenStruct)
  error = struct.error
  return struct unless error && error.code && error.message
  # binding.pry
  raise Lbry::ApiError.new(error)
end

#post(lbry_command, *lbry_params) ⇒ Object



38
39
40
41
# File 'lib/lbry/api.rb', line 38

def post(lbry_command, *lbry_params)
  headers = { "Content-Type" => "application/x-www-form-urlencoded" }
  request(:post, lbry_command, *lbry_params, headers: headers)
end

#put(lbry_command, lbry_params = {}, options: {}) ⇒ Object



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

def put(lbry_command, lbry_params = {}, options: {})
  request(:put, lbry_command, lbry_params)
end

#request(http_verb, lbry_command, lbry_params, headers: {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/lbry/api.rb', line 27

def request(http_verb, lbry_command, lbry_params, headers: {})
  params = { body: { method: lbry_command, params: lbry_params }.to_json }
  params[:headers] = headers if headers.keys.any?
  response = self.class.send(http_verb, '', params)
  parse_response(response)
end