Class: Hikerapi::Client
- Inherits:
-
Object
- Object
- Hikerapi::Client
- Defined in:
- lib/hikerapi/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #comments(media_id_or_code_or_url, options = {}) ⇒ Object
- #get(endpoint, params = {}, options = {}) ⇒ Object
-
#initialize(config = nil, api_key: nil) ⇒ Client
constructor
A new instance of Client.
- #media ⇒ Object
- #post(endpoint, params = {}, options = {}) ⇒ Object
- #posts(username_or_id_or_url, options = {}) ⇒ Object
- #reels(username_or_id_or_url, options = {}) ⇒ Object
- #request(endpoint, method: :get, params: {}, options: {}) ⇒ Object
- #user ⇒ Object
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_key ⇒ Object (readonly)
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/hikerapi/client.rb', line 11 def api_key @api_key end |
#config ⇒ Object (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, = {}) media.comments(media_id_or_code_or_url, ) end |
#get(endpoint, params = {}, options = {}) ⇒ Object
38 39 40 |
# File 'lib/hikerapi/client.rb', line 38 def get(endpoint, params = {}, = {}) request(endpoint, method: :get, params: params, options: ) end |
#media ⇒ Object
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 = {}, = {}) request(endpoint, method: :post, params: params, 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, = {}) user.medias(username_or_id_or_url, ) 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, = {}) user.clips(username_or_id_or_url, ) 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 = [: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 = [: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 = [:open_timeout] || config.open_timeout http.read_timeout = [:read_timeout] || config.read_timeout max_retries = [: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 |