Class: Unbounce::Api

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

Constant Summary collapse

MAX_RESULT_LIMIT =
'1000'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Api

Returns a new instance of Api.



13
14
15
16
17
18
19
20
21
# File 'lib/unbounce.rb', line 13

def initialize(opts = {})
  api_version = opts[:api_version].nil? ? "application/vnd.unbounce.api.v0.4+json" : "application/vnd.unbounce.api.v#{opts[:api_version]}+json"
  @headers = {Accept: api_version}
  @url = opts[:api_url].nil? ? "https://api.unbounce.com" : opts[:api_url]
  @auth = 'Basic ' + Base64.encode64(opts[:key]).chomp  
  @account_id = opts[:account_id].to_s if opts[:account_id]  

  #  -H "Accept: application/vnd.unbounce.api.v0.4+json"
end

Instance Method Details

#get_accounts(opts = {}) ⇒ Object

Account



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/unbounce.rb', line 102

def get_accounts(opts = {})
  opts[:path] = "/accounts"       

  if opts[:account_id]
    opts[:path] = opts[:path] + "/#{opts[:account_id]}/pages"
  else
    # opts[:data] = "accounts"
  end

  return get_responses(opts)  
end

#get_leads(opts = {}) ⇒ Object

Leads



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/unbounce.rb', line 131

def get_leads(opts = {})

  if opts[:page_id]
    opts[:path] = "/pages/#{opts[:page_id]}/leads"
  #  opts[:data] = "leads"
  elsif opts[:lead_id]
    opts[:path] = "/leads/#{opts[:lead_id]}"
  end   

  return get_responses(opts)  
end

#get_pages(opts = {}) ⇒ Object

Pages



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/unbounce.rb', line 115

def get_pages(opts = {})
  opts[:path] = "/pages"       

  if opts[:account_id]
    opts[:path] = "/accounts/#{opts[:account_id]}/pages"
  elsif opts[:page_id]
    opts[:path] = opts[:path] + "/#{opts[:page_id]}"
    opts[:path] = opts[:path] + "/form_fields" if opts[:form_fields] == true
  else
   # opts[:data] = "pages" 
  end    

  return get_responses(opts)  
end

#get_responses(opts = {}) ⇒ Object



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
93
94
95
96
97
98
99
# File 'lib/unbounce.rb', line 51

def get_responses(opts = {})
  params = set_params(opts)
  response = parse_json(RestClient.get(@url+params[:path], params: params,Authorization: @auth, headers: @headers)).body      
  
  more = response["metadata"]["next"].nil? ?  nil : response["metadata"]["next"]["href"]

  while more
    more_response = parse_json(RestClient.get(more,Authorization: @auth, headers: @headers)).body

    if more_response["leads"]
      response["leads"].push(more_response["leads"])
      response["leads"].flatten!
    end

    if more_response["domains"]
      response["domains"].push(more_response["domains"])
      response["domains"].flatten!
    end

    if more_response["pages"]
      response["pages"].push(more_response["pages"])
      response["pages"].flatten!
    end

    if more_response["page_groups"]
      response["page_groups"].push(more_response["page_groups"])
      response["page_groups"].flatten!
    end

    if more_response["sub_accounts"]
      response["sub_accounts"].push(more_response["sub_accounts"])
      response["sub_accounts"].flatten!
    end

    if more_response["accounts"]
      response["accounts"].push(more_response["accounts"])
      response["accounts"].flatten!
    end

    if more_response["form_fields"]
      response["form_fields"].push(more_response["form_fields"])
      response["form_fields"].flatten!
    end
    
    more = more_response["metadata"]["next"].nil? ?  nil : more_response["metadata"]["next"]["href"]
  end                 
  
  return response
end

#parse_json(response) ⇒ Object



27
28
29
30
# File 'lib/unbounce.rb', line 27

def parse_json(response)
  body = JSON.parse(response.to_str) if response.code == 200
  OpenStruct.new(code: response.code, body: body)
end

#set_account_id(opts = {}) ⇒ Object



23
24
25
# File 'lib/unbounce.rb', line 23

def (opts = {})
  @account_id = opts[:account_id].to_s
end

#set_params(opts = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/unbounce.rb', line 32

def set_params(opts = {})
  params = {}
  #Result params
  params[:path] = opts[:path] if opts[:path]
  params[:sort_order] = opts[:sort_order] if opts[:sort_order] && (opts[:sort_order] == "asc" || opts[:sort_order] == "desc") # asc, desc
  params[:count] = true if opts[:count] # When true, don't return the response's collection attribute.
  params[:include_sub_pages] = true if opts[:include_sub_pages] # When true, include sub page form fields in the response

  #Page params      
  params[:from] = opts[:from] if opts[:from] # Limit results to those created after from. Example: 2014-12-31T00:00:00.000Z
  params[:to] = opts[:to] if opts[:to] # Limit results to those created before to. Example: 2014-12-31T23:59:59.999Z
  params[:offset] = opts[:offset] if opts[:offset] # Omit the first offset number of results. Example: 3
  params[:limit] =  (opts[:limit] != nil && opts[:limit].to_i < MAX_RESULT_LIMIT.to_i && opts[:limit].to_i > 0 ) ? opts[:limit] : MAX_RESULT_LIMIT  # Only return limit number of results. Example: 100
  params[:with_stats] = opts[:with_stats] if opts[:with_stats] # When true, include page stats for the collection. 
  params[:role] = opts[:role] if opts[:role] # Restricts the scope of the returned pages. one of viewer, author
 
  return params
end