Class: ScrapeCreators::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/scrape_creators/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/scrape_creators/client.rb', line 13

def initialize(config = nil, api_key: nil)
  @config = config || ScrapeCreators.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/scrape_creators/client.rb', line 11

def api_key
  @api_key
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

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



30
31
32
33
34
35
# File 'lib/scrape_creators/client.rb', line 30

def comments(url_or_code, options = {})
  url = normalize_url(url_or_code)
  params = { url: url }.merge(options)
  res = get("/v2/instagram/post/comments", params)
  res.is_a?(Hash) ? (res["comments"] || []) : []
end

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



37
38
39
# File 'lib/scrape_creators/client.rb', line 37

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

#post(url_or_code, options = {}) ⇒ Object



24
25
26
27
28
# File 'lib/scrape_creators/client.rb', line 24

def post(url_or_code, options = {})
  url = normalize_url(url_or_code)
  params = { url: url }.merge(options)
  get("/v1/instagram/post", params)
end

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



18
19
20
21
22
# File 'lib/scrape_creators/client.rb', line 18

def posts(username_or_handle, options = {})
  params = { handle: username_or_handle }.merge(options)
  res = get("/v2/instagram/user/posts", params)
  res.is_a?(Hash) ? (res["items"] || []) : []
end

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



41
42
43
44
45
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
# File 'lib/scrape_creators/client.rb', line 41

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, "Scrape Creators API key is missing. Set ENV['SCRAPE_CREATORS_API_KEY'] or configure via ScrapeCreators.configure { |c| c.api_key = '...' }"
  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-api-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

  res = http.request(req)
  parse_response(res)
end