Class: Hikerapi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, api_key: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/hikerapi/client.rb', line 13

def initialize(config = nil, api_key: nil)
  @config = config || Hikerapi.configuration
  @api_key = api_key || @config.api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/hikerapi/client.rb', line 11

def api_key
  @api_key
end

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/hikerapi/client.rb', line 11

def config
  @config
end

Instance Method Details

#comments(media_id_or_code_or_url, options = {}) ⇒ Object



34
35
36
# File 'lib/hikerapi/client.rb', line 34

def comments(media_id_or_code_or_url, options = {})
  media.comments(media_id_or_code_or_url, options)
end

#get(endpoint, params = {}, options = {}) ⇒ Object



38
39
40
# File 'lib/hikerapi/client.rb', line 38

def get(endpoint, params = {}, options = {})
  request(endpoint, method: :get, params: params, options: options)
end

#mediaObject



22
23
24
# File 'lib/hikerapi/client.rb', line 22

def media
  @media ||= Media.new(self)
end

#post(endpoint, params = {}, options = {}) ⇒ Object



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

def post(endpoint, params = {}, options = {})
  request(endpoint, method: :post, params: params, options: options)
end

#posts(username_or_id_or_url, options = {}) ⇒ Object



26
27
28
# File 'lib/hikerapi/client.rb', line 26

def posts(username_or_id_or_url, options = {})
  user.medias(username_or_id_or_url, options)
end

#reels(username_or_id_or_url, options = {}) ⇒ Object



30
31
32
# File 'lib/hikerapi/client.rb', line 30

def reels(username_or_id_or_url, options = {})
  user.clips(username_or_id_or_url, options)
end

#request(endpoint, method: :get, params: {}, options: {}) ⇒ Object



46
47
48
49
50
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
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hikerapi/client.rb', line 46

def request(endpoint, method: :get, params: {}, options: {})
  current_key = options[:api_key] || api_key
  if current_key.nil? || current_key.to_s.strip.empty?
    raise ConfigurationError, "HikerAPI key is missing. Set ENV['HIKER_API_KEY'], configure via Hikerapi.configure { |c| c.api_key = '...' }, or pass api_key: to Client.new"
  end

  clean_endpoint = endpoint.to_s.start_with?('/') ? endpoint.to_s : "/#{endpoint}"
  base_url = options[:api_base_url] || config.api_base_url
  url_str = "#{base_url}#{clean_endpoint}"

  uri = URI.parse(url_str)

  if method == :get
    uri.query = URI.encode_www_form(params) if params && !params.empty?
    req = Net::HTTP::Get.new(uri.request_uri)
  else
    req = Net::HTTP::Post.new(uri.request_uri)
    req['Content-Type'] = 'application/json'
    req.body = JSON.generate(params) if params && !params.empty?
  end

  req['x-access-key'] = current_key
  req['Accept'] = 'application/json'
  req['Accept-Encoding'] = 'gzip, deflate'

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.open_timeout = options[:open_timeout] || config.open_timeout
  http.read_timeout = options[:read_timeout] || config.read_timeout

  max_retries = options[:max_retries] || 3
  retries = 0

  begin
    res = http.request(req)
    parse_response(res)
  rescue APIError => e
    if e.status == 429 && retries < max_retries
      retries += 1
      delay = (res.respond_to?(:[]) && res['Retry-After']) ? res['Retry-After'].to_f : (1.2 * retries)
      warn "HikerAPI 429 Rate Limited. Retrying in #{delay}s... (attempt #{retries}/#{max_retries})"
      sleep(delay)
      retry
    end
    raise e
  end
end

#userObject



18
19
20
# File 'lib/hikerapi/client.rb', line 18

def user
  @user ||= User.new(self)
end